Todd
Todd

Reputation: 179

RecyclerView Adapter does not fill up with my array of data

It seems like when I try to push my 2 json objects to my RecyclerView it shows only once, I have used the debugger but still cant find the bug

Im parsing a JSONArray of 2 objects and showing it into my RecyclerView

Parser

  public Canciones getJsonResults(){

        Canciones canciones = new Canciones();
        InputStream is = getResources().openRawResource(R.raw.song_data);
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }

        }catch (IOException e) {
            e.printStackTrace();
        }

        finally {

            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        String jsonString = writer.toString();
        try {

            JSONArray objects = new JSONArray(jsonString);
            for (int i = 0; i < objects.length(); i++) {
                JSONObject cancionesObj = objects.getJSONObject(i);
                int id = cancionesObj.getInt("id");
                String song_title = cancionesObj.getString("song_title");
                String album = cancionesObj.getString("album");
                String cover =  cancionesObj.getString("cover");
                String song_id =  cancionesObj.getString("song_id");
                String artist =  cancionesObj.getString("artist");
                canciones.setId(id);
                canciones.setSongTitle(song_title);
                canciones.setAlbum(album);
                canciones.setCover(cover);
                canciones.setSongId(song_id);
                canciones.setArtist(artist);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }


        return canciones;
    }

And Im adding it to my ArrayList like this

mRecyclerView = findViewById(R.id.songsRecycler);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        misCanciones.add(getJsonResults());
        mAdapter = new RecyclerAdapter(this,R.layout.list_item,misCanciones);
        mRecyclerView.setAdapter(mAdapter);

Output

enter image description here

Json Data Structure

[
  {
    "id": 1,
    "song_title": "Master Of Puppets",
    "album": "Master Of Puppets",
    "artist": "Metallica",
    "cover": "metallica_mop",
    "song_id": "biumbis"
  },
  {
    "id": 2,
    "song_title": "Flight Of Icarus",
    "album": "Piece Of Mind",
    "artist": "Iron Maiden",
    "cover": "iron_pom",
    "song_id": "biumbis"
  }
]

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="48dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="4dp"
        android:id="@+id/rowImage"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rowText"
        android:layout_marginTop="4dp"
        android:layout_marginLeft="8dp"
        />



</LinearLayout>

Adapter

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MusicaViewHolder> {

    private Context mContext;
    private int mLayoutResourceId;
    private ArrayList<Canciones> mArrayListCanciones;

    public RecyclerAdapter(Context mContext, int mLayoutResourceId, ArrayList<Canciones> mCanciones) {
        this.mContext = mContext;
        this.mLayoutResourceId = mLayoutResourceId;
        this.mArrayListCanciones = mCanciones;
    }



    @NonNull
    @Override
    public MusicaViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(mContext).inflate(mLayoutResourceId, viewGroup, false);
        return new MusicaViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MusicaViewHolder musicaViewHolder, int position) {

        Canciones canciones = mArrayListCanciones.get(position);

        int drawableId = mContext.getResources().getIdentifier(canciones.getCover(), "drawable", mContext.getPackageName());
        musicaViewHolder.mRowImage.setImageResource(drawableId);
        musicaViewHolder.mTextImage.setText(canciones.getSongTitle());
    }

    @Override
    public int getItemCount() {
        return mArrayListCanciones.size();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public class MusicaViewHolder extends RecyclerView.ViewHolder{

        private ImageView mRowImage;
        private TextView mTextImage;

        public MusicaViewHolder(@NonNull View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            mRowImage = itemView.findViewById(R.id.rowImage);
            mTextImage = itemView.findViewById(R.id.rowText);


        }

Seems like the for loop from the JSONArray is taking only the last value, I was thinking on store for each loop inside another arraylist and then pass it to my ArrayList<Canciones> but it shows duplicates

Upvotes: 0

Views: 287

Answers (2)

Quick learner
Quick learner

Reputation: 11457

i have updated your method , and seems like you are not adding all objects to arraylist

Just use this method

public getJsonResults() {
        ArrayList<Canciones> cancionesArrayList = new ArrayList<>();
        Canciones canciones = new Canciones();
        InputStream is = getResources().openRawResource(R.raw.song_data);
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        String jsonString = writer.toString();
        try {

            JSONArray objects = new JSONArray(jsonString);
            for (int i = 0; i < objects.length(); i++) {
                JSONObject cancionesObj = objects.getJSONObject(i);
                int id = cancionesObj.getInt("id");
                String song_title = cancionesObj.getString("song_title");
                String album = cancionesObj.getString("album");
                String cover = cancionesObj.getString("cover");
                String song_id = cancionesObj.getString("song_id");
                String artist = cancionesObj.getString("artist");
                canciones.setId(id);
                canciones.setSongTitle(song_title);
                canciones.setAlbum(album);
                canciones.setCover(cover);
                canciones.setSongId(song_id);
                canciones.setArtist(artist);
                cancionesArrayList.add(canciones);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }


        mAdapter = new RecyclerAdapter(this, R.layout.list_item, cancionesArrayList);
        mRecyclerView.setAdapter(mAdapter);
    }

Now usage

mRecyclerView = findViewById(R.id.songsRecycler);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        getJsonResults();

Upvotes: 1

Tan Pham
Tan Pham

Reputation: 178

the getJsonResults() method is return only one last value if you write like that.

Edit the getJsonResults() method:

public ArrayList<Canciones> getJsonResults(){
        ArrayList<Canciones> cancioesList = new ArrayList();

        InputStream is = getResources().openRawResource(R.raw.song_data);
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }

        }catch (IOException e) {
            e.printStackTrace();
        }

        finally {

            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        String jsonString = writer.toString();
        try {

            JSONArray objects = new JSONArray(jsonString);
            for (int i = 0; i < objects.length(); i++) {
                Canciones canciones = new Canciones();
                JSONObject cancionesObj = objects.getJSONObject(i);
                int id = cancionesObj.getInt("id");
                String song_title = cancionesObj.getString("song_title");
                String album = cancionesObj.getString("album");
                String cover =  cancionesObj.getString("cover");
                String song_id =  cancionesObj.getString("song_id");
                String artist =  cancionesObj.getString("artist");
                canciones.setId(id);
                canciones.setSongTitle(song_title);
                canciones.setAlbum(album);
                canciones.setCover(cover);
                canciones.setSongId(song_id);
                canciones.setArtist(artist);
                cancioesList.add(canciones);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }


        return cancioesList;
    }

and when you get a list of Canciones objects, add to misCanciones with this method:

misCanciones.addAll(getJsonResults())

Upvotes: 2

Related Questions