滚动列表视图会使我的视图检查倍增

时间:2014-03-28 15:15:19

标签: android listview android-listview scrollview checkedtextview

我是初学者,所以请容易。我有两个listview并行,这里只有listViewSelectFile他可以进行检查。问题是我从这个列表视图中检查了一个项目!示例:如果我从列表视图中选择第一个项目,则下一个项目直到我移动列表视图中的滚动未选中。如果在一个页面上他们可以是格言9项目(第一项是下一次检查8未选中)并检查10项目然后未选中下一项8项目然后检查项目19到目前为止一项。我测试并且手机是水平的,这也是同样的问题。我认为问题出现的是当我移动滚动下一个项目(CheckTextView ID)时,他给出了id 0,并且他乘以我的CheckTextView ID。我怎么解决这个问题?任何例子?谢谢 !!!!!如果你没有遇到这个问题,也许你可以给我一个例子,说明如何用textviewcheck制作2个listview参数。

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

切换功能:

public void toggle(CheckedTextView v) {
    if (v.isChecked()) {
        v.setChecked(false);
    } else {
        v.setChecked(true);
    }
}

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个并行列表视图:

  <?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>

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>

1 个答案:

答案 0 :(得分:0)

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

然后,如果我旋转屏幕,我向适配器询问我onSaveInstanceState中已检查项目的数组,并在onRestoreInstanceState方法中重复使用。

希望它有所帮助。