Search code examples
androidadapterandroid-recyclerviewandroid-storage

Sort data coming from phone storage in alphabetical format?


I am beginner in android.I am trying to get audio files from storage and put it in recycler view.And it loads perfectly but,i don't know how do i sort that whole list in alphabetical order of that file name.I have tried some ways but it did not worked. Please help me to solve this.Thank you.

This is My Java file: 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));
                        }


                    });
                }
            }
        }

    }

Solution

  • You need to sort record in manage query try below changes

    You can manage order by ascending descending order

    Ascending order

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

    Descending order

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

    Change in manage query

    musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,proj, null, null, sortOrder);