问题
Hi I create arecyclerview apps. when I click an item it shows text and image but I want to add r.raw.b mp3 file as a third item. How could I do it? here is detailed activity. How Could I change them? if anyone help, thanks alot..
Bundle mBundle = getIntent().getExtras();
if (mBundle != null) {
mToolbar.setTitle(mBundle.getString("Title"));
mFlower.setImageResource(mBundle.getInt("Image"));
mDescription.setText(mBundle.getString("Description"));
here is main page:
GridLayoutManager mGridLayoutManager = new GridLayoutManager(MainActivity.this, 2);
mRecyclerView.setLayoutManager(mGridLayoutManager);
mFlowerList = new ArrayList<>();
mFlowerData = new FlowerData("Rose", getString(R.string.description_flower_rose),
R.drawable.rose,R.raw.b);
and here is my adapter:
@Override
public void onBindViewHolder(final FlowerViewHolder holder, int position) {
holder.mImage.setImageResource(mFlowerList.get(position).getFlowerImage());
holder.mTitle.setText(mFlowerList.get(position).getFlowerName());
holder.mCardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent mIntent = new Intent(mContext, DetailActivity.class);
mIntent.putExtra("Title", mFlowerList.get(holder.getAdapterPosition()).getFlowerName());
mIntent.putExtra("Description", mFlowerList.get(holder.getAdapterPosition()).getFlowerDescription());
mIntent.putExtra("Image", mFlowerList.get(holder.getAdapterPosition()).getFlowerImage());
mContext.startActivity(mIntent);
}
});
回答1:
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();
}
}
回答2:
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;
}
}
}
来源:https://stackoverflow.com/questions/48378997/adding-mp3-file-on-recyclerview