mardi 12 septembre 2017

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

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.please help me.

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // Get the data item for this 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 have removed:"+Common.selectedHostArray.get(indexToRemove)+","+Common.selectedUsernameArray.get(indexToRemove)+","+Common.selectedPasswordArray.get(indexToRemove),Toast.LENGTH_SHORT).show();*/

                    }
                }
            });
            name_agency.setText(ftpTables.get(position).getNewsAgency("newsAgency"));
            //holder.telefon1.setText(taxiLists.get(position).getTelefon1("telefon1"));
            //holder.telefon2.setText(taxiLists.get(position).getTelefon2("telefon2"));
            // Lookup view for data population
            //TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
            //TextView tvHome = (TextView) convertView.findViewById(R.id.tvHome);
            // Populate the data into the template view using the data object
            //tvName.setText(user.name);
            //tvHome.setText(user.hometown);
            // Return the completed view to render on screen

            return convertView;
        }

Aucun commentaire:

Enregistrer un commentaire