2012-12-20 41 views
0

我正在尝试使用自定义适配器和多选的listView。 我的适配器是这样的:多选在android 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呼吁,但始终以虚假的价值。

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

+0

您是否尝试过以下示例? http://theopentutorials.com/tutorials/android/listview/android-multiple-selection-listview/ – itsrajesh4uguys

+1

试试这个也..它有很好的例子http://dj-android.blogspot.in/2012/04/ milti-selection-listview-android-with.html – itsrajesh4uguys

+0

感谢您的帮助。我找到解决方案。 –

回答

0

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

相关问题