【问题标题】:Having checkbox in listview,scrolling loses selected checkbox and random checkbox get checked在列表视图中有复选框,滚动会丢失选中的复选框和随机复选框被选中
【发布时间】:2016-06-23 12:51:53
【问题描述】:

在列表视图中有复选框,滚动会丢失选中的复选框,随机复选框会被选中 在列表视图中有复选框,滚动会丢失选中的复选框和 随机复选框被选中。 我正在滚动此列表视图选中的复选框未选中。 复选框失去了他们的状态

public class FriendList extends ArrayAdapter<FriendListResult> {
    private List<FriendListResult> list;
    LayoutInflater layoutInflater;
    public int resource;
    private Holder holder = null;

  //Constructor
  public FriendList(Context context, int resource, List<FriendListResult> objects) {
    super(context, resource, objects);
    this.list = objects;
    this.resource = resource;
      // Cache the LayoutInflate to avoid asking for a new one each time.
    this.layoutInflater = LayoutInflater.from(context);
    }

// getview method
@Override
public View getView(final int position, View view, ViewGroup parent) {
// check if view is null or not
// create new row view
    if (view == null) {
// inflating layout
        view = layoutInflater.inflate(resource, null);
// initializing  holder
        holder = new Holder();
        holder.txtTitle = (TextView) view.findViewById(R.id.tvContactNameMain);
     // checkbox defined
        holder.checkBox = (CheckBox) view.findViewById(R.id.chkboxFriendListItem);
        view.setTag(holder);
    } 
    else {
        holder = (Holder) view.getTag();
    }
    // set value from the list
    final FriendListResult friendListResult = list.get(position);
    // get and set name
    holder.txtTitle.setText(friendListResult.getNickname());

// checkbox changeListener
holder.checkBox.setOnCheckedChangeListener(new  >CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
//  Maximum three selection for checkbox
  {   
  if (checkArrayList.size() >= 3 && isChecked) {
        buttonView.setChecked(false); 
Toast.makeText(getContext(), "You have already chosen 3 friends",
        Toast.LENGTH_LONG).show();
    } else if (isChecked) {
       //  storing position id to arraylist
        checkArrayList.add(getItem(position).getId());
        notifyDataSetChanged();
      // userCheckCount to get the count
        userCheckCount++;
    } else if (!isChecked) {    
    //removing position from arraylist
        checkArrayList.remove(getItem(position).getId());
        notifyDataSetChanged();
        userCheckCount--;
    }    
  } 
 });
  return view;
}
 // holder class initializing widgets
    class Holder {   
    private TextView txtTitle;
    private CheckBox checkBox;
    }
}

【问题讨论】:

  • 我推荐使用RecyclerView,它提供了处理这个问题的默认机制

标签: android listview checkbox android-arrayadapter


【解决方案1】:

您可以查看此链接。 使用 POJO 类保存复选框的选中状态。 很简单,只需通过此链接即可。

http://android-pratap.blogspot.in/2015/01/recyclerview-with-checkbox-example.html

CheckBox in RecyclerView keeps on checking different items

【讨论】:

    猜你喜欢
    • 2016-01-19
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多