简体   繁体   中英

Not able to get selected items from multi-list view when subclassed array adapter

I have a layout like this:

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

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="#b5b5b5"
    android:dividerHeight="1dp"
    android:cacheColorHint="#00000000"/>
</LinearLayout>

And the row layout like this:

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

    <TextView android:id="@+id/rowTextView"
        style="@style/HeaderTitleText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold"
        android:textSize="16sp" >
    </TextView>

    <CheckBox android:id="@+id/CheckBox01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_alignParentRight="true" android:layout_marginRight="6sp"
        android:focusable="false">
    </CheckBox>

</RelativeLayout>

In my activity I am able to show list values and am able to select and un-select them, however, I'm not able to get the ones that are selected.

Here is what I'm doing:

private ListView listView;
public class MyActivity extends BaseActivity {

  protected void onCreate (Bundle instance1) {
        listView = getListView();
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        new MyTask().execute(); //loads the list from server
  }

    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getTitle().toString().equalsIgnoreCase("save")) {
            int totalItems = 0;

            SparseBooleanArray checked = listView.getCheckedItemPositions();
            Log.d("checked size: ", checked.size()+"");
            for (int i = 0; i < checked.size(); i++) {
                if(checked.valueAt(i) == true) {
                    Tag tag = (Tag) listView.getItemAtPosition(checked.keyAt(i));
                    Log.i("xxxx", i + " " + tag);
                    totalItems++;
                }
            }

            Toast.makeText(this, "Got click: " + totalItems, Toast.LENGTH_SHORT).show();
        }
        return true;
    }
}

Total items is always zero and in the logs checked size is always 0 as well.

What am i doing wrong?

My Task does the following in onPostExecute()

listAdapter = new MyCustomAdapter(getApplicationContext(), myList);
listView.setAdapter(listAdapter);

Where do you have below lines of code in your onCreate method ?

super.onCreate(instance1);
setContentView(R.layout.yourlayout);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM