2

I have the following setup:

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" />

My problem is that the list items are not getting selected when clicked... The row is seems to be clickable, it highlights when clicked, but the checkmark does not appears. What am i doing wrong?

EDIT: This code is just fine. The problem was elsewhere.

4

1 回答 1

1

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

于 2014-02-21T12:08:05.170 回答