ListView并选择一个位置

时间:2018-06-13 08:30:17

标签: android listview

我在数据库中保存了位置,我正在尝试将它们检索到ListView。每个位置都有一个名称,一个纬度和一个经度,但我唯一想要在列表中显示的位置名称,并在背景中保留纬度和经度,所以我可以根据什么将它们保存到数据库中用户在ListView

中选择的位置名称

这是我目前的代码:

public void onResponse(JSONArray response) {
    for (int i = 0; i < response.length(); i++) {
        JSONObject object = null;
        try {
            object = response.getJSONObject(i);
            items.add(object.getString("locationName")); items.add(object.getString("latitude")); items.add(object.getString("longitude"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), R.layout.location, items);
    listView.setAdapter(adapter);

    FuturaTextViewBold listviewHearder = (FuturaTextViewBold) customView.findViewById(R.id.tv_header);
    listviewHearder.setBackgroundColor(Color.GREEN);
    listviewHearder.setText("SELECT A LOCATION");

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String SelectedItem = (String) listView.getItemAtPosition(position);
            startLocation.setText(SelectedItem);

        }
    });
}

2 个答案:

答案 0 :(得分:0)

确定使用此代码

public void onResponse(JSONArray response) {
    List<String> locationName = new ArrayList<>();
    for (int i = 0; i < response.length(); i++) {
        JSONObject object = null;
        try {
            object = response.getJSONObject(i);
            locationName.add(object.getString("locationName"));
            items.add(object.getString("locationName")); items.add(object.getString("latitude")); items.add(object.getString("longitude"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), R.layout.location, locationName);
    listView.setAdapter(adapter);

    FuturaTextViewBold listviewHearder = (FuturaTextViewBold) customView.findViewById(R.id.tv_header);
    listviewHearder.setBackgroundColor(Color.GREEN);
    listviewHearder.setText("SELECT A LOCATION");

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String SelectedItem = (String) listView.getItemAtPosition(position);
            startLocation.setText(SelectedItem);

        }
    });


}

只需添加新的List locationName并将其放入ListView Adapter

答案 1 :(得分:0)

创建POJO类

class Pojo {
    double latitude;
    double longitude;
    String name;
}

现在在for循环中为项创建此类的对象。 因此,您将创建新对象,而不是items.add()。

List<Pojo> pojos = new ArrayList<>():
for (int i = 0; i < response.length(); i++) {
    JSONObject object = null;
    try {
        object = response.getJSONObject(i);
        Pojo obj = new Pojo(); // use suitable name for the class
        obj.name = object.getString("locationName");
        obj.latitude = object.getString("latitude");
        obj.longitude = object.getString("longitude");

        pojos.add(obj);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

现在将这些对象传递到适配器中。 为此,您需要使用ArrayAdapter.java的自定义实现。对于此检查:How to use ArrayAdapter<myClass>