Why is my recyclerview only displaying the content of the first item?

Renboy

I am having a problem with my recyclerview, It only displays the content of the first item like this: enter image description here

I have no idea what caused this, I'm really confused because I have never encountered something like this before. As you can see on the toast, the response return 3 data but I don't understand why the others are not being displayed.

Playlist.java

public class Playlist extends AppCompatActivity {

// inisiasi toolbar
private Toolbar toolbar;

// navigation drawer
public DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;

RecyclerView recyclerView;
String[] id,title,dir, artists;
ArrayList<String> artist;
String navTitles[];
TypedArray navIcons;
RecyclerView.Adapter recyclerViewAdapter;

TextView textView;
String video;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_playlist);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    recyclerView  = (RecyclerView) findViewById(R.id.recyclerView);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
    upArrow.setColorFilter(getResources().getColor(R.color.colorIcons), PorterDuff.Mode.SRC_ATOP);
    getSupportActionBar().setHomeAsUpIndicator(upArrow);
    Intent intent = getIntent();
    video = intent.getStringExtra("songs");

    //textView = (TextView) findViewById(R.id.text);
    //textView.setText(video);

    getPlaylist();


    // dir = PlaylistJson.dirs;

    //artist = new ArrayList<String>(Arrays.asList(title));

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case android.R.id.home:
            finish();
            break;
    }
    return super.onOptionsItemSelected(item);
}

private void getPlaylist(){

    final ProgressDialog loading = ProgressDialog.show(this,"Fetching Data","Please wait...",false,false);
    //Creating a string request
    StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://musicmania.hol.es/playlist/getSongsFromPlaylist",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    //If we are getting success from server
                    Toast.makeText(Playlist.this, response, Toast.LENGTH_LONG).show();

                    loading.dismiss();
                    showPlaylistJSON(response);

                    id = PlaylistJson.ids;
                    title = PlaylistJson.titles;
                    artists = PlaylistJson.artists;

                    recyclerView= (RecyclerView) findViewById(R.id.my_recycler_view);
                    RecyclerViewAdapter adapter=new RecyclerViewAdapter(id, title,artists, Playlist.this);
                    recyclerView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    recyclerView.setHasFixedSize(true);
                    recyclerView.setLayoutManager(new LinearLayoutManager(Playlist.this));
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //You can handle error here if you want
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> params = new HashMap<>();
            //Adding parameters to request
            params.put("playlist", video);

            //returning parameter
            return params;
        }
    };

    //Adding the string request to the queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

private void showPlaylistJSON(String json){
    PlaylistJson pj = new PlaylistJson(json);
    pj.parseJSON();
    }
}

RecyclerViewAdapter.java

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> {

LayoutInflater inflater;
Context context;

String[] id,title, artists;

public RecyclerViewAdapter(String[] id, String[] titles, String[] artists, Context context){
    this.id = id;
    this.title = titles;
    this.artists = artists;
    this.context = context;
}

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = null;
    RecyclerViewHolder viewHolder = null;

    if(Integer.parseInt(id[0]) != 0){
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false);
        viewHolder = new RecyclerViewHolder(view, context);
    }else{
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_list, parent, false);
        viewHolder = new RecyclerViewHolder(view, context);
    }
    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {

    if(Integer.parseInt(id[0]) != 0) {
        holder.item2.setText(title[position]);
        holder.imageView2.setTag(holder);
        holder.artist.setText(artists[position]);
    }else{
        holder.item2.setText(title[position]);
    }

}

@Override
public int getItemCount() {

    return title.length;
}

public static class RecyclerViewHolder extends RecyclerView.ViewHolder {

    TextView item;
    ImageView imageView;
    TextView item2;
    TextView artist;
    ImageView imageView2;
    ImageButton addtoplaylist;
    Context context;

    public RecyclerViewHolder(final View itemView, final Context context) {
        super(itemView);

        this.context = context;
        item = (TextView) itemView.findViewById(R.id.tv_NavTitle);
        imageView = (ImageView) itemView.findViewById(R.id.iv_NavIcon);
        item2 = (TextView) itemView.findViewById(R.id.list_title);
        imageView2 = (ImageView) itemView.findViewById(R.id.list_avatar);
        artist = (TextView) itemView.findViewById(R.id.list_artist);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(v.getContext(), Video.class);
                intent.putExtra("video", ParseJson.dirs[getAdapterPosition()]);
                v.getContext().startActivity(intent);
            }
        });


    }
}

}

PlaylistJson.java

package com.example.rendell.musicmaniajukebox.json_model;

