在onRefresh调用中更新listview的Android

时间:2014-07-22 03:24:28

标签: android android-listview swiperefreshlayout

所以我从一个线程中动态构建我的列表视图,因此它可以异步工作。我刚刚实现了googles SwipeRefreshLayout。所以在onRefresh调用中,我想重建列表。然而,我尝试的一切都没有做任何事情或告诉我。

"只有创建视图层次结构的原始线程才能触及其视图。“

如何构建列表

 public void updateQueue(final ListView lstvw, Context ctx, final Boolean refresh) {

        final String [] stringArray = stringArrayList.toArray(new String[stringArrayList.size()]);
        adapter = new ArrayAdapter(ctx,android.R.layout.simple_list_item_1, stringArrayList);


        new Thread(new Runnable() {
            public void run() {

                try {
                    try {

                        HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
                        HttpGet httpget = new HttpGet("http://test.net/test.php"); // Set the action you want to do
                        HttpResponse response = httpclient.execute(httpget); // Executeit
                        HttpEntity entity = response.getEntity();
                        InputStream is = entity.getContent(); // Create an InputStream with the response
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                        StringBuilder sb = new StringBuilder();
                        String line = null;
                        while ((line = reader.readLine()) != null) // Read line by line
                            sb.append(line + "\n");


                        String resString = sb.toString(); // Result is here
                        JSONArray json = new JSONArray(resString);
                        for(int i=0;i<json.length();i++){
                            JSONObject json_data = json.getJSONObject(i);
                            String jsonvalues =  json_data.getString("title");
                            if (!jsonvalues.equals("")) {
                                stringArrayList.add(json_data.getString("artist") + " - " + json_data.getString("title"));
                                // .. get all value here
                            }



                            Log.e("TEST", "TEST RES E " + jsonvalues);

                        }
                        if (stringArrayList.isEmpty()) {
                            stringArrayList.add("No items currently in queue");
                        }


                        is.close(); // Close the stream
                        if (refresh) {
                            getActivity().runOnUiThread(new Runnable() {
                                public void run() {
                                    lstvw.setVisibility(View.VISIBLE);
                                    adapter.notifyDataSetChanged();
                                    spinner.setVisibility(View.GONE);
                                }
                            });
                        }

                        //adapter.notifyDataSetChanged();
                        Log.e("TEST", "TEST RES Ended ");




                    }
                    catch (Exception e) {
                        Log.e("TESTAPP", "SOME Catch Error E "  + e.toString());
                    }
                }
                catch (Exception e){
                    Log.e("TESTAPP", "SOME Error" + e.getMessage());
                }
            }
        }).start();
    }


}

onRefresh

            swipeLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_container);
        swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        swipeLayout.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() {

                @Override public void onRefresh() {
                    updateQueue(lstvw,ctx,true);
                    swipeLayout.setRefreshing(false);

                }});

1 个答案:

答案 0 :(得分:1)

使用 asynctask 代替线程,它非常容易使用。 ui相关的chages应该在主线程中完成。 请检查运行应用时是否实际注释了notifydatasetchanged方法?

//adapter.notifyDataSetChanged();
 Log.e("TEST", "TEST RES Ended ");

如果没有,那么将它放入你的runonui线程method.or而不是使用线程使用asynctask