我正在尝试使用json填充两个微调器。我能够使用arraylist获得两个微调器,但我只能选择第二个微调器中的最后一行。
第一个微调器允许用户选择食物,然后使用LoadResults类更新微调器。第二个微调器显示相同食物的不同成分列表,并显示使用第二个旋转器选择的成分对食物的评级。 / p>
现在的问题是,当我使用第一个微调器选择食物时,第二个微调器会填充,但它只让我选择最后一行,它只显示我选择最后一行时的评级。由于某种原因,元素的数量第二个微调器不会使用addListenerOnSpinnerItemSelection2中的setOnItemSelectedListener进行更新.json是数组中对象数组中的对象。
如果我得到一些帮助,我将不胜感激。
这是我的代码:
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
}