在listview中的随机复选框选择中未获取的确切位置显示indexoutofboundsexception

时间:2017-09-13 04:08:24

标签: android listview checkbox indexoutofboundsexception android-checkbox

我有一个带有textview的listview和一个复选框,它从一个arraylist中填充从web服务获取数据。它运行正常。当我检查列表中的特定复选框位置时,如果我从顶部线性检查到底。但是,如果我在列表中随机选中任何复选框,则不会获取复选框的确切位置,并显示 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;
        }
}
}

1 个答案:

答案 0 :(得分:0)

不要使位置变量最终,使用标记[] 获取FtpTable的对象,然后使用cb_agency.setTag(ftpTable);setOnCheckedChangeListener中获取对象。

compoundButton.getTag()