2
我有一個列表視圖與一個TextView和一個複選框,其會從一個ArrayList從網絡service.It獲取數據填充工作fine.Now當我檢查在一個特定的如果我從上到下線性檢查,則列表中的複選框位置正在被抓取。 但是如果我在列表中選中任何複選框隨機複選框的確切位置並不牽強,它表明indexoutofbounds exception.I曾經嘗試都陣列適配器和底座adapter.please幫助我。確切位置,顯示IndexOutOfBoundsException異常
底座適配器
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;
}
}
}
我已經嘗試了兩個適配器底座適配器和陣列適配器和都顯示相同的exception.I已編輯與底座適配器code.as據,如果我不做出最後的關鍵字是最後的關鍵字有關的問題不會在onCheckChanged中獲取,我該怎麼做? – user8601021
我在回答中告訴過你,你可以使用標籤獲得物體,而不需要位置。 –
所以無論我正在使用onCheckChanged位置應該我cb_agency,getTag()編輯它?u能請編輯我的代碼,並張貼了我該怎麼辦呢? – user8601021