从 CSV 文件中读取 NULL 值

Read NULL values from CSV file

我的申请有两个问题。第一个是我正在尝试从具有空值的 CSV 文件中读取一列。第二个是根据从 CSV 中读取的值,我应该更改上传到 google 地图上的标记的颜色,但是现在颜色根据读取的最后一个值更改并且所有标记都相同颜色而不是基于它们对应值的颜色。所以我的问题是:当应用程序从 CSV 获取空值时,如何阻止应用程序崩溃,我是否根据每个标记的对应值更改它们的颜色?

CSV 文件:

1,45.66680458,25.60458787,salvat
2,45.66672655,25.6047773,gresit
3,45.66670734,25.60465392,

更新后的工作代码:

 public void Upload() throws IOException {

    File file = new File(getExternalFilesDir(null), "fisier.csv");
    if (file.exists()) {
        InputStream instream = new FileInputStream(file);
        InputStreamReader inputreader = new InputStreamReader(instream);
        BufferedReader reader = new BufferedReader(inputreader);
        List<LatLng> latLngList = new ArrayList<LatLng>();
        String line = " ";
        List<String> stares = new ArrayList<String>();
        while ((line = reader.readLine()) != null) // Read until end of file
        {
            String[] tokens = line.split(",", 4);
            double lat = Double.parseDouble(tokens[1]);
            double lon = Double.parseDouble(tokens[2]);
            stare = String.valueOf(tokens[3]);

            latLngList.add(new LatLng(lat, lon));
            stares.add(stare);
            mMap.setOnMarkerClickListener(this);




    }
        reader.close();
        for (int i = 0; i < latLngList.size(); i++) {
            float color = 0;

            if (stares.get(i).equals("gresit")) {
                color = BitmapDescriptorFactory.HUE_BLUE;
            }
            else if (stares.get(i).equals("salvat")) {
                color = BitmapDescriptorFactory.HUE_GREEN;
            }
            else {
                color = BitmapDescriptorFactory.HUE_RED;
            }
            mMap.addMarker(new MarkerOptions()
                    .position(latLngList.get(i))
                    .title("Din CSV!")
                    .draggable(true)
                    .icon(BitmapDescriptorFactory.defaultMarker(color)));

                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLngList.get(i), DEFAULT_ZOOM));

    }

        Toast.makeText(MapsActivity.this,"Fisierul a fost incarcat", Toast.LENGTH_SHORT).show();
        }


    else
    {
        Toast.makeText(MapsActivity.this,"Nu exista fisierul!", Toast.LENGTH_SHORT).show();
    }

}

stare 值定义另一个列表:
List<String> stares = new ArrayList<String>();
latLngList.add(new LatLng(lat, lon)); 之后的 while 循环中添加:
stares.add(stare);

更改for循环:

    for (int i = 0; i < latLngList.size(); i++) {
        if (stares.get(i).equals("gresit")) {
            color = BitmapDescriptorFactory.HUE_BLUE;
        } else if (stares.get(i).equals("salvat")) {
            color = BitmapDescriptorFactory.HUE_GREEN;
        } else {
            color = BitmapDescriptorFactory.HUE_RED;
        }
        mMap.addMarker(new MarkerOptions()
                .position(latLngList.get(i))
                .title("Din CSV!")
                .draggable(true)
                .icon(BitmapDescriptorFactory.defaultMarker(color))

    };

更新:
stare = String.valueOf(tokens[3]); 替换为:

try {
    stare = String.valueOf(tokens[3]);
} catch (Exception e) {
    stare = "";
    e.printStackTrace();
}

所以如果没有第 4 列,应用程序不会崩溃。