Android 遍历ListView时出现NullPointerException

Android 遍历ListView时出现NullPointerException,android,android-listview,Android,Android Listview,代码: 我得到了一个NullPointerException,因为getChildAt返回可见行,我还必须检查不可见行,不管它们是否被选中。那么如何在单独的按钮上检查它呢?LinearLayout ly=LinearLayout findviewbydr.id.lv 上面一行-它是否尝试采用根布局?如果是这样,就不能使用findViewById。而是使用: GetLayoutFlater.inflateR.layout.mylayout getLayoutInflater是Activity的方法

代码:

我得到了一个NullPointerException,因为getChildAt返回可见行,我还必须检查不可见行,不管它们是否被选中。那么如何在单独的按钮上检查它呢?

LinearLayout ly=LinearLayout findviewbydr.id.lv

上面一行-它是否尝试采用根布局?如果是这样,就不能使用findViewById。而是使用:

GetLayoutFlater.inflateR.layout.mylayout


getLayoutInflater是Activity的方法

我猜您希望从ListView中选中的行中获取TextView的文本。您不能使用getChildAt来解析所有的行,相反,您应该使用在适配器中传递的数据、游标并确定检查了哪些行。由于使用自定义行进行布局,因此必须使用自定义适配器以某种方式保持行复选框的选中/未选中状态。当需要获取文本时,只需单击该按钮,即可找到当前选中的行,并直接从光标中提取所需的文本

存储复选框状态的一种简单方法是创建一个ArrayList,您将根据您选择的复选框(可能效率不高)对其进行修改:

SimpleCursorAdapter ada = new SimpleCursorAdapter(this,
                R.layout.custom_layout, ssCursor, new String[] {
                        "String" }, new int[] {
                        R.id.txt2 });
        lvSms.setAdapter(ada); 

  btnDel.setOnClickListener(new View.OnClickListener() {

            private ArrayList<String> msgArr;

            public void onClick(View v) {

                LinearLayout ly = (LinearLayout) findViewById(R.id.lv);
                ListView lvMsg = (ListView) ly.getChildAt(3);
                int listCount = lvSms.getCount();
                for (int a = 0 ; a < listCount ; a++)
                {

                    LinearLayout ll = (LinearLayout) lvMsg.getChildAt(a);
                    LinearLayout l2 = (LinearLayout) ll.getChildAt(0);
                    LinearLayout l3 = (LinearLayout) l2.getChildAt(0);
                    CheckBox chkBox =(CheckBox) l3.getChildAt(0);
                    boolean valueOfChkBox = chkBox.isChecked();
                    if (valueOfChkBox) {
                        LinearLayout l4 = (LinearLayout) l2.getChildAt(1);
                        TextView txt1 = (TextView) l4.getChildAt(0);
                        msgArr = new ArrayList<String>();
                        msgArr.add(txt1.getText().toString());
                        Log.i("hello", txt1.getText().toString());
                    }

                    } });

然后解析从getItemsThatAreChecked获得的ArrayList,并从游标中提取数据。这里有一个小例子git://gist.github.com/2634525.git 使用布局

但如何遍历列表视图项?请在设置适配器的位置添加代码,或者如果是自定义适配器,请添加代码?@Luksprog我已根据您的请求添加了适配器代码。请帮忙…你想完成什么?一般来说,不是代码方面的。我只需要在单击listener时访问单独按钮中的列表视图项。因为我必须检查列表视图项值…@kamil这不是方法。当您上下滚动ListView时,列表行中的复选框是否显示为随机选中/未选中?
private class CustomAdapter extends SimpleCursorAdapter {
            // this will hold the status of the CheckBoxes
        private ArrayList<Boolean> checkItems = new ArrayList<Boolean>();

        public CustomAdapter(Context context, int layout, Cursor c,
                String[] from, int[] to) {
            super(context, layout, c, from, to);
            int count = c.getCount();
            for (int i = 0; i < count; i++) {
                checkItems.add(false);
            }

        }

            //this method returns the current status of the CheckBoxes
            //use this to see what CheckBoxes are currently checked
        public ArrayList<Boolean> getItemsThatAreChecked() {
            return checkItems;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            super.bindView(view, context, cursor);
            int position = cursor.getPosition();
            CheckBox ckb = (CheckBox) view.findViewById(R.id.checkBox);
            ckb.setTag(new Integer(position));
            ckb.setChecked(checkItems.get(position));
            ckb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    Integer realPosition = (Integer) buttonView.getTag();
                    checkItems.set(realPosition, isChecked);
                }

            });
        }

    }