Search code examples
androidlistviewlistviewitem

Android: not able to add listview items dynamically through volley


Hi I'm working on CardSwipe functionality like tinder. (For understanding see the attached image)

enter image description here

For this i followed this code, when using static data like bellow, I'm successfully adding items in ListView

private void loadCards2(){

        itemsaArrayList = new ArrayList<>();

        for(int i = 0; i < 10; i++){

            CardSwipeItems items = new CardSwipeItems();

            items.setCardImageUrl("http://i.ytimg.com/vi/PnxsTxV8y3g/maxresdefault.jpg");
            items.setCardDescription("But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness.");

            itemsaArrayList.add(items);
        }

        flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);
        myAppAdapter = new CardSwipeAdapter(itemsaArrayList, CardSwipeActivity.this);
        flingContainer.setAdapter(myAppAdapter);

        initControlls();

    }

But when I'm trying to add the items dynamically by using volley means, items are not adding in the ListView. (For this volley please see the loadCards() method in the CardSwipeActivity)

I tried lot of approaches to load the items in the list view dynamically. For ex: I used Thread (For code see this) and i also used Handler (For code see this) but I'm not able to add the items in ListView dynamically

If any one know the solution for this means please tell me. Thank you.......

Edit

*My method*

private void loadCards(){

        String Url = "myApi";
        Log.e("Url", Url);

        final ProgressDialog dialog = ProgressDialog.show(CardSwipeActivity.this, null, null);
        ProgressBar spinner = new android.widget.ProgressBar(CardSwipeActivity.this, null,android.R.attr.progressBarStyle);
        spinner.getIndeterminateDrawable().setColorFilter(Color.parseColor("#009689"), android.graphics.PorterDuff.Mode.SRC_IN);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.setContentView(spinner);
        dialog.setCancelable(false);
        dialog.show();


        StringRequest request = new StringRequest(Request.Method.GET, Url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                dialog.dismiss();

                if(response != null && !response.startsWith("<HTML>")){

                    Log.e("OnResponse", response);

                    try {
                        JSONArray jsonArray = new JSONArray(response);

                        if(jsonArray.length() > 0){

                            itemsaArrayList = new ArrayList<>();

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

                                JSONObject singleObj = jsonArray.getJSONObject(i);

                                String imgId = singleObj.getString("avatar_file_id");
                                String name = singleObj.getString("name");

                                CardSwipeItems items = new CardSwipeItems();

                                Log.e("imgId", imgId);
                                Log.e("name", name);

                                items.setCardImageUrl(imgId);
                                items.setCardDescription(name);

                                itemsaArrayList.add(items);

                            }

                            flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);
                            myAppAdapter = new CardSwipeAdapter(itemsaArrayList, CardSwipeActivity.this);
                            flingContainer.setAdapter(myAppAdapter);

                            initControlls();

                        }

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

                }else{

                    Log.e("Internet", "Internet");
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                dialog.dismiss();

                if(error != null){

                    Log.e("error", error.toString());
                }else{


                }
            }
        }){

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> params = new HashMap<String, String>();
                params.put("token","b32daf7b50c7f21dba80dd0651174e3839c22f56");
                params.put("user_id","386");

                Log.e("Headers", "**********Headers*************");
                Log.e("token","b32daf7b50c7f21dba80dd0651174e3839c22f56");
                Log.e("user_id","386");

                return params;
            }
        };

        RequestQueue queue = Volley.newRequestQueue(CardSwipeActivity.this);
        queue.add(request);
        queue.getCache().remove(Url);

    }

Solution

  • You can add items dynamically to the adapter by addding items to the list you passed originally to adapter like this:

    itemsaArrayList.add(items);
    

    than you need to notify the adapter like this from your UI thread:

    runOnUiThread(new Runnable() {
        public void run() {
            adapter.notifyDataSetChanged();
        }
    });
    

    And to call it on the UI-Thread, use have to use runOnUiThread() of Activity. Then only, notifyDataSetChanged() will work.

    Also have a look at this post