多次列出视图重复值并在第 6 个值之后重复选择复选框

list view repeating values several times and repeating check box selection every after 6th value

我正在使用自定义列表视图基础适配器。在将值传递给适配器时,它的重复值。在 viewholder 中,我使用了一个复选框,而 select 该复选框列表自动 select 此后的每 6 个复选框。

这是我的适配器的完整代码。

public class CallLogAdapter extends BaseAdapter {

private Activity activity;
private LayoutInflater li;
List<CallLogInfo> callData;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
Context context;
static Boolean checkboxstate[];
ArrayList<MultipleSelectedContact> mainDataList;
int i = 0;

public CallLogAdapter(Activity activity, List<CallLogInfo> callData, ArrayList<MultipleSelectedContact> selectedContacts) {
    this.activity = activity;
    this.callData = callData;
    this.mainDataList = selectedContacts;
    context = activity;
    checkboxstate = new Boolean[callData.size()];
}

// View lookup cache
private static class ViewHolder {
    TextView phoneNo, date, addComment, duration;
    CheckBox checkBox;
    CardView card;
    ImageView callTypeImage;
    int count;
}


@Override
public int getCount() {
    if (callData != null && callData.size() != 0) {
        return callData.size();
    }
    return 0;
}

@Override
public Object getItem(int position) {
    return callData.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {


    View v = convertView;
    final ViewHolder viewHolder;  // view lookup cache stored in tag

    if (v == null) {
        li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.single_card, parent, false);
        viewHolder = new ViewHolder();

        viewHolder.card = (CardView) v.findViewById(R.id.card_view);
        viewHolder.callTypeImage = (ImageView) v.findViewById(R.id.callTypeImage);
        viewHolder.phoneNo = (TextView) v.findViewById(R.id.phoneNoText);
        viewHolder.date = (TextView) v.findViewById(R.id.dateText);
        viewHolder.duration = (TextView) v.findViewById(R.id.callDurationText);
        viewHolder.checkBox = (CheckBox) v.findViewById(R.id.checkBox);
        viewHolder.addComment = (TextView) v.findViewById(R.id.addCommentText);

        v.setTag(viewHolder);

    } else {
        viewHolder = (ViewHolder) v.getTag();
    }

    viewHolder.count = position;

    final CallLogInfo Info;
    Info = callData.get(position);
    switch (Info.callType) {
        case "Outgoing":
            viewHolder.callTypeImage.setImageResource(R.mipmap.up_arrow);
            break;
        case "Incoming":
            viewHolder.callTypeImage.setImageResource(R.mipmap.down_arrow);
            break;
        case "Missed":
            viewHolder.callTypeImage.setImageResource(R.mipmap.miss_arrow);
            break;
    }
    viewHolder.phoneNo.setText(Info.phoneNo);
    viewHolder.date.setText(Info.date);
    viewHolder.duration.setText(Info.duration);
    viewHolder.addComment.setTag(viewHolder.count);
    viewHolder.checkBox.setTag(viewHolder.count);


    if (checkboxstate[((int) viewHolder.checkBox.getTag())] == null) {
        checkboxstate[((int) viewHolder.checkBox.getTag())] = false;
    }
    viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
            if (((CheckBox) view).isChecked()) {
                checkboxstate[((int) viewHolder.checkBox.getTag())] = true;
                mainDataList.add(i, new MultipleSelectedContact());
                mainDataList.get(i).phoneNoS = Info.phoneNo;
                mainDataList.get(i).setIsSelected(viewHolder.checkBox.isSelected());
                map.put(((int) viewHolder.checkBox.getTag()), i);
                i++;
                view.setSelected(true);
            } else {
                checkboxstate[((int) viewHolder.checkBox.getTag())] = false;
                mainDataList.remove(map.get(((int) viewHolder.checkBox.getTag())));
                view.setSelected(false);
            }
        }
    });

    viewHolder.addComment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // custom dialog
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.activity_add_comment);
            dialog.setTitle("Add Comment Here..");
            // set the custom dialog components - text, image and button
            final EditText text = (EditText) dialog.findViewById(R.id.messageEditText);

            Button dialogButton = (Button) dialog.findViewById(R.id.messageAddButton);
            // if button is clicked, close the custom dialog
            dialogButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    String comment = text.getText().toString();

                    DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "CallLogDb", null);
                    SQLiteDatabase db = helper.getWritableDatabase();
                    DaoMaster daoMaster = new DaoMaster(db);
                    DaoSession session = daoMaster.newSession();
                    CallCommentsDetailDao callCommentDao = session.getCallCommentsDetailDao();

                    CallCommentsDetail commentInfo = new CallCommentsDetail();

                    commentInfo.setCommentId(position);
                    commentInfo.setComments(comment);
                    callCommentDao.insertOrReplace(commentInfo);

                    session.clear();
                    db.close();

                    dialog.dismiss();
                }
            });

            dialog.show();

        }
    });

    viewHolder.card.setTag(position);
    viewHolder.card.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, MessageContentActivity.class);
            intent.putExtra("callDetails", Info);
            context.startActivity(intent);
        }
    });

    return v;

}
}

在我使用 ((int) viewHolder.checkBox.getTag()) 的代码中。我也尝试使用位置。但仍然无法正常工作.. 谁能帮我找出我哪里出错了

您可以使用 SparseBooleanArray 来保存复选框的状态,而不是将其设置为标签,并且您不会在 getView() 方法中设置复选框状态,例如

viewholder.checkbox.setChecked(booleanArray.valueAt(position))

然后在 OnClick() 上切换状态,例如

booleanArray.put(position,!booleanArray.valueAt(position));
notifyDataSetChanged();

此外,如果列表行包含复选框或 buttons.Use Recyclerview 以获得更好的自定义和性能,listItemClick 将无法正常工作。 Sample Implementation of recyclerview

在 getView 上设置复选框状态

if (checkboxstate[((int) viewHolder.checkBox.getTag())] == null) {
    checkboxstate[((int) viewHolder.checkBox.getTag())] = false;
}
viewholder.checkbox.setChecked(checkboxstate[((int)viewHolder.checkBox.getTag())]);