【问题标题】:ListView multiCheckListView 多重检查
【发布时间】:2014-01-28 11:53:45
【问题描述】:

一直在尝试不同类型的列表视图。我现在可以在列表中显示它,实现搜索过滤器。但是,选中复选框将选中多个复选框,这不是我想要的。因此,我面临着以下问题。

  1. 如何修改它,使其只检查我想要的多选行?

  2. 我将把这段代码转移到一个片段中,所以每次我想更改“职员”时如何刷新“列表视图”,这将再次从数据库中检索查询并重新填充列表。我是否能够从活动中调用该方法或位于片段中的任何内容来触发刷新?

  3. 检索为选中复选框的 ID 存储的 ID。

非常感谢!

活动 - 将很快将其移动到片段。

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_encounter);
        encouterlist = new ArrayList<HashMap<String, String>>();
        myContext = this;
        encounterDB = new eAndPDbAdapter(myContext);
        encounterDB.open();
        Cursor mCursor = encounterDB.retrieveAllEncounterCursor();
        int count = mCursor.getCount();

        displaySharedPreferences();
        if ((mCursor != null) && (mCursor.getCount() > 0)) {
            mCursor.moveToFirst();

            do {
                // Get the data
                // Convert the initial to String
                if (clerkShip.equals(mCursor.getString(2)))
                {
                String eID = mCursor.getString(1);
                String encounter_name = mCursor.getString(3);
                String encounter_type = mCursor.getString(4);

            //  ename = (TextView) findViewById(R.id.eid);
            //  this.ename.setTextColor(Color.RED);
                //indicate that its successful
                Log.i("Successful retrival of", encounter_name);

                HashMap<String, String> map = new HashMap<String, String>();

                map.put(TAG_E_ID, eID);
                map.put(TAG_E_NAME, encounter_name);
                map.put(TAG_E_TYPE, encounter_type);

                // Add it to the array such that i can handle the array
                // afterwhich
                encouterlist.add(map);
            }
            }
            // move to the next row
                        while (mCursor.moveToNext());

        }

        filterText = (EditText) findViewById(R.id.encountersSearch);
        filterText.addTextChangedListener(filterTextWatcher);
        // need to link the layout
        encounterList = (ListView) findViewById(R.id.encounterslist);

        cBox = (CheckBox) findViewById(R.id.listCheck);
        encounterList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
            ((CheckBox)parent.getChildAt(position).findViewById(R.id.listCheck)).setChecked(true);

            }
        }
        );
this.adapter = new SimpleAdapter(EncounterActivity.this,
        encouterlist, R.layout.list_v, new String[] { TAG_E_ID,
                 TAG_E_NAME, TAG_E_TYPE,
                TAG_E_REQUIRED_ATTEMPTS }, new int[] { R.id.eid,
                R.id.ename,
                R.id.etype });

this.encounterList.setAdapter(adapter);


};


private TextWatcher filterTextWatcher = new TextWatcher() {

    public void afterTextChanged(Editable s) {
    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        ((Filterable) adapter).getFilter().filter(s);
    }

};

public void displaySharedPreferences() {
    SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(EncounterActivity.this);

    String listPrefs = prefs.getString("listpref", "No Clerkship Selected");

    StringBuilder builder2 = new StringBuilder();
    builder2.append(listPrefs);

clerkShip = builder2.toString();

}

Activity_Encounter.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical">

    <EditText 
     android:id="@+id/encountersSearch"
     android:hint="Enter encounter here"
    android:layout_width="fill_parent" 
    android:windowSoftInputMode="stateHidden"
    android:imeOptions="actionSearch"
                android:singleLine="true"
    android:layout_height="wrap_content"/>

    <ListView
        android:id="@+id/encounterslist"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />


</LinearLayout>

list_v.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/eid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone" />


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <CheckBox
        android:id="@+id/listCheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:clickable="false"
        android:focusable="false" >
    </CheckBox>

    <TextView
        android:id="@+id/ename"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left" />
</LinearLayout>

<TextView
    android:id="@+id/etype"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone" />

<TextView
    android:id="@+id/erequiredattempts"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone" />

    </LinearLayout>

【问题讨论】:

标签: android android-layout listview android-listview android-fragments


【解决方案1】:

您可以将检查状态保存在数组中,并在 getview 中根据位置设置检查/未检查状态。假设有 10 个项目,有一个包含 10 个项目的 bool 数组,它存储检查状态与否。然后在 getView 中,使用位置变量检查这个数组并设置 UI 状态

【讨论】:

  • 我遇到了 listview 回收视图问题。因此,如果我检查项目 1 并向下滚动。项目 17 和 33 也将被检查。
  • 这就是我的意思,如果你有 100 个项目,你需要有 100 个项目的布尔数组,arr[0] = true(选中),在你的 getView 中做 if(arr[position] {/ / 设置复选框选中} // else { //uncheck}),位置来自 getView 参数。我认为您每次选中复选框时都会有一个单击侦听器,将该特定实例设置为 true
  • @Walalala 你可以使用SparseBooleanArray。查看 Romain Guy 的解决方案@groups.google.com/forum/?fromgroups#!topic/android-developers/…
  • 再次与我发布的内容相同
  • @PulkitSethi 你最好用代码发布解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-19
  • 2015-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-12
相关资源
最近更新 更多