为什么onCheckedChangeListener就像这样?

时间:2017-06-11 04:31:49

标签: android listview android-arrayadapter oncheckedchanged

我是Android开发的新手,现在面临一个问题。我已经制作了一个列表视图,必须在块中显示一些标题和描述,每个块都有一个复选框。我想得到复选框的状态使用 onCheckedChangeListener(),但每当我滚动ListView时,状态就会丢失并且它会被取消选中,因为它是第一个。为什么会发生这种情况?为什么要使用 onClickListener()解决了这个问题?

适配器注

public class AdapterNote extends ArrayAdapter<StructNote> {
  public AdapterNote(ArrayList<StructNote> array) {
    super(G.context, R.layout.adapter_notes, array);

  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    StructNote item = getItem(position);
    if (convertView == null) {
      convertView = G.inflater.inflate(R.layout.adapter_notes, parent,);
      holder = new ViewHolder(convertView);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    holder.fill(item, position);
    return convertView;
  }


  private static class ViewHolder {
    public TextView txt_title;
    public TextView txt_description;
    public CheckBox chk_done;

    public ViewHolder(View view) {

      txt_title = (TextView) view.findViewById(R.id.txt_title);
      txt_description = (TextView) view.findViewById(R.id.txt_descriptoin);
      chk_done = (CheckBox) view.findViewById(R.id.chk_done);
    }


    public void fill(final StructNote item, final int a) {
      txt_title.setText(item.title);
      txt_description.setText(item.description);
      chk_done.setChecked(item.done);

      chk_done.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          CheckBox checkBox = (CheckBox) buttonView;
          item.done = checkBox.isChecked();
        }

      });
      }


  }
}

MainActiity

public class MainActivity extends AppCompatActivity {
  public ArrayList<StructNote> notes = new ArrayList<>();
  ListView lst_content;
  ArrayAdapter adapter;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lst_content = (ListView) findViewById(R.id.lst_content);
    for (int i = 0; i <= 100; i++) {
      StructNote note = new StructNote();
      note.title = "Title: " + i;
      note.description = "Description: " + i;
      note.done = false;
      notes.add(note);
    }
    adapter = new AdapterNote(notes);
    lst_content.setAdapter(adapter);
    adapter.notifyDataSetChanged();

  }
}

StuctNote

public class StructNote {
  public String title;
  public String description;
  public boolean done;
}

<强“G

public class G extends Application {
  public static Context context;
  public static LayoutInflater inflater;

  @Override
  public void onCreate() {
    super.onCreate();
    context = getApplicationContext();
    inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }
}

1 个答案:

答案 0 :(得分:0)

您的代码存在两个问题

  1. 当您更改数据时,您没有打电话
  2.   

    notifyDataSetChanged();

    **  2.在适配器中设置Checkbox值时,它调用 setOnCheckedChangeListener

    所以你可以这样做,从ViewHolder中删除填充函数,在getView中添加它也添加

      

    holder.chk_done.setOnCheckedChangeListener(空);   在设置检查值之前

    所以你的代码将在getView中,如下所示

    @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
           final StructNote item = getItem(position);
            if (convertView == null) {
                convertView = G.inflater.inflate(R.layout.adapter_notes, parent,false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.txt_title.setText(item.title);
            holder.txt_description.setText(item.description);
            holder.chk_done.setOnCheckedChangeListener(null);
            holder.chk_done.setChecked(item.done);
    
            holder.chk_done.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    item.done = isChecked;
                    notifyDataSetChanged();
                }
    
            });
    
            return convertView;
        }
    

    从viewholder中删除 fill()方法