我有一些复选框的列表视图,我需要根据存储在数组中的一些数据自动检查它们。扩展底座适配器我的自定义适配器:getChildAt的Nullpointerexception
public class MyAdapter extends BaseAdapter
{
private Context context;
public SPCMjereAdapter(Context c)
{
context = c;
}
public int getCount() {
return MyArrList.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public int getlistItemsCount()
{
return listView.getChildCount();
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_list_row, null);
}
// ColID
TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis);
txtOpis.setText(MyArrList.get(position).get("OpisMjere") +".");
// ColCode
TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode);
txtRbMjere.setText(MyArrList.get(position).get("RbMjere"));
// ColChk
CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);
Chk.setTag(MyArrList.get(position).get("RbMjere"));
return convertView;
}
}
这也是我如何检查项目
int k=0;
int j=0;
for (j=0; j<numberOfItems; j++)
{
LinearLayout itemLayout = (LinearLayout)listView.getChildAt(j); // Find by under LinearLayout
CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.ColChk);
for (k=0; k<rbmjere.size(); k++)
{
if (checkbox.getTag().toString() == rbmjere.get(k).toString())
{
checkbox.setChecked(true);
}
}
}
的问题是线
LinearLayout itemLayout = (LinearLayout)listView.getChildAt(j);
所以,如果我把这个代码检查项目的问题是,列表视图显示例如3项,但代码只识别2项和第三项失踪。如何检测所有项目何时可见或如何检测列表视图的呈现完成时?
我建议你在for循环中使用'int count = listView.getChildCount();'然后'for(j = 0; j
user370305
看看你的'for'循环 - 它只循环两次 - 对于0和1(它<2,所以不包括2):) – g00dy
但是在哪里放?我想在listview加载所有数据后检查项目。我不确定我的逻辑是否正确... – Josef