androidlistview-adapter

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


I am using a custom list view base adapter. while passing values to the adapter its repeating values. and in viewholder I am using a checkbox, while selecting that checkbox list auto select the every 6th checkbox after that.

here is my adapter full code.

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;

}
}

Here in the code where I am using ((int) viewHolder.checkBox.getTag()) . I tried using position also. but still its not working.. can anyone please help me to find out where I am going wrong


Solution

  • Set your check box state on getView

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