我正在开发一个Android媒体播放器。我是ANDROID的新手。我使用以下代码显示查询结果中的所有音乐。我想要每首歌一次,但如果我在适配器视图中设置文本大小,它会不止一次地显示一首歌而忽略许多其他歌曲,如下面的代码所示。如何在适配器视图中设置文本大小并避免此问题?
谢谢你
public class MusicAdapter extends BaseAdapter {
private Context mContext;
public MusicAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
TextView tv = new TextView(mContext.getApplicationContext());
tv.setTextSize(25); //THE PROBLEM IS HERE
String id = null;
if (convertView == null) {
music_column_index = musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
musicCursor.moveToPosition(position);
id = musicCursor.getString(music_column_index);
music_column_index = musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
musicCursor.moveToPosition(position);
String artist = musicCursor.getString(music_column_index);
id += "Artist:" + artist;
tv.setText(id);
} else
tv = (TextView) convertView;
return tv;
}
}
答案 0 :(得分:0)
可能是因为你在try {}语句中写了相同的代码并且在while内? ))
我的意思是这些界限:
path.add(musicCursor.getString(musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
title.add(musicCursor.getString(musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)));
artist.add(musicCursor.getString(musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)));
album.add(musicCursor.getString(musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM)));
year.add(musicCursor.getString(musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.YEAR)));
答案 1 :(得分:0)
我已经使用如下的SimpleCursorAdapter解决了这个问题。它是一个内置的适配器,非常聪明。 This link helped me a lot
你永远不应该设置listview的android:layout_height =“wrap_content”。它会导致重复出现列表项。
ListAdapter adapter = new SimpleCursorAdapter(this,
// Use a template that displays a text view
R.layout.list_item,
// Give the cursor to the list adatper
musicCursor,
// Map the NAME column in the people database to...
new String[] { MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST },
// The "text1" view defined in the XML template
new int[] { R.id.music_title, R.id.music_artist });
setListAdapter(adapter);