import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class PlaylistJson {
public static String[] ids;
public static String[] titles;
public static String[] artists;
public static String[] dirs;

public static final String JSON_ARRAY = "result";
public static final String KEY_ID = "id";
public static final String KEY_TITLE = "title";
public static final String KEY_ARTIST = "artist";
public static final String KEY_DIR = "dir";

private JSONArray users = null;

private String json;

public PlaylistJson(String json){
    this.json = json;
}

public void parseJSON(){
    JSONObject jsonObject=null;
    try {
        jsonObject = new JSONObject(json);
        users = jsonObject.getJSONArray(JSON_ARRAY);

        ids = new String[users.length()];
        titles = new String[users.length()];
        artists = new String[users.length()];
        dirs = new String[users.length()];

        for(int i=0;i<users.length();i++){
            JSONObject jo = users.getJSONObject(i);
            ids[i] = jo.getString(KEY_ID);
            titles[i] = jo.getString(KEY_TITLE);
            artists[i] = jo.getString(KEY_ARTIST);
            dirs[i] = jo.getString(KEY_DIR);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
  }
}
Renboy

So the problem was in my PlaylistJson.java file. My volley response only returns 3 items per set e.g. {"id":1, "title": "song", "artist":"artist"} but I am also initialing for the dir which doesn't receive any json so maybe the bug came from that. Anyway, removed that and it worked.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Only first item in ListView is displaying

From Dev

Collapsing Action Bar but only shows when RecyclerView is fully displaying the first item

From Dev

ListBox only displaying first item selected

From Dev

Why Is My C# Code Only Displaying The First Row from my Database?

From Dev

Why does my spinner not displaying selected item

From Dev

why is my list item thumbs displaying like this?

From Dev

my Recyclerview only show last item

From Dev

Why isn't my RecyclerView displaying my adapter items?

From Dev

multiple child in nodes in php simplexml-displaying only first item

From Dev

My loop is only checking the first array item

From Dev

My loop is only checking the first array item

From Dev

Recyclerview sqllite update only works on first item on the list

From Dev

Why is my IdentitySet only showing the last item?

From Dev

RecyclerView only displaying the top Data

From Dev

Why my listview only detects the first checkbox?

From Dev

Why is only my first HTTP request running?

From Dev

Why is my for only working on the first variable?

From Dev

Why is my for loop only grabbing first element?

From Dev

Displaying only last item in array

From Dev

why is my image not displaying?

From Dev

How to make RecyclerView To Show Only First Two Item and remain item will be visible by view More options

From Dev

Displaying hashed content on my website

From Dev

Why is my list function only outputting first value of my array?

From Dev

XtreReport displaying second empty page when my first page content is less ? Winforms Devexpress

From Dev

Why doesn't my array start with the first item that is mapped to it? Swift

From Dev

Why is CSS content image not displaying?

From Dev

why it is displaying object object not content?

From Dev

Android RecyclerView select first Item

From Dev

Why does my Android ListAdapter only show one item?

Related Related

  1. 1

    Only first item in ListView is displaying

  2. 2

    Collapsing Action Bar but only shows when RecyclerView is fully displaying the first item

  3. 3

    ListBox only displaying first item selected

  4. 4

    Why Is My C# Code Only Displaying The First Row from my Database?

  5. 5

    Why does my spinner not displaying selected item

  6. 6

    why is my list item thumbs displaying like this?

  7. 7

    my Recyclerview only show last item

  8. 8

    Why isn't my RecyclerView displaying my adapter items?

  9. 9

    multiple child in nodes in php simplexml-displaying only first item

  10. 10

    My loop is only checking the first array item

  11. 11

    My loop is only checking the first array item

  12. 12

    Recyclerview sqllite update only works on first item on the list

  13. 13

    Why is my IdentitySet only showing the last item?

  14. 14

    RecyclerView only displaying the top Data

  15. 15

    Why my listview only detects the first checkbox?

  16. 16

    Why is only my first HTTP request running?

  17. 17

    Why is my for only working on the first variable?

  18. 18

    Why is my for loop only grabbing first element?

  19. 19

    Displaying only last item in array

  20. 20

    why is my image not displaying?

  21. 21

    How to make RecyclerView To Show Only First Two Item and remain item will be visible by view More options

  22. 22

    Displaying hashed content on my website

  23. 23

    Why is my list function only outputting first value of my array?

  24. 24

    XtreReport displaying second empty page when my first page content is less ? Winforms Devexpress

  25. 25

    Why doesn't my array start with the first item that is mapped to it? Swift

  26. 26

    Why is CSS content image not displaying?

  27. 27

    why it is displaying object object not content?

  28. 28

    Android RecyclerView select first Item

  29. 29

    Why does my Android ListAdapter only show one item?

HotTag

Archive