0

我是初学者,所以放轻松。我有两个 listview parallel ,这里只有 listViewSelectFile 他可以做检查。问题是当我从这个列表视图中检查一个项目时!示例:如果我从 listview 中选择第一项,则未选中下一项直到我从 listview 移动滚动。如果在一个页面上,它们最多可以看到 9 个项目(第一项是检查,接下来是 8 个未选中)并且第 10 个项目被选中,然后接下来的 8 个项目被取消选中,那么第 19 个项目被选中,到目前为止是一个。我测试了手机水平,这是同样的问题。我认为出现的问题是当我移动滚动下一项的(CheckTextView ID)时,他给出的 id 为 0,然后他乘以我的 CheckTextView ID。我该如何解决这个问题?有什么例子吗?谢谢 !!!!!如果你不

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

我的适配器:

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

1 回答 1

0

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

然后,如果我旋转屏幕,我会询问onSaveInstanceState我在我的onRestoreInstanceState方法中重用的一组检查项目的适配器。

希望能帮助到你。

于 2014-03-28T15:33:38.193 回答