0

好的,我正在为 android 制作一个媒体播放器应用程序。直到现在一切都很好。到目前为止,我有一个列表视图,显示你的 sdcard(内部和外部)上的所有 .mp3 文件,并且在播放时显示音乐可视化器。但我不能为我的生活按字母顺序排列列表。一切都是动态的,所以 xml 在这里不起作用。

 public class MusicPlayerActivity extends Activity {
  ListView musiclist;
  Cursor musiccursor;
  int music_column_index;
  int count;
  private Intent aIntent;
  public static String filename;
  private RelativeLayout mRelativeLayout;


  @Override
  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init_phone_music_grid();
        mRelativeLayout = new RelativeLayout(this);
        setContentView(mRelativeLayout);
        mRelativeLayout.addView(musiclist);

  }

  public void init_phone_music_grid() {
        System.gc();

        String[] projection = {
                MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.DATA,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.DURATION,
                MediaStore.Audio.Media.ALBUM
        };

        Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

        musiccursor = managedQuery(allsongsuri, projection , selection, null, null);

        count = musiccursor.getCount();
        musiclist = (ListView) findViewById(R.id.PhoneMusicList);
        musiclist.setAdapter(new EfficientAdapter(getApplicationContext()));
        musiclist.setOnItemClickListener(musicgridlistener);

  }
  private OnItemClickListener musicgridlistener = new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position,long id) {
              System.gc();
              music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
              musiccursor.moveToPosition(position);
              filename = musiccursor.getString(music_column_index);
              aIntent = new Intent(v.getContext(), AudioFX.class);
              aIntent.getStringExtra(filename);
              startActivity(aIntent);
        }
      };

     class EfficientAdapter extends BaseAdapter {
      private Context mContext;

      public EfficientAdapter(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();
              String id = null;
              TextView tv;
              if (convertView == null) {
              tv = new TextView(mContext.getApplicationContext());
              } else{
              tv = (TextView) convertView;
              }
              musiccursor.moveToPosition(position);
              music_column_index = musiccursor
              .getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
              id = musiccursor.getString(music_column_index);
              tv.setText(id);
              tv.setTextSize(20);
              return tv;
              }}}
4

2 回答 2

1

你只需要使用DEFAULT_SORT_ORDER,像这样:

String sortOrder = MediaStore.Audio.Media.DEFAULT_SORT_ORDER;
musiccursor = managedQuery(allsongsuri, projection , selection, null, sortOrder);
于 2012-04-18T06:07:53.670 回答
-1

在 getView 方法中显示标题不会得到按字母顺序排列的列表。

为了将列表按字母顺序排列,请将所有音乐标题放入字符串数组或 ArrayList 中,手动将它们按字母顺序排列,然后将该字符串数组传递给 EfficientAdapters 构造函数并将其存储在本地并根据列表位置使用它们。

 class EfficientAdapter extends BaseAdapter {
  private Context mContext;
  private String values[];

  public EfficientAdapter(Context c, String[] a) {
      mContext = c;
      values = a;
  }
      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();
          String id = null;
          TextView tv;
          if (convertView == null) {
          tv = new TextView(mContext.getApplicationContext());
          } else{
          tv = (TextView) convertView;
          }

          id = values[position];


          tv.setText(id);
          tv.setTextSize(20);
          return tv;
          }}}

希望这可以帮助.....

于 2012-04-18T06:03:51.810 回答