我是 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));
}
});
}
}
}
}