【问题标题】:Check box change its value when i scroll the list滚动列表时复选框更改其值
【发布时间】:2023-03-17 13:20:02
【问题描述】:

Checkbox auto call onCheckedChange when listview scroll?

在此链接中找到了解决方案,但我仍然遇到问题..我的应用程序中有标签主机,所以一旦我切换到它,当我回到列表时,问题仍然会自动出现在我的第一个和最后一个项目中检查...帮帮我..提前谢谢

public class FoodAdapter extends ArrayAdapter {
    private List<Food> list= new ArrayList<Food>()  ;
    private Context context;
    public FoodAdapter(Context context, int resource) {
        super(context, resource);
        this.context = context;
    }

    static class ListHolder
    {
        TextView FOOD_NAME;
        CheckBox CHECK;

    }

    public void addToList(List<Food> list)
    {
        this.list = list;
    }

    public  int getCount()
    {
        return this.list.size();

    }

    public  Object getItem(int position)
    {
        return this.list.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View list = convertView;
        final ListHolder listHolder;
        if(convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            list = inflater.inflate(R.layout.custom_list, parent, false);
            listHolder = new ListHolder();
            listHolder.FOOD_NAME = (TextView) list.findViewById(R.id.textView);
            listHolder.CHECK = (CheckBox) list.findViewById(R.id.checkBox);
            list.setTag(listHolder);

        }
        else {
            listHolder = (ListHolder) list.getTag();
        }
        final Food food = (Food) getItem(position);
        listHolder.FOOD_NAME.setText(food.getFood_name());
        listHolder.CHECK.setOnCheckedChangeListener(null);
        listHolder.CHECK.setChecked(food.isSeleted());
        listHolder.CHECK.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    listHolder.CHECK.setChecked(true);
                    food.setSeleted(true);

                } else {
                    listHolder.CHECK.setChecked(false);
                    food.setSeleted(true);
                }
            }
        });


        return list;
    }
}

【问题讨论】:

标签: android listactivity listadapter


【解决方案1】:

您可以通过使用简单的代码来实现这一点,Listview 已经具有属性 ChoiceMode。您可以将其设置为 CHOICE_MODE_MULTIPLE。

这是来自 android 示例项目的代码示例-

https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/view/List11.java?autodive=0%2F%2F

【讨论】:

    猜你喜欢
    • 2023-04-06
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 2018-12-16
    • 1970-01-01
    相关资源
    最近更新 更多