在多选ListView中不检查列表项

时间:2014-02-21 12:02:03

标签: android listview checkbox android-listview multiple-choice

我有以下设置:

Activity:

public class UploadActivity extends AbstractListActivity {

    protected PackageAdapter mAdapter = new PackageAdapter();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        setListAdapter(mAdapter);
    }
}

Adapter:

public class PackageAdapter extends ArrayAdapter<PointPackage> {

    ...

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        TextView view;
        if (convertView == null) {
            view = (TextView) mInflater.inflate(R.layout.list_item_package, parent, false);
        } else {
            view = (TextView) convertView;
        }

        bind(getItem(position), view);

        return view;
    }

    private void bind(PointPackage item, TextView view) {
        view.setText(item.getName());
    }
}

activity_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

list_item_package.xml:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:drawableLeft="@drawable/collection"
    android:drawablePadding="@dimen/compactPadding"
    android:gravity="center_vertical"
    android:textAppearance="?android:attr/textAppearanceListItem" />

我的问题是单击时没有选择列表项...行似乎是可点击的,单击时会突出显示,但不会显示复选标记。 我做错了什么?

修改 这段代码很好。问题出在其他地方。

1 个答案:

答案 0 :(得分:1)

您需要实现onItemClick侦听器,然后您可以获取被单击的视图/项目。从该视图中获取CheckedTextView并将其标记为已选中。