Sabir Hossain
Sabir Hossain

Reputation: 1205

RecyclerView not showing data from JSON Array after parsing

I'm trying to get a JSON array using Android Volley but my RecyclerView not showing data. the recyclerView is empty. I have checked my URL there is no problem.

BackgroundTask.java

    public class BackgroundTask {

    Context context;
    String jsonURL = "my url";
    ArrayList<Contact> arrayList = new ArrayList<>();

    public BackgroundTask(Context ctx){

        this.context = ctx;
    }

    public ArrayList<Contact> getList(){

        final JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, jsonURL, (String) null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        int count = 0;
                        while (count > response.length()){
                            try {
                                JSONObject jsonObject = response.getJSONObject(count);
                                Contact contact = new Contact(jsonObject.getString("Name"),jsonObject.getString("Email"));
                                arrayList.add(contact);
                                count++;

                            } catch (JSONException e) {

                                Toast.makeText(context,"Error",Toast.LENGTH_SHORT).show();
                                e.printStackTrace();
                            }
                        }

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

            }
        }
        );
        Singletone.getSingletone(context).addToRequest(jsonArrayRequest);
        return arrayList;
    }
}

MySingletone.java

    public class Singletone {

    private static Singletone singletone;
    private RequestQueue requestQueue;
    private static Context context;

    private Singletone(Context ctx){
        context = ctx.getApplicationContext();
        requestQueue = getRequestQueue();
    }

    public RequestQueue getRequestQueue(){

        if(requestQueue == null){
            requestQueue = Volley.newRequestQueue(context);
        }
        return requestQueue;
    }

    public static synchronized Singletone getSingletone(Context ctx){

        if(singletone == null){
            singletone = new Singletone(ctx.getApplicationContext());
        }

        return singletone;
    }

    public<T> void addToRequest(Request<T> request){

        requestQueue.add(request);
    }

}

RecyclerAdapter.java

    public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.MyViewHolder> {

    ArrayList<Contact> arrayList = new ArrayList<>();

    public RecycleAdapter(ArrayList<Contact> array){

        this.arrayList = array;
    }

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

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item,parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return  myViewHolder;
    }

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

        holder.Name.setText(arrayList.get(position).getName().toString());
        holder.Email.setText((arrayList.get(position).getEmail().toString()));

    }

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


    public static class MyViewHolder extends RecyclerView.ViewHolder{

        TextView Name, Email;
        public MyViewHolder(View itemView) {
            super(itemView);

            Name = (TextView) itemView.findViewById(R.id.txt_name);
            Email = (TextView) itemView.findViewById(R.id.txt_email);
        }
    }
}

Upvotes: 0

Views: 1358

Answers (3)

Neerajlal K
Neerajlal K

Reputation: 6828

The request is asynchronous. You cant just return the ArrayList from getList method. You have to populate the RecyclerView inside the callback method onResponse by calling adapter.notifyDataSetChanged().

Also your while loop never runs because count is never greater than response.length();

@Override
public void onResponse(JSONArray response) {
    if(response != null && response.length() > 0){
        for (int count = 0; count < response.length(); count++ ) {
            try {
                JSONObject jsonObject = response.getJSONObject(count);
                Contact contact = new Contact(jsonObject.getString("Name"), jsonObject.getString("Email"));
                arrayList.add(contact);
            } catch (JSONException e) {
                Toast.makeText(context,"Error",Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
        }
        yourAdapter.notifyDataSetChanged();
    }
}

Upvotes: 2

rafsanahmad007
rafsanahmad007

Reputation: 23881

Try changing this:

int count = 0;
while (count > response.length()){
    try {
        JSONObject jsonObject = response.getJSONObject(count);
        Contact contact = new Contact(jsonObject.getString("Name"),jsonObject.getString("Email"));
        arrayList.add(contact);
        count++;

    } catch (JSONException e) {

        Toast.makeText(context,"Error",Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }
}

to

 for(int count=0;count<response.length();count++){
    try {
        JSONObject jsonObject = response.getJSONObject(count);
        Contact contact = new Contact(jsonObject.getString("Name"),jsonObject.getString("Email"));
        arrayList.add(contact);

    } catch (JSONException e) {

        Toast.makeText(context,"Error",Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }
}

Upvotes: 1

John O&#39;Reilly
John O&#39;Reilly

Reputation: 10330

You need to call notifyDataSetChanged(); after updating arrayList

Upvotes: 0

Related Questions