适配器视图需要 Android 帮助来设置文本大小

发布于 2024-10-29 21:17:20 字数 1587 浏览 1 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

×纯※雪 2024-11-05 21:17:20

可能是因为您在 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)));

Probably because you write the same code within try{} statement and within while? ))

I mean these lines:

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)));
哆兒滾 2024-11-05 21:17:20

我已经通过使用 SimpleCursorAdapter 解决了这个问题,如下所示。它是一个内置适配器,非常智能。 此链接对我帮助很大

而且你永远不应该设置 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);

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.

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文