我创建了一个简单的应用程序,它将从editText中记录用户输入,并将它放入我的SD卡上的csv中.现在我的问题是读取csv并将其作为我的应用程序上的下一个活动的变量.
这是我的主要活动:
package com.willardcuizon.differentialgps;import java.io.file;import androID.location.LocationListener;import androID.location.LocationManager;import androID.os.Bundle;import androID.app.Activity;import androID.content.Context;import androID.vIEw.Menu;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.Toast;public class Main extends Activity implements OnClickListener{button start;EditText lat;EditText lon;LocationListener locList;CSVfileWriter csv;StringBuffer filePath;file file;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); start = (button) findVIEwByID(R.ID.button1); start.setonClickListener(this); lat = (EditText) findVIEwByID(R.ID.editText1); lon = (EditText) findVIEwByID(R.ID.editText2); filePath = new StringBuffer(); filePath.append("/sdcard/surveyedGPS.csv"); file = new file(filePath.toString()); csv = new CSVfileWriter(file);}@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true;}@OverrIDepublic voID onClick(VIEw v) { // Todo auto-generated method stub if(v.getID()==R.ID.button1){ LocationManager locman = (LocationManager)getSystemService(Context.LOCATION_SERVICE); if(locman.isProvIDerEnabled(LocationManager.GPS_PROVIDER)){ Toast.makeText( getApplicationContext(), "GPS is Enabled.", Toast.LENGTH_SHORT ).show(); csv.writeheader(lat.getText().toString()); csv.writeheader(lon.getText().toString()); } else{ Toast.makeText( getApplicationContext(), "GPS is Disabled.Please enable GPS", Toast.LENGTH_SHORT ).show(); } }}}
这是我的CSVWriter类:
package com.willardcuizon.differentialgps;import java.io.file;import java.io.fileWriter;import java.io.IOException;import java.io.PrintWriter;public class CSVfileWriter {private PrintWriter csvWriter; private file file;public CSVfileWriter(file file) {this.file = file;}public voID writeheader(String data) {try { if (data != null) { csvWriter = new PrintWriter(new fileWriter(file, true)); csvWriter.print(","); csvWriter.print(data); csvWriter.close(); } } catch (IOException e) { e.printstacktrace(); }}}
解决方法:
您应该考虑使用this或this库,因为它将负责编写/解析csv,并在其值中使用quote作为字符.
> https://code.google.com/p/secrets-for-android/source/browse/trunk/src/au/com/bytecode/opencsv/
> http://opencsv.sourceforge.net/
以上是内存溢出为你收集整理的java – Android读取csv全部内容,希望文章能够帮你解决java – Android读取csv所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)