我创建了一个使用Parse JSON的音乐播放器应用程序。但是我遇到一个问题,如何从JSON数据中创建下一个按钮?
公共类RecyclerViewAdapter扩展了RecyclerView.Adapter {
private Context mContext;
private List<Songs> mData;
RequestOptions option;
public RecyclerViewAdapter(Context mContext, List<Songs> mData) {
this.mContext = mContext;
this.mData = mData;
option = new RequestOptions().centerCrop().placeholder(R.drawable.image).error(R.drawable.image);
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater inflater = LayoutInflater.from(mContext);
view = inflater.inflate(R.layout.list_music, parent, false);
final MyViewHolder myViewHolder = new MyViewHolder(view);
myViewHolder.c_content.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(mContext, MusicPlayer.class);
i.putExtra("wap_judul", mData.get(myViewHolder.getAdapterPosition()).getJudul());
i.putExtra("wap_artis", mData.get(myViewHolder.getAdapterPosition()).getArtis());
i.putExtra("wap_lagu", mData.get(myViewHolder.getAdapterPosition()).getLagu());
i.putExtra("wap_lirik", mData.get(myViewHolder.getAdapterPosition()).getLirik());
i.putExtra("wap_gambar", mData.get(myViewHolder.getAdapterPosition()).getGambar());
mContext.startActivity(i);
}
});
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.tv_judul.setText(mData.get(position).getJudul());
holder.tv_artis.setText(mData.get(position).getArtis());
Glide.with(mContext).load(mData.get(position).getGambar()).apply(option).into(holder.img_gambar);
}
@Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_judul;
TextView tv_artis;
ImageView img_gambar;
LinearLayout c_content;
public MyViewHolder(View itemView) {
super(itemView);
c_content = itemView.findViewById(R.id.container);
tv_judul = itemView.findViewById(R.id.judul_lagu);
tv_artis = itemView.findViewById(R.id.artis);
img_gambar = itemView.findViewById(R.id.image_lagu);
}
}
公共类MainActivity扩展了AppCompatActivity,实现了SwipeRefreshLayout.OnRefreshListener {
private final String URL_Data = "http://koleksiapp.com/coba.json";
private JsonArrayRequest request;
private RequestQueue requestQueue;
public SwipeRefreshLayout swipeRefreshLayout;
private List<Songs> listSongs;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipedown);
listSongs = new ArrayList<>();
recyclerView = findViewById(R.id.recycleviewid);
swipeRefreshLayout.setColorSchemeResources(R.color.swipe1, R.color.swipe2, R.color.swipe3);
swipeRefreshLayout.setOnRefreshListener(this);
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
jsonRequest();
}
});
}
private void jsonRequest() {
swipeRefreshLayout.setRefreshing(true);
request = new JsonArrayRequest(URL_Data, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jsonObject = null;
for (int i = 0; i < response.length(); i++) {
try {
jsonObject = response.getJSONObject(i);
Songs songs = new Songs();
songs.setJudul(jsonObject.getString("judul"));
songs.setArtis(jsonObject.getString("artis"));
songs.setLagu(jsonObject.getString("lagu"));
songs.setGambar(jsonObject.getString("gambar"));
listSongs.add(songs);
} catch (JSONException e) {
e.printStackTrace();
}
}
setRecyclerView(listSongs);
swipeRefreshLayout.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(request);
}
private void setRecyclerView(List<Songs> listSongs) {
RecyclerViewAdapter myadapter = new RecyclerViewAdapter(this, listSongs);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(myadapter);
}
@Override
public void onRefresh() {
listSongs.clear();
jsonRequest();
}
公共类MusicPlayer扩展了AppCompatActivity {
private MediaPlayer playerMusic = null;
private int musicPosition = 0;
private Button buttonplay, buttonnext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music_player);
String judul = getIntent().getExtras().getString("wap_judul");
String artis = getIntent().getExtras().getString("wap_artis");
buttonplay = findViewById(R.id.play);
buttonnext = findViewById(R.id.next);
TextView tv_judul = findViewById(R.id.judul_lagu);
TextView tv_artis = findViewById(R.id.artis);
tv_judul.setText(judul);
tv_artis.setText(artis);
buttonnext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
buttonplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playpause();
}
});
}
private void playpause() {
if(playerMusic == null){
playerload();
}
if(!playerMusic.isPlaying()) {
if(musicPosition > 0 ){
playerMusic.seekTo(musicPosition);
}
buttonplay.setBackgroundResource(R.drawable.pause);
playerMusic.start();
playlagi();
} else {
pausemusic();
}
}
private void playerload() {
String lagu = getIntent().getExtras().getString("wap_lagu");
playerMusic = new MediaPlayer();
try{
playerMusic.setAudioStreamType(AudioManager.STREAM_MUSIC);
playerMusic.setDataSource(lagu);
playerMusic.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
private void playlagi() {
playerMusic.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
if(playerMusic != null){
buttonplay.setBackgroundResource(R.drawable.play);
musicPosition = 0;
playerMusic.stop();
playerMusic.release();
playerMusic = null;
}
}
});
}
private void pausemusic() {
buttonplay.setBackgroundResource(R.drawable.play);
playerMusic.pause();
musicPosition = playerMusic.getCurrentPosition();
}