【问题标题】:Filter list items with radiobuttons in android在android中使用单选按钮过滤列表项
【发布时间】:2015-11-26 13:27:26
【问题描述】:

假设我有三个单选按钮(全部、水果、蔬菜),我想根据选中的单选按钮过滤列表中的项目。例如,如果用户选中“水果”单选按钮,则仅应显示列表中的水果项目。 这是我的 CustomListAdapter,listItems 和 descriptions 充满了水果和蔬菜的信息。

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 来仅显示我想要的项目,但是如何使用单选按钮过滤它们?

【问题讨论】:

    标签: android listview filter radio-button


    【解决方案1】:
    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
    

    【讨论】:

      猜你喜欢
      • 2020-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-09
      • 1970-01-01
      相关资源
      最近更新 更多