Reputation: 75
I've got a jsonArray List which has got two property. I want to get selected names id. I'm using spinner tool but I can only bind string array of name. is there another tool that I can bind both id and name on android.
[ {"name":"ILKER","ID":55}, {"name":"ILKER","ID":5}, {"name":"MEHMET","ID":3} ]
Upvotes: 0
Views: 262
Reputation: 6438
Using a custom data adapter is the most flexible option.
ArrayList<MyObject> objects = new ArrayList<MyObject>();
ArrayAdapter<MyObject> adapter = new ArrayAdapter<MyObject>(getContext(), android.R.layout.simple_spinner_item, objects){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyObject myObject = getItem(position);
// inflate view, set values, return view
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
MyObject myObject = getItem(position);
// inflate view, set values, return view
}
};
If you are looking for something quick and easy, bind the list/array of objects to the spinner and override toString() for that object.
private void test() {
ArrayList<MyObject> objects = new ArrayList<MyObject>();
ArrayAdapter<MyObject> adapter = new ArrayAdapter<MyObject>(getContext(), android.R.layout.simple_spinner_item, objects);
}
class MyObject {
public int Id;
public String Name;
@Override
public String toString() {
return Name + " " + Id;
}
}
Upvotes: 0
Reputation: 7641
If you just want to id & name in a single spinner then you can directly append those two by taking arraylist of datatype string.
For example,
Arraylist al = new Arraylist();
al.add(name + "" + id);
Above code will solve your problem. Set this arraylist to your arrayadapter.
Upvotes: 0
Reputation: 5636
You can create two arrays one for name and another for id field. Bind name array with spinner and when user selects any item from spinner, you will get the index of the item selected in the name array, so using same index value you can get the corresponding id from ID array.
Upvotes: 2