Alphabetize在ListView中查询音乐文件

时间:2012-04-18 05:47:01

标签: android android-listview alphabetical

好的,我正在为Android制作媒体播放器应用程序。一切都还好,直到现在。到目前为止,我有一个列表视图,显示你的SD卡(内部和外部)上的所有.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;
              }}}

2 个答案:

答案 0 :(得分:1)

您只需使用DEFAULT_SORT_ORDER,就像这样:

String sortOrder = MediaStore.Audio.Media.DEFAULT_SORT_ORDER;
musiccursor = managedQuery(allsongsuri, projection , selection, null, sortOrder);

答案 1 :(得分:-1)

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

为了将列表将所有音乐标题按字母顺序排列到String数组或ArrayList中 手动按字母顺序排列并将该String数组传递给EfficientAdapters Constructor 并将其存储在本地并根据列表位置使用它们。

 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;
          }}}

希望这会有所帮助.....