0

そのため、リスト ビューをスレッド内から動的に構築して、非同期で動作するようにしています。そして、Google 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();
    }


}

オンリフレッシュ

            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

use asynctask instead of thread it is very easy to use. ui related chages should be done in main thread . please check whether notifydatasetchanged method are actually commented while you are running your app ?

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

if not then put it into your runonui thread method.or instead of using thread use asynctask

于 2014-07-22T03:38:31.660 に答える