-1

I have locations save in the database and I am trying to retrieve them into a ListView. Each location has a name, a latitude and a longitude but the only thing that I am trying to display on the list in the name of the location and leave the latitude and longitude in the background so I can save them to the database based on what the location name selected by the user in the ListView.

Here is my current code:

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

        }
    });
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Evans Munatsa
  • 39
  • 1
  • 7
  • not sure what's that you're asking for? – Krzysztof Kubicki Jun 13 '18 at 08:32
  • when click it you want just name? – Radesh Jun 13 '18 at 08:36
  • I think you should create a pojo with name, latitude and longitude and pass the pojos to your list and to the array adapter. And overwrite the toString method of your pojo to return the name. – Vall0n Jun 13 '18 at 08:40
  • @Redesh Yes l just want the name but display but keep the latitude and longitude in the background so when a user clicks on a particular item the name, latitude and longitude are save in the database – Evans Munatsa Jun 13 '18 at 08:44

2 Answers2

0

ok use this code

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

        }
    });


}

just add new List locationName and put it to ListView Adapter

Radesh
  • 13,084
  • 4
  • 51
  • 64
  • you just duplicated my code and put it in a different format but that won't solve what l need to solve – Evans Munatsa Jun 13 '18 at 08:48
  • l have that but the only thing l want to display in the listView for the users is the LocationName and leave the longitude and latitude in the background because some users might not understand all that information in the list – Evans Munatsa Jun 13 '18 at 08:52
  • that does not solve anything instead l am just creating a new ListView adapter but when l click l wont get the longitude and Latitude to save into the database – Evans Munatsa Jun 13 '18 at 10:02
  • can you share R.layout.location – Radesh Jun 13 '18 at 11:01
0

Create a POJO class

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

Now create objects of this class in your for loop for items. So, instead of the items.add(), you'll create new objects.

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

Now pass these objects into your adapter. For that, you need to use a custom implementation of ArrayAdapter.java. For this check : How to use ArrayAdapter<myClass>

Apoorv Parmar
  • 274
  • 4
  • 9