Search code examples
androidjsonspinner

Id is not getting perfect values, Get id based on name from spinner onitemselected


I'm trying to load Some data from JSON to Android spinner, the data is loading perfectly but when I'm trying to get id for the selected item its showing the last name id value whatever the item is selected.

Here's my JSON response

 [{"name":"aaa","id":"1"},
  {"name":"bbb","id":"2"},
 {"name":"ccc", "id":"3"},
{"name":"ddd",  "id":"4"}]

Mainactivity.java

protected void receivedata(final String result){
    final ArrayList<String> list = new ArrayList<>();
    final ArrayList<GetDataAdapter> datalist = new ArrayList<>();
    list.clear();
    try{
        GetDataAdapter GetDatadp = new GetDataAdapter();
        JSONArray jArray = new JSONArray(result);
        for(int i=0;i<jArray.length();i++){
            JSONObject jsonObject=jArray.getJSONObject(i);
            GetDatadp.setName(jsonObject.getString("name"));
            GetDatadp.setId(jsonObject.getString("id"));
            datalist.add(GetDatadp);

            list.add(jsonObject.getString("name"));
        }
    }
    catch(JSONException e){
        e.printStackTrace();
    }

    final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,R.layout.spinner_layout, R.id.txt, list);
    sp.setAdapter(adapter);
    sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
            public void onItemSelected(AdapterView<?> arg0, View view, int position, long row_id) {
            String id = datalist.get(position).getId();

            Toast.makeText(getApplicationContext(),"Value   " +id , Toast.LENGTH_LONG).show();

        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            //Do something
        }
    }
    );
}

GetDataAdapte.java

public class GetDataAdapter {
String name;
String id;

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
}

So when I'm trying to select names aaa/bbb or whatever it is showing the id value 4.how to get display the correct id.


Solution

  • To avoid a naming confusion between Adapter and DTO (model class)

    Adapter:

    An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.

    However, you problem as I understood because you only created a one object which is GetDataAdapter in your case, so in order to get a valid list of GetDataAdapter objects you have to create a new instance in the loop, take a look for the updated code below :

     try{
        GetDataAdapter GetDatadp ;
        JSONObject jsonObject;
        JSONArray jArray = new JSONArray(result);
    
        for(int i=0;i<jArray.length();i++){
    
            jsonObject=jArray.getJSONObject(i);
    
            GetDatadp = new GetDataAdapter();
            GetDatadp.setName(jsonObject.getString("name"));
            GetDatadp.setId(jsonObject.getString("id"));
            datalist.add(GetDatadp);
    
            list.add(jsonObject.getString("name"));
        }
    }
    catch(JSONException e){
        e.printStackTrace();
    }