【问题标题】:Read NULL values from CSV file从 CSV 文件中读取 NULL 值
【发布时间】:2019-01-30 02:39:23
【问题描述】:

我的申请有两个问题。第一个是我正在尝试从具有空值的 CSV 文件中读取列。第二个是基于从 CSV 读取的值,我应该更改上传到谷歌地图上的标记的颜色,但现在颜色根据读取的最后一个值更改,并且所有标记都获得相同的颜色而不是颜色基于它们对应的值。所以我的问题是:当应用程序从 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();
    }

}

【问题讨论】:

  • String[] data = line.split(","); if(data.length 大于 3) && data[3] not null) stare = String.valueOf(data[3]);

标签: java android csv google-maps-markers


【解决方案1】:

stare 值定义另一个列表:
List&lt;String&gt; stares = new ArrayList&lt;String&gt;();
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 列,应用程序不会崩溃。

【讨论】:

  • 非常感谢您的回答。这与提到颜色代码需要在添加标记之前一起使用。
  • 我很高兴_______
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-05
  • 2012-03-09
  • 2015-05-23
  • 2013-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多