按字母格式对来自手机存储的数据进行排序?

时间:2017-08-12 06:20:44

标签: android android-recyclerview adapter recycler-adapter android-storage

我是android的初学者。我正在尝试从存储中获取音频文件并将其放入回收站视图中。它完全加载但是,我不知道如何按字母顺序对整个列表进行排序该文件名的顺序。我尝试了一些方法,但没有奏效。请帮我解决这个问题。谢谢。

这是我的Java文件: 的 Recycler_MusicPlayer.java:

 public class Recycler_MusicPlayer extends Recycler_Music_Player_onclick {
        // private RecyclerView recyclerView;
        Adapter adapter;
        Cursor musiccursor;
        int music_column_index;
        int count;
        FastScrollRecyclerView recyclerView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.recycler__music_player);
            setTitle("List of Songs");
            recyclerView = (FastScrollRecyclerView) findViewById(R.id.recycler);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));

            System.gc();
            String[] proj = {MediaStore.Audio.Media._ID,
                    MediaStore.Audio.Media.DATA,
                    MediaStore.Audio.Media.DISPLAY_NAME,
                    MediaStore.Video.Media.SIZE};
            musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    proj, null, null, null);
            count = musiccursor.getCount();
            adapter = new Adapter(this);
            recyclerView.setAdapter(adapter);

        }


        public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {

            private Context mContext;
            private LayoutInflater inflater;

            public Adapter(Recycler_MusicPlayer recycler_musicPlayer) {
                inflater = LayoutInflater.from(recycler_musicPlayer);
                mContext = recycler_musicPlayer;
            }

            @Override
            public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = inflater.inflate(R.layout.song_layout, parent, false);
                return new MyViewHolder(view);

            }

            @Override
            public void onBindViewHolder(MyViewHolder holder, int position) {
                System.gc();
                String id = 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.SIZE);
                musiccursor.moveToPosition(position);
                id += " Size(KB):" + musiccursor.getString(music_column_index);
                holder.song.setText(id);
            }

            @Override
            public int getItemCount() {
                return count;
            }

            public class MyViewHolder extends RecyclerView.ViewHolder {
                TextView song;
                ImageView play;

                public MyViewHolder(View itemView) {
                    super(itemView);
                    song = (TextView) itemView.findViewById(R.id.song);
                    play = (ImageView) itemView.findViewById(R.id.play);
                    itemView.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            if (mMediaPlayer.isPlaying()) {
                                Log.e("zzzzzzzzzzzz", "is playingggg");
                                mMediaPlayer.pause();
                                pause = 0;
                            }
                            Log.e("ddddddddddd", "after set play on click");
                            int pos = getAdapterPosition();
                            startActivity(new Intent(v.getContext(), Recycler_Music_Player_onclick.class)
                                    .putExtra("pos", pos));
                        }


                    });
                }
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

您需要在管理查询中对记录进行排序,请尝试以下更改

您可以按升序降序管理订单

升序

private String[] array = {"hello", "hi", "what is your name"};

private String getRandomCharacter() {
    return array[random(array.length)];
}

private int random(int length) {
    return new Random().nextInt(length);
}

protected Word_generator() {
    boolean running = true;
    int count = 0;
    int max = 200;
    while (running) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < 1; i++) {
            sb.append(getRandomCharacter());
            System.out.println();
            System.out.println("Word-->");
        }

        System.out.println(sb.toString());


        if (count++ == max) {
            running = false;
            System.out.println("finished");
        }
    }
}
public static void main(String[] args) {
    new Word_generator();

}

降序

String sortOrder = MediaStore.Audio.Media.DISPLAY_NAME+ " ASC";

更改管理查询

String sortOrder = MediaStore.Audio.Media.DISPLAY_NAME+ " DESC";