I have included the permission to read external storage in manifest
PlaylistActivity.java
public class PlaylistActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private SongsAdapter songsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playlist);
recyclerView=(RecyclerView)findViewById(R.id.playlistactivityrecyclerview);
songsAdapter=new SongsAdapter();
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setAdapter(songsAdapter);
Toast.makeText(getApplicationContext(),"Hi",Toast.LENGTH_LONG).show();
}
}
SongsAdapter
public class SongsAdapter extends RecyclerView.Adapter<SongsAdapter.MyViewHolder>{
SongsManager songsManager;
String MEDIA_PATH = Environment.getExternalStorageDirectory() + "";
ArrayList<HashMap<String, String>> songList=new ArrayList<>();;
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
songsManager=new SongsManager();
songList =songsManager.getPlayList(MEDIA_PATH);
if (songList != null) {
for (int i = 0; i < songList.size(); i++) {
String fileName = songList.get(i).get("file_name");
String filePath = songList.get(i).get("file_path");
holder.textView.setText(fileName);
}
}
}
@Override
public int getItemCount() {
return songList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView textView;
public MyViewHolder(View itemView){
super(itemView);
textView=(TextView)itemView.findViewById(R.id.listitemtextview);
}
@Override
public void onClick(View v) {
}
}
}
SongsManager
public class SongsManager {
public ArrayList<HashMap<String, String>> getPlayList(String rootPath) {
ArrayList<HashMap<String, String>> fileList = new ArrayList<>();
try {
File rootFolder = new File(rootPath);
File[] files = rootFolder.listFiles(); //here you will get NPE if directory doesn't contains any file,handle it like this.
for (File file : files) {
if (file.isDirectory()) {
if (getPlayList(file.getAbsolutePath()) != null) {
fileList.addAll(getPlayList(file.getAbsolutePath()));
} else {
break;
}
} else if (file.getName().endsWith(".mp3")) {
HashMap<String, String> song = new HashMap<>();
song.put("file_path", file.getAbsolutePath());
song.put("file_name", file.getName());
fileList.add(song);
}
}
return fileList;
} catch (Exception e) {
return null;
}
}
}
I am trying to read all mp3 files from my android device.The reading code works.But when i use recyclerview it does not get display on it.I have included the read external storage permission in manifest.Please help
The problem with your code is your taking the songs data in onBindViewHolder method and since it is every time called may be it producing the issue.For more info check RecyclerView.Adapter#onBindViewHolder Before initializing recycler view first read all mp3 files then set list to recycler view and access those items accordingly.
Change your codes like this, it will work.
PlaylistActivity.java
public class PlaylistActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private SongsAdapter songsAdapter;
SongsManager songsManager;
String MEDIA_PATH = Environment.getExternalStorageDirectory() + "";
ArrayList<HashMap<String, String>> songList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playlist);
recyclerView=(RecyclerView)findViewById(R.id.playlistactivityrecyclerview);
songsManager=new SongsManager();
songList =songsManager.getPlayList(MEDIA_PATH);
songsAdapter=new SongsAdapter(songList);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerView.setAdapter(songsAdapter);
Toast.makeText(getApplicationContext(),"Hi",Toast.LENGTH_LONG).show();
}
}
SongsAdapter
public class SongsAdapter extends RecyclerView.Adapter<SongsAdapter.MyViewHolder>{
ArrayList<HashMap<String, String>> songList;
public SongsAdapter(ArrayList<HashMap<String, String>> songList)
{
this.songList = songList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.textView.setText(songlist.get(position).get("file_name"));
}
}
@Override
public int getItemCount() {
return songList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView textView;
public MyViewHolder(View itemView){
super(itemView);
textView=(TextView)itemView.findViewById(R.id.listitemtextview);
}
@Override
public void onClick(View v) {
}
}
}
SongsManager
public class SongsManager {
public ArrayList<HashMap<String, String>> getPlayList(String rootPath) {
ArrayList<HashMap<String, String>> fileList = new ArrayList<>();
try {
File rootFolder = new File(rootPath);
File[] files = rootFolder.listFiles(); //here you will get NPE if directory doesn't contains any file,handle it like this.
for (File file : files) {
if (file.isDirectory()) {
if (getPlayList(file.getAbsolutePath()) != null) {
fileList.addAll(getPlayList(file.getAbsolutePath()));
} else {
break;
}
} else if (file.getName().endsWith(".mp3")) {
HashMap<String, String> song = new HashMap<>();
song.put("file_path", file.getAbsolutePath());
song.put("file_name", file.getName());
fileList.add(song);
}
}
return fileList;
} catch (Exception e) {
return null;
}
}
}