适配器视图需要 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;
}
}
I am developing an android media player. I am new to ANDROID. I am using the following code to show all the music from a query result. I want every song once, but it showing a single song more than once and ignoring many others, if I set the text size in the adapter view as in the following code. How can I set the text size in the adapter view and avoid this problem?
Thank u
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能是因为您在 try{} 语句和 while 内编写了相同的代码? ))
我的意思是这些行:
Probably because you write the same code within try{} statement and within while? ))
I mean these lines:
我已经通过使用 SimpleCursorAdapter 解决了这个问题,如下所示。它是一个内置适配器,非常智能。 此链接对我帮助很大
而且你永远不应该设置 android列表视图的 :layout_height="wrap_content"。它会导致列表项重复出现。
I have solved it by using SimpleCursorAdapter like below. It is a built in adapter, which is really smart. This link helped me a lot
And you should never set android:layout_height="wrap_content" of a listview. It causes the repeating occurrence of list items.