列表视图中的随机复选框选择未获取确切位置,显示索引超出范围异常

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

我有一个带有文本视图的列表视图和一个复选框,该复选框从一个从网络获取数据的数组列表中填充 service.It 工作 fine.Now 当我检查列表中的特定复选框位置时正在获取如果我从上到下线性检查。但是,如果我随机检查列表中的任何复选框,则不会获取复选框的确切位置并显示 indexoutofbounds exception.I 数组适配器和基都试过了 adapter.please 帮帮我。

基础适配器

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;
}

数组适配器

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 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;
        }
}
}

不要将位置变量设置为最终变量,使用标签 cb_agency.setTag(ftpTable); 获取 FtpTable 的对象,然后使用 compoundButton.getTag().

获取 setOnCheckedChangeListener 中的对象
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();
                   }
}