Data parsed correctly from server but Android recyclerview is empty

Tags: , , , ,



I made an online music player with PHP code server and I have no problem fetching data in JSON format. I have a problem with the Android side where no data is shown in my recyclerview although I initialized it correctly. Please help me to find out what’s wrong.

Here’s my code:

MainActivity:

public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RequestQueue requestQueue;
Context context;
ListAdapter adapter;
LinearLayoutManager layoutManager;
List<Listening> list = new ArrayList<>();
String url = "https://www.learnhere.ir/listening.php";
AccessDataOnServer data;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context = this;
    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    requestQueue = Volley.newRequestQueue(context);
    layoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
    adapter = new ListAdapter(list, context);
    data = new AccessDataOnServer();
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(layoutManager);
    data.getData(context, list, recyclerView, url, requestQueue);
}
}

ListAdapter:

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {

List<Listening> listeningList;
Context context;

public ListAdapter(List<Listening> listeningList, Context context) {
    this.listeningList = listeningList;
    this.context = context;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.items, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    final Listening listening = listeningList.get(position);
    holder.track.setText(listening.getTitle());
    holder.term.setText(listening.getTerm());
    Picasso.get().load(listening.getCover()).placeholder(R.mipmap.ic_launcher).into(holder.avatar);
}

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

public static class ViewHolder extends RecyclerView.ViewHolder {
    CardView cardView;
    ImageView avatar;
    TextView track, term;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        cardView = itemView.findViewById(R.id.card_view);
        avatar = itemView.findViewById(R.id.avatar);
        track = itemView.findViewById(R.id.track_title);
        term = itemView.findViewById(R.id.term);
    }
}
}

Class to get my data by help of Volley Library:

public class AccessDataOnServer extends AppCompatActivity {
RequestQueue req;
Context context;
ListAdapter adapter;
List<Listening> list1 = new ArrayList<>();
String url = "https://learnhere.ir/listening.php";

public void getData(final Context con, final List<Listening> list1, final RecyclerView recyclerView, String url, RequestQueue req
) {
    this.req = req;
    req = Volley.newRequestQueue(con);
    this.list1 = list1;
    JsonObjectRequest job = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

            try {

                JSONArray jsonArray = response.getJSONArray("music");

                for (int i = 0; i < jsonArray.length(); i++) {

                    JSONObject job = jsonArray.getJSONObject(i);

                    String title = job.getString("title");
                    String cover = job.getString("cover");
                    String link = job.getString("track");
                    String term = job.getString("term");

                    Listening m = new Listening();
                    m.setTitle(title);
                    m.setTerm(term);
                    m.setCover(cover);
                    m.setTrackLink(link);
                    list1.add(m);

                }

                adapter = new ListAdapter(list1, con);
                recyclerView.setItemAnimator(new DefaultItemAnimator());
                recyclerView.setAdapter(adapter);


            } catch (Exception e) {
                Toast.makeText(con, "try error", Toast.LENGTH_SHORT).show();

            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            Toast.makeText(con, "not response", Toast.LENGTH_SHORT).show();

        }
    });

    req.add(job);
}
}

My data model:

public class Listening {

private String title;
private String term;
private String cover;
private String trackLink;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getTerm() {
    return term;
}

public void setTerm(String term) {
    this.term = term;
}

public String getCover() {
    return cover;
}

public void setCover(String cover) {
    this.cover = cover;
}

public String getTrackLink() {
    return trackLink;
}

public void setTrackLink(String trackLink) {
    this.trackLink = trackLink;
}
}

Answer

Your codes work correctly and there’s no problem with them. Did you test it on emulator? Try to wipe data on emulator or test on physical device. Your problem should be solved.



Source: stackoverflow