Last active
January 21, 2020 07:21
-
-
Save bikashthapa01/50242f4055e3c0fec707ca9589c22e19 to your computer and use it in GitHub Desktop.
Parsing JSON with Recyclerview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.smallacademy.songslist; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import androidx.annotation.NonNull; | |
import androidx.recyclerview.widget.RecyclerView; | |
import com.android.volley.AuthFailureError; | |
import com.squareup.picasso.Picasso; | |
import java.util.List; | |
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> { | |
LayoutInflater inflater; | |
List<Song> songs; | |
public Adapter(Context ctx, List<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) { | |
// bind the data | |
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); | |
// handle onClick | |
itemView.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Toast.makeText(v.getContext(), "Do Something With this Click", Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" | |
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" /> | |
<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" /> | |
<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" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
</androidx.cardview.widget.CardView> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.smallacademy.songslist; | |
import androidx.appcompat.app.AppCompatActivity; | |
import androidx.recyclerview.widget.LinearLayoutManager; | |
import androidx.recyclerview.widget.RecyclerView; | |
import android.os.Bundle; | |
import android.util.Log; | |
import com.android.volley.Request; | |
import com.android.volley.RequestQueue; | |
import com.android.volley.Response; | |
import com.android.volley.VolleyError; | |
import com.android.volley.toolbox.JsonArrayRequest; | |
import com.android.volley.toolbox.Volley; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MainActivity extends AppCompatActivity { | |
RecyclerView recyclerView; | |
List<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); | |
Song song = new 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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.smallacademy.songslist; | |
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
Song song = new 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment