0

所以我从一个线程中动态地构建我的列表视图,因此它可以异步工作。我刚刚实现了谷歌的 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);

                }});
4

1 回答 1

1

使用asynctask而不是 thread 它很容易使用。ui 相关的更改应该在主线程中完成。请检查 notifydatasetchanged 方法是否在您运行应用程序时实际被注释?

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

如果没有,则将其放入您的 runonui 线程方法中。或者使用 asynctask 代替使用线程

于 2014-07-22T03:38:31.660 回答