In my custom listview, I have a check box, couple of TextViews etc. I need to implement check event of this check box. If I used, setOnCheckedChangeListener of the check box, it fires when scrolling the listview. Then I used setOnClickListener, but still I some items are un-checked when I am scrolling the list. Someone has advised to do as below link, CheckBox gets unchecked on scroll in a custom listview
But there it is only considering the Check box as the listview item. Isn't it? I have multiple controls such as check box, text view, editview in my custom list view item. Could you please help?
public class DocumentRegisterAdapter extends BaseAdapter{
private LayoutInflater inflater;
private ArrayList<Document> objects;
public int selectedItem=-1;
Context context;
String x="";
String prev = "";
private final boolean[] mCheckedState;
public DocumentRegisterAdapter(Context context1,ArrayList<Document> objects) {
inflater = LayoutInflater.from(context1);
this.objects = objects;
context = context1;
mCheckedState = new boolean[objects.size()];
}
@Override
public int getCount() {
return objects.size();
}
public void setList(ArrayList<Document> objects){
this.objects=objects;
}
@Override
public Object getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null) {
//convertView = inflater.inflate(R parent, false);
convertView = inflater.inflate(R.layout.document_register_cell,parent,false);
}
final CheckBox chkCheckin = (CheckBox) convertView.findViewById(R.id.checkBox);
if (((DashboardActivity)context).isCalledCheckOut ||((DashboardActivity)context).isCalledCheckIn)
{
chkCheckin.setVisibility(View.VISIBLE);
if (GlobalVariables.getGsCurrentFragment() == Constants.documentRegistryFragment) {
if (chkCheckin.isChecked()) {
chkCheckin.setChecked(false);
}
}
//chkCheckin.setChecked(false);
}
else
{
chkCheckin.setVisibility(View.GONE);
}
chkCheckin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(chkCheckin.isChecked())
{
((DashboardActivity) context).checkInOutDocument = new Document(objects.get(position).documentNo, objects.get(position).documenttitle, objects.get(position).revision, objects.get(position).category, objects.get(position).discipline, objects.get(position).status, objects.get(position).type, objects.get(position).fileFormat, objects.get(position).checkOut, objects.get(position).intKey, objects.get(position).getReason(), "1", objects.get(position).isSelected());
((DashboardActivity) context).checkInOutDocList.add(((DashboardActivity) context).checkInOutDocument);
((DashboardActivity) context).pos.put(Integer.toString(position), Integer.toString(((DashboardActivity) context).checkInOutDocList.size() - 1));
}
else
{
Iterator itr = ((DashboardActivity) context).checkInOutDocList.iterator();
Document strElement = null;
while(itr.hasNext()) {
strElement = (Document) itr.next();
if (strElement.documentNo.equals(objects.get(position).documentNo)) {
itr.remove();
break;
}
}
}
}
});
TextView txtDocumentNo = (TextView) convertView.findViewById(R.id.lblDocumentNo);
TextView txtDocumentTitle = (TextView) convertView.findViewById(R.id.lblDocumentTitle);
TextView txtRevision = (TextView) convertView.findViewById(R.id.lblRevVal);
TextView txtCategory = (TextView) convertView.findViewById(R.id.lblCategoryVal);
TextView txtDiscipline = (TextView) convertView.findViewById(R.id.lblDisciplineVal);
TextView txtStatus = (TextView) convertView.findViewById(R.id.lblStatusVal);
TextView txttype = (TextView) convertView.findViewById(R.id.lblTitleVal);
TextView txtFileFormat = (TextView) convertView.findViewById(R.id.lblFileFormatVal);
ImageView imgCheckOut = (ImageView) convertView.findViewById(R.id.imgCheckout);
ImageButton imgBtnAttachment = (ImageButton) convertView.findViewById(R.id.imageButton);
txtDocumentNo.setText(objects.get(position).documentNo);
txtDocumentTitle.setText(objects.get(position).documenttitle);
txtRevision.setText(objects.get(position).revision);
txtCategory.setText(objects.get(position).category);
txtDiscipline.setText(objects.get(position).discipline);
txtStatus.setText(objects.get(position).status);
txttype.setText(objects.get(position).type);
txtFileFormat.setText(objects.get(position).fileFormat);
if (objects.get(position).checkOut == 0){
imgCheckOut.setVisibility(View.INVISIBLE);
//Anu 2016/03/03
if(((DashboardActivity)context).isCalledCheckOut)
chkCheckin.setVisibility(View.VISIBLE);
else if(((DashboardActivity)context).isCalledCheckIn)
chkCheckin.setVisibility(View.INVISIBLE);
}
else{
Drawable d = convertView.getResources().getDrawable(R.drawable.tb_checkout);
imgCheckOut.setImageDrawable(d);
imgCheckOut.setVisibility(View.VISIBLE);
if(((DashboardActivity)context).isCalledCheckOut)
chkCheckin.setVisibility(View.INVISIBLE);
else if(((DashboardActivity)context).isCalledCheckIn)
chkCheckin.setVisibility(View.VISIBLE);
}
if (objects.get(position).fileFormat.length() > 0) {
// Drawable d = convertView.getResources().getDrawable(R.drawable.tbattachment3); // imgBtnAttachment.setImageDrawable(d);
imgBtnAttachment.setVisibility(View.VISIBLE);
}
else
{
imgBtnAttachment.setVisibility(View.INVISIBLE);
}
imgBtnAttachment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// ((DashboardActivity) context).throughAttButton = true;
((DashboardActivity) context).attachmentState=0;
((DashboardActivity) context).currentAttachment = 0;
((DashboardActivity)context).transmittalAttViewModule = Constants.documentModule;
DocumentServices documentServices = new DocumentServices();
List<Map<String,String>> docAttachmentsList = new ArrayList<Map<String, String>>();
((DashboardActivity) context).transmittalDocIntKey = objects.get(position).intKey;
((DashboardActivity) context).currentlyDownloadingFileInfo.put("IntKey", objects.get(position).intKey + "");
((DashboardActivity)context).currentlyDownloadingFileInfo.put("Box", Constants.DeviceInfoBox.DOCUMENT.getValue()+"");
if(GlobalVariables.getGsDeviceAccessMode().equals(Constants.constOnline)) {
docAttachmentsList = documentServices.ListViewFilesForDownload(context, objects.get(position).intKey);
}else{
docAttachmentsList = documentServices.ListViewFilesForOfflineDownload(context, objects.get(position).intKey);
}
((DashboardActivity) context).revisedAttachmentList = new ArrayList<>();
if(docAttachmentsList.size()>0){
for (int k = 0; k < docAttachmentsList.size(); k++) {
((DashboardActivity) context).revisedAttachmentList.add(docAttachmentsList.get(k));
}
}
selectedItem = position;
((DashboardActivity) context).callTransmittalAttachment(0);
notifyDataSetChanged();
}
});
if(position%2 == 0)
{
String myHexColor = "#f8f2f2f2";
convertView.setBackgroundColor(Color.parseColor(myHexColor));
}
else
{
String myHexColor = "#ffffff";
convertView.setBackgroundColor(Color.parseColor(myHexColor));
}
if(selectedItem!=-1&&selectedItem==position){
convertView.setBackgroundColor(Color.parseColor("#969696"));
}
return convertView;
}
}