我有一些複選框的列表視圖,我需要根據存儲在數組中的一些數據自動檢查它們。擴展底座適配器我的自定義適配器: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