假设我有三个radiobuttons(All,Fruits,Vegetables),我想根据检查的radionbutton过滤列表中的项目。例如,如果用户检查“Fruits”单选按钮,则只应显示列表中作为水果的项目。 这是我的CustomListAdapter,listItems和描述中填充了有关水果和蔬菜的信息。
public class CustomListAdapter extends ArrayAdapter<String> implements Filterable {
private final Activity context;
private final ArrayList<ArrayList<String>> listItems;
private final ArrayList<String> descriptions;
public CustomListAdapter(Activity context, ArrayList<String> descriptions, ArrayList<ArrayList<String>> listItems) {
super(context, R.layout.list_view, descriptions);
this.context = context;
this.descriptions = descriptions;
this.listItems = listItems;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_view, null,true);
TextView txtDescription = (TextView) rowView.findViewById(R.id.description);
TextView txtDetails = (TextView) rowView.findViewById(R.id.details);
TextView txtDate = (TextView) rowView.findViewById(R.id.date);
TextView txtValue = (TextView) rowView.findViewById(R.id.value);
txtDescription.setText(descriptions.get(position));
txtDetails.setText(listItems.get(position).get(0));
txtDate.setText(listItems.get(position).get(1));
txtValue.setText(listItems.get(position).get(2));
return rowView;
}
}
这是与问题相关的代码的一部分:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
new GetInfoTask().execute();
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.filter);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// do something to filter items on listview
if (checkedId==R.id.all) {
} else if (checkedId==R.id.fruits) {
} else if (checkedId==R.id.vegetables) {
}
}
});
}
private void display() {
final CustomListAdapter adapter = new CustomListAdapter(this, descriptions, itemsList);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
}
我可能必须使用notifyDataSetChanged来显示我想要的项目,但是如何使用radiobuttons过滤它们?
答案 0 :(得分:0)
You can add list base on selected group from radio button
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.filter);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// do something to filter items on listview
if (checkedId==R.id.all) {
display(filerlist,discriptipnlist);
} else if (checkedId==R.id.fruits) {
display(filerlist,discriptipnlist);
} else if (checkedId==R.id.vegetables) {
display(filerlist,discriptipnlist);
}
}
});
}
private void display(ArrayList<ArrayList<String>> itemsList,ArrayList<ArrayList<String>> descriptions) {
final CustomListAdapter adapter = new CustomListAdapter(this, descriptions, itemsList);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
}
i hope this will work for you