从csv到ArrayList

时间:2015-12-12 16:32:21

标签: java android csv arraylist

我一直无法将数据从csv文件转换为arrayList,所以我可以用它来制作Polylines。无论我尝试什么,我都无法让它正常工作。有没有人指出我做错了什么。 我正在使用Toast来临时看到结果。

我希望代码能给我一个ArrayList,但它只返回一个空列表

我添加了应该发生这种情况的地图活动以及在mainActivity中写入文件的功能

MapsActivity;

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();
            }

        }

MainActivity中的此代码写入文件;

    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();
    }

}

1 个答案:

答案 0 :(得分:0)

我建议您使用CSVParser(Apache 2.0许可证),这很简单: https://github.com/OpenBD/openbd-core/tree/master/src/au/com/bytecode/opencsv