Android 滚动listview会使我的视图检查增加一倍

Android 滚动listview会使我的视图检查增加一倍,android,listview,android-listview,scrollview,checkedtextview,Android,Listview,Android Listview,Scrollview,Checkedtextview,我是个新手,别紧张。我有两个listview并行,这里只有listViewSelectFile他可以进行检查。问题是当我从这个列表视图中检查一个项目时!!!示例:如果我从listview中选择第一个项目,则在我从listview中移动滚动条之前的下一个项目将被取消选中。如果在一个页面上,他们最多可以看到9个项目,第一个项目是检查,第八个项目是检查,第十个项目是检查,第八个项目是检查,然后第八个项目是检查,然后第十九个项目是检查,到目前为止只有一个。我测试了一下,手机是水平的,这是同一个问题。我想

我是个新手,别紧张。我有两个listview并行,这里只有listViewSelectFile他可以进行检查。问题是当我从这个列表视图中检查一个项目时!!!示例:如果我从listview中选择第一个项目,则在我从listview中移动滚动条之前的下一个项目将被取消选中。如果在一个页面上,他们最多可以看到9个项目,第一个项目是检查,第八个项目是检查,第十个项目是检查,第八个项目是检查,然后第八个项目是检查,然后第十九个项目是检查,到目前为止只有一个。我测试了一下,手机是水平的,这是同一个问题。我想出现的问题是当我移动滚动下一个项目的sCheckTextView ID时,他给了ID 0,然后他乘以我的CheckTextView ID。我怎样才能解决这个问题?有什么例子吗?非常感谢。如果你不明白这个问题,也许你可以给我一个例子,如何使2个listview与textviewcheck并行

    PopulateListView();
bot的全部代码都在PopulateListView中:

adapterImageFileName = new ItemsAdapter(
                        screen3_select_from_lists.this, mImageFilenames);

                        listViewSelectFile.setAdapter(adapterImageFileName);

            listViewSelectFile
                    .setOnItemClickListener(new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> parent,
                                View view, int position, long id) {
                            // TODO Auto-generated method stub
                            // When clicked, show a toast with the TextView text
                            try {
                                LinearLayout item=(LinearLayout) view;
                                CheckedTextView tv = (CheckedTextView)item.findViewById(R.id.textView1);
                            toggle(tv);

                            } catch (Exception e) {
                                Toast.makeText(getApplicationContext(),
                                        e.getMessage(), Toast.LENGTH_LONG)
                                        .show();
                            }
                        }
                    });
MyAdapter:

private class ItemsAdapter extends BaseAdapter {

        String[] items;

        public ItemsAdapter(Context context, String[] mImageFilenames) {
            // TODO Auto-generated constructor stub
            this.items = mImageFilenames;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return items.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.list_view_rows, null);
            }
            CheckedTextView post = (CheckedTextView)v
                    .findViewById(R.id.textView1);
            post.setText(items[position]);
            return v;
        }

    }
和我的xml,2个并行列表视图:

list_view_rows.xml:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >



    <CheckedTextView  
      android:id="@+id/textView1" 
      android:paddingLeft="20dip" 
      android:paddingRight="20dip" 
      android:paddingTop="10dip"
      android:paddingBottom="10dip" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="?android:attr/listPreferredItemHeight"  
      android:gravity="center_vertical"  
      android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
      android:onClick="toggle" /> 

</LinearLayout>

您的问题是,为了优化资源,Android正在重用您的视图。我设法解决了这个问题,创建了自己的适配器,并将我检查过的ID保存在适配器中。每次调用我的getView方法时,我都会清除check对象,然后查看在我的checked行结构中,用户是否检查了该行/ID

然后,如果我旋转屏幕,我会向onSaveInstanceState中的已检查项数组的适配器发出请求,我会在onRestoreInstanceState方法中重用该数组


希望有帮助。

仍然不起作用,请从顶部检查代码,代码已更新。你能更正我的代码吗?非常感谢。正如我所看到的,您并没有更新listview索引。除此之外,您必须在“getview”方法中执行setchecke true或false,这取决于您提供给适配器的数组
  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false" >



        <ListView
            android:id="@+id/listviewSelectStudents"
            android:layout_width="0dip"
            android:layout_height="match_parent" 
            android:layout_weight="1"
            android:choiceMode="multipleChoice"
            >
        </ListView>


        <ListView
            android:id="@+id/listviewSelectFiles"
            android:layout_width="0dip"
            android:layout_height="match_parent" 
            android:layout_weight="1"
            android:choiceMode="multipleChoice">
        </ListView>


</LinearLayout>
  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >



    <CheckedTextView  
      android:id="@+id/textView1" 
      android:paddingLeft="20dip" 
      android:paddingRight="20dip" 
      android:paddingTop="10dip"
      android:paddingBottom="10dip" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="?android:attr/listPreferredItemHeight"  
      android:gravity="center_vertical"  
      android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
      android:onClick="toggle" /> 

</LinearLayout>