我已经用复选框制作了列表视图,但在滚动列表视图时,更多复选框是随机选择的,它不会保持它们的位置



我已经用CheckBox进行了ListView,但是在滚动列表视图时,更多CheckBox是随机选择的,它不会保持它们的位置。请帮助我。提前致谢

public override View GetView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        view = context.LayoutInflater.Inflate(Resource.Layout.row1, parent, false);
    }
    Data item = this[position];
    view.FindViewById<TextView>(Resource.Id.title).Text = item.nameview;
    view.FindViewById<TextView>(Resource.Id.reporter).Text = item.EmailIdview;
    checkbox = view.FindViewById<CheckBox>(Resource.Id.checkbox);          
    checkbox.Tag = item.nameview;
    checkbox.SetOnCheckedChangeListener(new CheckedChangeListener(context, list, position));
    return view;
}
public class CheckedChangeListener : Java.Lang.Object, CompoundButton.IOnCheckedChangeListener {
    private Activity context;
    private List<Data> list;
    private  int mPosition;
    public CheckedChangeListener(Activity context, List<Data> list, int mPosition) {
        this.context = context;
        this.list = list;
        this.mPosition = mPosition;
    }
    public void OnCheckedChanged(CompoundButton buttonView, bool isChecked) {
        Group gr = new Group();                
        string name = buttonView.Tag.ToString();
        if (isChecked) {
            gr.checkboxSelected(name, isChecked);//list.ElementAt(mPosition)                    
        } else {                
            gr.setPosition(mPosition);                    
        }
    }           
}
您可以使用

模型类来保持复选框的选中状态。如我所见,您有一个名为 Data 的模型类

你可以像这样在数据模型类中添加isChecked,

public class Data {
    String name;
    boolean isChecked;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public boolean isChecked() {
        return isChecked;
    }
    public void setChecked(boolean checked) {
        isChecked = checked;
    }
}

现在你有某种适配器类:使用视图持有人模式

您可以设置标记复选框。我可以为此给出您的参考示例,您将得到所需的结果。

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import java.util.List;
public class DataAdapter extends BaseAdapter {

    public Activity context;
    public List<Data> list;
    public DataAdapter(Activity context, List<Data> list) {
        this.context = context;
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public Object getItem(int i) {
        return list.get(i);
    }
    @Override
    public long getItemId(int i) {
        return list.size();
    }
    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        Holder holder;
        if (view == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            view = inflater.inflate(R.layout.list_row, viewGroup, false);
            holder = new Holder();
            holder.textView = (TextView) view.findViewById(R.id.textView);
            holder.checkBox = (CheckBox) view.findViewById(R.id.checkbox);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        holder.checkBox.setOnCheckedChangeListener(new CheckedChangeListener());
        holder.checkBox.setTag(position);//important line
        holder.textView.setText(list.get(position).getName());
        holder.checkBox.setChecked(list.get(position).isChecked());
        return view;
    }
    private class CheckedChangeListener implements CompoundButton.OnCheckedChangeListener {
        public CheckedChangeListener() {
        }
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            int position=(int) compoundButton.getTag();//important line
            list.get(position).setChecked(b);//important line
        }
    }
    public static class Holder {
        TextView textView;
        CheckBox checkBox;
    }
}

快乐的编码;)

他们可以是 no. 此问题的原因。

您的

适配器不知道您的列表中有多少种类型的视图。为此,您可以在适配器中包含这两个功能。

@Override public int getViewTypeCount() { return 1; }
@Override
public int getItemViewType(int position) {
    return position;
} 

其他原因可能是您正在更新列表中的某些内容,但没有在从中获取它的数据库(模型(中更新它。滚动时,列表视图将丢失有关从屏幕滚动的项目的信息。它将从您提供给它的数据库中再次加载它们。因此,该数据库应该具有更新的信息。查看此链接了解详细信息。

不要在适配器中使用 else 到所有 ifs:您在适配器中使用的尽可能多的 if 条件始终提供 else,以便适配器知道该怎么做。这有时是适配器项目混乱的原因。

不使用视图持有人模式:这也是此问题的一个众所周知的原因。您应该使用视图持有人模式,以确保适配器不会一次又一次地绘制布局。

这是因为ListView的项目视图的重用机制。您可以更改Data的源代码,就像答案之一所说的那样,或者您可以向适配器添加一个List字段以保存每个项目的检查状态并在显示相关项目时恢复。

在适配器中覆盖以下两个方法:

    @Override
    public int getViewTypeCount() {
        return getCount();
    }
    @Override
    public int getItemViewType(int position) {
        return position;
    }

最新更新