Android: 无法通过 volley 动态添加列表视图项目

Android: not able to add listview items dynamically through volley

您好,我正在开发 CardSwipe 功能,例如 tinder。 (为了理解见附图)

为此,我遵循了 this code,当使用像下面这样的静态数据时,我成功地在 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();

    }

但是当我尝试使用 volley 方式动态添加项目时,项目没有添加到 ListView 中。 (对于这个截击 请参阅 CardSwipeActivity)

中的 loadCards() 方法

我尝试了很多方法来动态加载列表视图中的项目。例如:我使用了 Thread(代码见 this) and i also used Handler (For code see this),但我无法在 ListView 中动态添加项目

如果有人知道这种方法的解决方法请告诉我。谢谢......

编辑

*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);

    }

您可以将项目动态添加到适配器,方法是将项目添加到最初传递给适配器的列表中,如下所示:

itemsaArrayList.add(items);

比您需要从您的 UI 线程这样通知适配器:

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

要在 UI-Thread 上调用它,必须使用 Activity 的 runOnUiThread()。只有这样,notifyDataSetChanged() 才会起作用。

也看看这个post

如果您从服务器获得响应,请尝试将下面的行从 onResponse 方法移动到 onCreate 方法。

     flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);

希望这会奏效