Question

Hi I created an activity, in that i want to get json data in spinner via List Adapter. How it possible please help me.

Using this code i get json data to Array List.

ArrayList<HashMap<String, String>> officeList;   
for(int i=0; i<jsonArray.length();i++)
    {
        jsonObj = jsonArray.getJSONObject(i);
        O_Id = jsonObj.getString("O_Id");
        O_Name = jsonObj.getString("O_Name");                           

        HashMap<String, String> contact = new HashMap<String, String>();
        contact.put("office_id", O_Id);
        contact.put("office_name", O_Name);                         
        officeList.add(contact);
    }

I want to get only "office_name" in the spinner. How to attach officeList into spinner.

Was it helpful?

Solution 2

For that you can make Custom Adapter and pass your ArrayList to that adapter constructor as a Parameter.

Something Like that..

   CustomAdapter cp = new CustomAdapter(YourActivityName.this,officeList); 
   spinner.setAdapter(cp);

for that you have also make one custom layout xml file in which your TextView will be.

Now CustomAdapter extebds with BaseAdapter

and after that in getView() method you can get officename by

   String ofcname  = yourarraylist.get(position).get("office_name");
   txtview.setText(ofcname);

OTHER TIPS

Instead of hashmap get the values in the java pojo object and override toString method.

class MyOffice{

private int office_id;
private String office_name;

  @Override
   public String toString() {
    // TODO Auto-generated method stub
    return office_name;
   }

} 

and set the adapter to the spinner as ArrayAdapter<MyOffice>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top