android listView中的MultiSelect不起作用

时间:2012-12-20 14:44:41

标签: android listview multi-select

我正在尝试使用自定义适配器和多选项创建listView。 我的适配器看起来像这样:

public class MusicPopupListAdapter extends BaseAdapter {

Cursor cursor;
Context context;
public MusicPopupListAdapter(Context context) {
    this.context = context;
    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String[] cursor_cols = {
            MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.TITLE,
    };
    String where = MediaStore.Audio.Media.IS_MUSIC + "=1";
    cursor = context.getContentResolver().query(uri, cursor_cols, where, null, null);
}


@Override
public int getCount() {
    return cursor.getCount();
}

@Override
public Track getItem(int position) {
    cursor.moveToPosition(position);
    Track track = new Track();
    track.id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
    track.album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
    track.artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
    track.title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
    return track;
}

@Override
public long getItemId(int position) {
    Track track = getItem(position);
    return track.id;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    RowHandler handler;
    if (convertView == null) 
    {
        convertView = LayoutInflater.from(context).inflate(R.layout.row, parent, false);
        handler = new RowHandler();
        handler.artist = (TextView) convertView.findViewById(R.id.artist);
        handler.album = (TextView) convertView.findViewById(R.id.album);
        handler.title = (TextView) convertView.findViewById(R.id.title);
        convertView.setTag(handler);
    }
    else
        handler = (RowHandler)convertView.getTag();
    Track track = getItem(position);
    handler.artist.setText(track.artist);
    handler.album.setText(track.album);
    handler.title.setText(track.title);
    return convertView;
}

public static class Track {
    long id;
    String artist;
    String album;
    String title;
}

public static class RowHandler {
    TextView artist;
    TextView album;
    TextView title;
}

作为行的布局,我使用扩展的相对布局:

public class CheckableRelativeLayout extends RelativeLayout implements Checkable {

boolean checked = false;
public CheckableRelativeLayout(Context context) {
    super(context);
}

public CheckableRelativeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CheckableRelativeLayout(Context context, AttributeSet attrs, int style)
{
    super(context, attrs, style);
}

@Override
public boolean isChecked() {
    return checked;
}

@Override
public void setChecked(boolean checked) {
    this.checked = checked;
}

@Override
public void toggle() {
    checked=!checked;
}

在调试中我看到,setChecked正在调用,但总是带有false值。

我将listView的选择模式设置为ListView.CHOICE_MODE_MULTIPLE。 我错过了什么?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。问题在于弹出窗口。默认情况下,弹出窗口是在非焦点模式下创建的。解决方案是设置弹出窗口focusable = true。