exact position not getting fetched on random checkbox selection in listview,shows indexoutofboundsexception

+2 votes
asked Sep 13, 2017 by user8601021

I have a listview with a textview and a checkbox which gets populated from an arraylist getting data from web service.It works fine.Now when I check on a particular checkbox position in the list is getting fetched if I check linearly from top to bottom.However if I check any checkbox randomly in the list the exact position of the checkbox is not fetched and it shows indexoutofbounds exception.I have tried both array adapter and base adapter.please help me.

Base Adapter

public NewsAdapter(Context context, List<FtpTable> ftpTables) { //Common.selectedFtpTable=null; this.mContext= context; this.ftpTables = ftpTables; inflater = LayoutInflater.from(mContext);
}
public class ViewHolder { TextView name_agency; CheckBox cb_agency; //TextView ime; //TextView telefon1; //TextView telefon2;
}
@Override
public int getCount() { return ftpTables.size();
}
@Override
public FtpTable getItem(int position) { return ftpTables.get(position);
}
@Override
public long getItemId(int position) { return position;
}
public View getView(final int position, View view, ViewGroup parent) { final ViewHolder holder; if (view == null) { holder = new ViewHolder(); view = inflater.inflate(R.layout.news_agencies_row, parent, false); holder.name_agency = (TextView) view.findViewById(R.id.name_agency); holder.cb_agency=(CheckBox)view.findViewById(R.id.chk_agency); view.setTag(holder); holder.cb_agency.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (holder.cb_agency.isChecked()) { Toast.makeText(mContext,"You have selected:"+ftpTables.get(position).getNewsAgency("newsAgency"),Toast.LENGTH_SHORT).show(); Common.selectedNewsAgencyArray.add(ftpTables.get(position).getNewsAgency("newsAgency")); //Common.selectedFtpTable.add(ftpTables.get(position)); //Toast.makeText(mContext,Common.selectedFtpTable.size(),Toast.LENGTH_SHORT).show(); Common.selectedHostArray.add(ftpTables.get(position).getHostFtp("hostFtp")); Toast.makeText(mContext,"SIZE OF HOST ARRAY:"+Common.selectedHostArray.size(),Toast.LENGTH_SHORT).show(); // Toast.makeText(mContext,"You have added:"+Common.selectedHostArray.get(position),Toast.LENGTH_SHORT).show(); Common.selectedUsernameArray.add(ftpTables.get(position).getUsernameFtp("usernameFtp")); Toast.makeText(mContext,"SIZE OF USERNAME ARRAY:"+Common.selectedUsernameArray.size(),Toast.LENGTH_SHORT).show(); //Toast.makeText(mContext,"You have added:"+Common.selectedUsernameArray.get(position),Toast.LENGTH_SHORT).show(); Common.selectedPasswordArray.add(ftpTables.get(position).getPasswordFtp("passwordFtp")); Toast.makeText(mContext,"SIZE OF PASSWORD ARRAY:"+Common.selectedPasswordArray.size(),Toast.LENGTH_SHORT).show(); Toast.makeText(mContext,"You have added:"+Common.selectedHostArray.get(position)+","+Common.selectedUsernameArray.get(position)+","+Common.selectedPasswordArray.get(position),Toast.LENGTH_SHORT).show(); } else { Toast.makeText(mContext,"You have unselected",Toast.LENGTH_SHORT).show(); /*indexToRemove = Common.selectedHostArray.indexOf(ftpTables.get(position).getHostFtp("hostFtp")); Common.selectedHostArray.remove(indexToRemove); Common.selectedNewsAgencyArray.remove(indexToRemove); Common.selectedUsernameArray.remove(indexToRemove); Common.selectedPasswordArray.remove(indexToRemove); Toast.makeText(mContext,"SIZE OF HOST ARRAY:"+Common.selectedHostArray.size(),Toast.LENGTH_SHORT).show(); Toast.makeText(mContext,"SIZE OF USERNAME ARRAY:"+Common.selectedUsernameArray.size(),Toast.LENGTH_SHORT).show(); Toast.makeText(mContext,"SIZE OF PASSWORD ARRAY:"+Common.selectedPasswordArray.size(),Toast.LENGTH_SHORT).show(); Toast.makeText(mContext,"You have removed:"+Common.selectedHostArray.get(indexToRemove)+","+Common.selectedUsernameArray.get(indexToRemove)+","+Common.selectedPasswordArray.get(indexToRemove),Toast.LENGTH_SHORT).show();*/ } } }); } else { holder = (ViewHolder) view.getTag(); } holder.name_agency.setText(ftpTables.get(position).getNewsAgency("newsAgency")); //holder.telefon1.setText(taxiLists.get(position).getTelefon1("telefon1")); //holder.telefon2.setText(taxiLists.get(position).getTelefon2("telefon2")); return view;
}

Array Adapter

public class AgencyListAdapter extends ArrayAdapter<FtpTable> { public List<FtpTable> ftpTables = null; int indexToRemove=-1; Context mContext; int CheckValue; public AgencyListAdapter(Context context, ArrayList<FtpTable> tables,int CheckValue) { super(context, 0, tables); this.mContext= context; this.ftpTables = tables; this.CheckValue=CheckValue; } @Override public View getView(final int position, View convertView, ViewGroup parent) { // Get the data item for this position //Common.position=position; FtpTable ftpTable = getItem(position); //convertView.setTag(ftpTable); // Check if an existing view is being reused, otherwise inflate the view //if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(R.layout.news_agencies_row, parent, false); final TextView name_agency = (TextView) convertView.findViewById(R.id.name_agency); final CheckBox cb_agency = (CheckBox) convertView.findViewById(R.id.chk_agency); convertView.setTag(ftpTable); cb_agency.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (cb_agency.isChecked()) { /*if (CheckValue == 0) { CheckValue = 1; Toast.makeText(mContext,String.valueOf(CheckValue),Toast.LENGTH_SHORT).show();*/ //cb_agency.setChecked(true); Toast.makeText(mContext, "You have selected:" + ftpTables.get(position).getNewsAgency("newsAgency"), Toast.LENGTH_SHORT).show(); Common.selectedNewsAgencyArray.add(ftpTables.get(position).getNewsAgency("newsAgency")); //Common.selectedFtpTable.add(ftpTables.get(position)); //Toast.makeText(mContext,Common.selectedFtpTable.size(),Toast.LENGTH_SHORT).show(); Common.selectedHostArray.add(ftpTables.get(position).getHostFtp("hostFtp")); Toast.makeText(mContext, "SIZE OF HOST ARRAY:" + Common.selectedHostArray.size(), Toast.LENGTH_SHORT).show(); // Toast.makeText(mContext,"You have added:"+Common.selectedHostArray.get(position),Toast.LENGTH_SHORT).show(); Common.selectedUsernameArray.add(ftpTables.get(position).getUsernameFtp("usernameFtp")); Toast.makeText(mContext, "SIZE OF USERNAME ARRAY:" + Common.selectedUsernameArray.size(), Toast.LENGTH_SHORT).show(); //Toast.makeText(mContext,"You have added:"+Common.selectedUsernameArray.get(position),Toast.LENGTH_SHORT).show(); Common.selectedPasswordArray.add(ftpTables.get(position).getPasswordFtp("passwordFtp")); Toast.makeText(mContext, "SIZE OF PASSWORD ARRAY:" + Common.selectedPasswordArray.size(), Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, "You have added:" + Common.selectedHostArray.get(position) + "," + Common.selectedUsernameArray.get(position) + "," + Common.selectedPasswordArray.get(position), Toast.LENGTH_SHORT).show(); //CheckValue=0; } else { //cb_agency.setChecked(false); //Toast.makeText(mContext, String.valueOf(CheckValue), Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, "You have unselected", Toast.LENGTH_SHORT).show(); //Toast.makeText(mContext,CheckValue,Toast.LENGTH_SHORT).show(); /*indexToRemove = Common.selectedHostArray.indexOf(ftpTables.get(position).getHostFtp("hostFtp")); Common.selectedHostArray.remove(indexToRemove); Common.selectedNewsAgencyArray.remove(indexToRemove); Common.selectedUsernameArray.remove(indexToRemove); Common.selectedPasswordArray.remove(indexToRemove); Toast.makeText(mContext,"SIZE OF HOST ARRAY:"+Common.selectedHostArray.size(),Toast.LENGTH_SHORT).show(); Toast.makeText(mContext,"SIZE OF USERNAME ARRAY:"+Common.selectedUsernameArray.size(),Toast.LENGTH_SHORT).show(); Toast.makeText(mContext,"SIZE OF PASSWORD ARRAY:"+Common.selectedPasswordArray.size(),Toast.LENGTH_SHORT).show(); Toast.makeText(mContext,"You 

1 Answer

0 votes
answered Mar 14, 2018 by divy-soni

Don't make position variable final, get the object of FtpTable by using tag cb_agency.setTag(ftpTable); then fetch the object in setOnCheckedChangeListener by using compoundButton.getTag().

cb_agency.setTag(ftpTable);
cb_agency.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { FtpTable ftp = (FtpTable) compoundButton.getTag(); //now you don't need position variable here. Toast.makeText(mContext, "You have selected:" + ftp.getNewsAgency("newsAgency"), Toast.LENGTH_SHORT).show(); }
}
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
...