NullPointerException异常的getChildAt [英] Nullpointerexception on getChildAt
问题描述
我有一些复选框列表视图,我需要根据存储在阵列的一些数据会自动检查。扩展底座适配器我的自定义适配器:
I have a listview with some check boxes and i need to check them automatically according to some data that are stored in array. My custom adapter that extends base adapter:
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;
}
}
和我这是怎么检查的项目
And this is how i check the items
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);
所以,如果我把这个code检查项目的问题是,列表视图显示例如3个项目,但code只承认2项和第三项缺失。如何当所有项目都可见或如何在列表视图渲染完成检测检测?
So if I call this code for checking items the problem is that listview show for example 3 items but the code recognize only 2 items and third item is missing. How to detect when all items are visible or how to detect when rendering of listview is finished?
推荐答案
问题就解决了。内部getView()我写这几行和它的作品只是我想:
Problem is solved. Inside getView() i wrote these few lines and it works just I wanted:
for (k=0; k<rbmjere.size(); k++)
{
if (Chk.getTag().toString().equals(rbmjere.get(k).toString()))
{
Chk.setChecked(true);
}
}
感谢所有的帮助和想法。
Thanks to all on help and ideas.
这篇关于NullPointerException异常的getChildAt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!