我有一个包含类别列表的Spinner,它从JSON获取数据。我想,当选择 All Category 以外的项目时,另一个子类别的微调器变得可见,并根据其类别从JSON加载数据。
之前我设法做到了,但是在离开它几天并完成其他部分之后,它就不再有用了。
由于第一个加载到微调器中的是所有类别,因此它触发了setOnItemSelectedListener()
并隐藏了subcategorySpinner
。如果其他项目被选中,则subcategorySpinner
仍未显示。
代码逻辑有问题吗?请帮忙。
categorySpinner = (Spinner) rootView.findViewById(R.id.categorySpinner);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.spinner_item, categoryList);
adapter2.setDropDownViewResource(R.layout.spinner_dropdown_item);
categorySpinner.setAdapter(adapter2);
categorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
categoryString = categorySpinner.getSelectedItem().toString().replace(" ", "%20");
if (categorySpinner.getSelectedItem().toString().equals("All Category")) {
Log.d("debug", "all category");
subcategorySpinner.setVisibility(View.GONE);
} else {
Log.d("debug", "other than all category");
subcategorySpinner.setVisibility(View.VISIBLE);
generateSubcategorySpinner(Config.GET_SUBCATEGORY_URL+"?category="+categoryString);
}
}
});
答案 0 :(得分:1)
将您的代码更改为
categorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
categoryString = parent.getItemAtPosition(position).toString();
Log.d("debug", categoryString+"..."+position);
if (categoryString.equals("All Category")) {
Log.d("debug", "all category");
subcategorySpinner.setVisibility(View.GONE);
} else {
Log.d("debug", "other than all category");
subcategorySpinner.setVisibility(View.VISIBLE);
categoryString = categoryString.replace(" ", "%20")
generateSubcategorySpinner(Config.GET_SUBCATEGORY_URL+"?category="+categoryString);
}
}
答案 1 :(得分:0)
试试此代码段
Spinner dropdown = (Spinner) findViewById(R.id.type);
final String[] items = new String[]{"SELECT TYPE OF OFFER","PERCENTAGE", "AMOUNT", "ITEM"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.spinner_item, items);
dropdown.setAdapter(adapter);
String selectedItem = dropdown.getSelectedItem().toString();
答案 2 :(得分:0)
试试这个
categorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
categoryString = parent.getItemAtPosition(position).toString().replace(" ", "%20");
if (categoryString.equals("All Category")) {
Log.d("debug", "all category");
subcategorySpinner.setVisibility(View.GONE);
} else {
Log.d("debug", "other than all category");
subcategorySpinner.setVisibility(View.VISIBLE);
generateSubcategorySpinner(Config.GET_SUBCATEGORY_URL+"?category="+categoryString);
}
}
答案 3 :(得分:0)
你可以直接使用,参数(位置)
而不是, categoryString = parent.getItemAtPosition(position).toString();
你可以使用, categoryString = position.toString();
答案 4 :(得分:0)
嗯,我不能完全理解我在逻辑上所做的事情,因为它看起来没什么不同但是这解决了它。哦,好吧......不管怎么说,无论是漂浮在船上,我猜。
使用H Raval的建议,我将if else
移动到一个方法中,现在它就像我想要的那样工作。
categorySpinner = (Spinner) rootView.findViewById(R.id.categorySpinner);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.spinner_item, categoryList);
adapter2.setDropDownViewResource(R.layout.spinner_dropdown_item);
categorySpinner.setAdapter(adapter2);
String str = categorySpinner.getSelectedItem().toString();
Log.d("debug", "str "+str);
categorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
categoryString = parent.getItemAtPosition(position).toString();
Log.d("debug", categoryString+"..."+position);
subCategorySpinnerView(categoryString);
}
});
private void subCategorySpinnerView(String categoryString) {
if (categoryString.equals("All Category")) {
Log.d("debug", "all category");
subcategorySpinner.setVisibility(View.GONE);
} else {
Log.d("debug", "other than all category");
subcategorySpinner.setVisibility(View.VISIBLE);
categoryString = categoryString.replace(" ", "%20");
generateSubcategorySpinner(Config.GET_SUBCATEGORY_URL+"?category="+categoryString);
}
}