我已经包含了在清单
中读取外部存储空间的权限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;
}
}
}
我正在尝试从我的Android设备读取所有mp3文件。阅读代码有效。但是当我使用recyclerview时它没有显示在它上面。我已经在manifest中包含了读取外部存储权限。请帮助
答案 0 :(得分:0)
你的代码的问题是你在onBindViewHolder方法中获取歌曲数据,因为它每次调用都可能产生问题。更多信息检查RecyclerView.Adapter#onBindViewHolder在初始化recycleler视图之前先读取所有mp3文件然后将列表设置为回收站视图并相应地访问这些项目。
像这样更改你的代码,它会起作用。
PlaylistActivity.java
ObjectMapper
SongsAdapter
Realm
SongsManager
class User:Object, Mappable {
dynamic var name: String?
}
func mapping(map: Map) {
name <- map["Name"]
}
答案 1 :(得分:0)
填写回收站视图的适配器时出错了。
在设置适配器之前获取所有歌曲列表,即主活动中的songList =songsManager.getPlayList(MEDIA_PATH);
。
现在使用构造函数将其传递给适配器
songsAdapter=new SongsAdapter(songlist);
在适配器holder.textView.setText(songlist.get(position).get("file_name"));