Я следую онлайн-руководству по созданию приложения Parsing JSON для Recycler View. Тем не менее, он ничего не показывает в приложении. Я не знаю, что с этим случилось. Может кто-нибудь помочь мне разобраться в проблеме? Спасибо. введите описание изображения здесь
MainActivity. java
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
List<net.smallacademy.songslist.Song> songs;
private static String JSON_URL = "http://starlord.hackerearth.com/studio";
Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.songsList);
songs = new ArrayList<>();
extractSongs();
}
private void extractSongs() {
RequestQueue queue = Volley.newRequestQueue(this);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, JSON_URL, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject songObject = response.getJSONObject(i);
net.smallacademy.songslist.Song song = new net.smallacademy.songslist.Song();
song.setTitle(songObject.getString("song").toString());
song.setArtists(songObject.getString("artists".toString()));
song.setCoverImage(songObject.getString("cover_image"));
song.setSongURL(songObject.getString("url"));
songs.add(song);
} catch (JSONException e) {
e.printStackTrace();
}
}
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
adapter = new Adapter(getApplicationContext(),songs);
recyclerView.setAdapter(adapter);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("tag", "onErrorResponse: " + error.getMessage());
}
});
queue.add(jsonArrayRequest);
}
}
Song. java
public class Song {
private String title;
private String artists;
private String coverImage;
private String songURL;
public Song(){}
public Song(String title,String artists,String coverImage,String songURL){
this.title = title;
this.artists = artists;
this.coverImage = coverImage;
this.songURL = songURL;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtists() {
return artists;
}
public void setArtists(String artists) {
this.artists = artists;
}
public String getCoverImage() {
return coverImage;
}
public void setCoverImage(String coverImage) {
this.coverImage = coverImage;
}
public String getSongURL() {
return songURL;
}
public void setSongURL(String songURL) {
this.songURL = songURL;
}
}
Адаптер. java
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
LayoutInflater inflater;
List<net.smallacademy.songslist.Song> songs;
public Adapter(Context ctx, List<net.smallacademy.songslist.Song> songs) {
this.inflater = LayoutInflater.from(ctx);
this.songs = songs;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom_list_layout,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.songTitle.setText(songs.get(position).getTitle());
holder.songArtists.setText(songs.get(position).getArtists());
Picasso.get().load(songs.get(position).getCoverImage()).into(holder.songCoverImage);
}
@Override
public int getItemCount() {
return songs.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView songTitle, songArtists;
ImageView songCoverImage;
public ViewHolder(@NonNull View itemView) {
super(itemView);
songTitle = itemView.findViewById(R.id.songTitle);
songArtists = itemView.findViewById(R.id.songArtist);
songCoverImage = itemView.findViewById(R.id.coverImage);
}
}
}
Activity_main. xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/songsList"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
custom_list_layout. xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/coverImage"
android:layout_width="75dp"
android:layout_height="75dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_launcher_background"
tools:ignore="VectorDrawableCompat" />
<TextView
android:id="@+id/songTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="Song Title"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/coverImage"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
android:layout_marginLeft="16dp" />
<TextView
android:id="@+id/songArtist"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Song Artist"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/coverImage"
app:layout_constraintTop_toBottomOf="@+id/songTitle"
android:layout_marginLeft="16dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>