0

我有一个带有一些复选框的列表视图,我需要根据存储在数组中的一些数据自动检查它们。我的扩展基本适配器的自定义适配器:

 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);

因此,如果我调用此代码来检查项目,问题是 listview 显示例如 3 个项目,但代码仅识别 2 个项目,并且缺少第三个项目。如何检测所有项目何时可见或如何检测列表视图的渲染何时完成?

4

3 回答 3

0

尝试使用阵列适配器而不是基本适配器或类似的东西:

public class ArrayListAdapter<T> extends ArrayAdapter<T> {



     private List<T> arrayList;

        public ArrayListAdapter(Context context, int layout, List<T> arrayList) {
            super(context, layout);
            this.arrayList = arrayList;
        }

        @Override
        public T getItem(int position) {
            return arrayList.get(position);
        }

        @Override
        public int getCount() {
            return arrayList.size();
        }
    }

创建适配器实例并设置 listView.setAdapter(arrayAdapter); 我看到你没有提供 List 来调整它为什么你得到 nullPointerException

于 2013-07-29T09:18:52.260 回答
0

1.可能适配器还没有设置listview

Handler handler = new Handler();        
    handler.postDelayed(new Runnable() {            
        public void run() {
              //your code
              CheckBox checkbox = (CheckBox)listView.getChildAt(j).findViewById(R.id.ColChk);
              //your code
        }
    }, 500);

2.可能你滚动listview所以索引不对,参考这个 Android ListView y position

于 2013-07-29T09:38:09.213 回答
0

问题解决了。在 getView() 里面我写了这几行,它只是我想要的:

for (k=0; k<rbmjere.size(); k++)
            {
                if (Chk.getTag().toString().equals(rbmjere.get(k).toString()))
                {
                    Chk.setChecked(true);

                }
            }   

感谢所有人的帮助和想法。

于 2013-07-29T11:21:13.637 回答