Java 从csv到ArrayList
我一直很难将csv文件中的数据放入arrayList,这样我以后就可以用它来制作多段线了。不管我怎么做,我都不能让它正常工作。有人知道我做错了什么吗。 我用干杯来暂时看看结果 我希望代码能给我一个ArrayList,但它只会返回一个空列表 我正在添加“映射”活动和将文件写入main活动的函数 地图活性Java 从csv到ArrayList,java,android,csv,arraylist,Java,Android,Csv,Arraylist,我一直很难将csv文件中的数据放入arrayList,这样我以后就可以用它来制作多段线了。不管我怎么做,我都不能让它正常工作。有人知道我做错了什么吗。 我用干杯来暂时看看结果 我希望代码能给我一个ArrayList,但它只会返回一个空列表 我正在添加“映射”活动和将文件写入main活动的函数 地图活性 public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private Google
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
FileInputStream fileInputStream = openFileInput("trip_file");
InputStreamReader inputReader = new InputStreamReader(fileInputStream);
BufferedReader BufferedReader = new BufferedReader(inputReader);
List<LatLng> latLngList = new ArrayList<LatLng>();
String line = "";
try {
while( (line = BufferedReader.readLine()) != null) // Read until end of file
{
double lat = Double.parseDouble(line.split(", ")[0]);
double lon = Double.parseDouble(line.split(", ")[1]);
latLngList.add(new LatLng(lat, lon));
}
} catch (IOException e) {
e.printStackTrace();
}
String teest = String.valueOf(latLngList);
Toast.makeText(getBaseContext(), teest,
Toast.LENGTH_SHORT).show();
}
}
我建议您使用CSVParser(Apache 2.0许可证),它很好而且简单:
这有点模糊。因此,在具体问题上做得更好。你期望什么,发生了什么,避免仅仅给我们一堆乱七八糟的代码,并要求我们“修复它”。
public void onLocationChanged(Location location) {
double lat = location.getLatitude();
double lon = location.getLongitude();
myList.add(lat);
myList.add(lon);
String file_name = "trip_file";
try {
String skrive = String.valueOf(myList);
FileOutputStream fileOutputStream = openFileOutput(file_name, MODE_PRIVATE);
fileOutputStream.write(skrive.getBytes());
fileOutputStream.close();
Toast.makeText(getApplicationContext(), "Location saved",Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}