I am trying to populate two spinners using json.I am able to get the two spinners using arraylist but I can only select the last row in the second spinner.
The first spinner let the user select the food and then update the spinners using LoadResults class.The second spinner show list of different ingredients for the same food and it shows the rating for the food with the selected ingredients using second spinner.
Now the problem is when I select a food using first spinner the second spinner gets populate but it only let me select the the last row and it only show the rating when I select last row.For some reason the number of elements in second spinner don't get updated using setOnItemSelectedListener in addListenerOnSpinnerItemSelection2.The json is objects in array of object in array.
Would appreciate it if I get some help.
here is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.food);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
listview = (ListView) findViewById(R.id.listview);
list = new ArrayList<HashMap<String, String>>();
ItemsOnSpinner1();
ItemsOnSpinner2();
addListenerOnSpinnerItemSelection();
addListenerOnSpinnerItemSelection2();
// Loading in Background Thread
new LoadResults().execute();
// get listview
// ListView listview = getListView();
}
public void ItemsOnSpinner1() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
List<Object> list = new ArrayList<Object>();
list.add("Food1");
list.add("Food2");
list.add("Select Food");
final int listsize = list.size() - 1;
ArrayAdapter<Object> dataAdapter = new ArrayAdapter<Object>(this,android.R.layout.simple_spinner_item, list) {
@Override
public int getCount() {
return(listsize);
}
};
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
spinner1.setSelection(listsize);
}
public void addListenerOnSpinnerItemSelection() {
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
FOOD_S=parent.getItemAtPosition(pos).toString();
if(FOOD_S.equals("Food1"))
{
FOOD_SELECT="Food1";
resetList = true;
ingNames.clear();
new LoadResults().execute();
}
else if(FOOD_S.equals("Food2"))
{
FOOD_SELECT="Food2";
ingNames.clear();
resetList = true;
new LoadResults().execute();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
FOOD_SELECT="Food2";
}
});
}
public void addListenerOnSpinnerItemSelection2() {
ArrayAdapter<String> dataAdapter2 =new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, ingNames);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter2);
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent2, View view, int pos2,
long id) {
INGREDIENTS_S=parent2.getItemAtPosition(pos2).toString();
for(int l=0;l<ingNames.length();l++){
if(INGREDIENTS_S.equals(ingNames.get(l)))
{
resetList = true;
ingNames.clear();
new LoadResults().execute();
}
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
public void ItemsOnSpinner2() {
spinner2 = (Spinner) findViewById(R.id.spinner2);
ingNames.add("Select ingredients");
}
class LoadResults extends AsyncTask<String, String, String> {
...code for getting json array from url
}
0 comments:
Post a Comment