ArrayAdapter已更改但未反映在UI

时间:2015-11-23 04:43:43

标签: android listview android-arrayadapter

我一直在使用ListView和ArrayAdapter。现在我在使用ArrayAdapter进行更改时陷入了困境。

我正在使用AsyncTack来更新UI元素,即ListView,最初在onCreate()方法中设置了ListView的适配器,但是我要在AsyncTask的onPostExecute()方法中更改数组适配器。 / p>

以下是代码

ListView mListView = (ListView)findViewById(R.id.dataListView);
vAdapter = new ArrayAdapter(getActivity(), R.layout.list_item_data,strings);
mLisstView.setAdapter(vAdapter);

的AsyncTask

public class getDataFromServer extends AsyncTask<String,Void,String[]>{


        public getDataFromServer() {
            super();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String[] doInBackground(String... parm) {
            // These two need to be declared outside the try/catch
            // so that they can be closed in the finally block.

            if(parm.length == 0){
                return null;
            }

            HttpURLConnection urlConnection = null;
            BufferedReader reader = null;

            // Will contain the raw JSON response as a string.
            String getJsonStr = null;

            try {
                final String FORECAST_BASE_URL="http://myUrlToServer?";
                final String QUERY_PARAM ="query";
                final String FORMAT_PARAM ="mode";
                final String UNITS_PARAM ="units";

                Uri baseUri = Uri.parse(FORECAST_BASE_URL).buildUpon()
                            .appendQueryParameter(QUERY_PARAM,parm[0])
                            .appendQueryParameter(FORMAT_PARAM, format)
                            .appendQueryParameter(UNITS_PARAM,units)
                            .build();

                Log.v("build Url: ",baseUri.toString());
                URL url = new URL(baseUri.toString());

                // Create the request and open the connection
                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();

                // Read the input stream into a String
                InputStream inputStream = urlConnection.getInputStream();
                StringBuffer buffer = new StringBuffer();
                if (inputStream == null) {
                    // Nothing to do.
                    return null;
                }
                reader = new BufferedReader(new InputStreamReader(inputStream));

                String line;
                while ((line = reader.readLine()) != null) {
                    buffer.append(line + "\n");
                }

                if (buffer.length() == 0) {
                    // Stream was empty.  No point in parsing.
                    return null;
                }
                getJsonStr = buffer.toString();
            } catch (IOException e) {                
                return null;
            } finally{
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (final IOException e) {
                        Log.e("Found in trouble", "Error closing stream", e);
                    }
                }
            }
            try {
                return getWeatherDataFromJson(getJsonStr,numDays);
            }catch(JSONException ex){
                ex.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String[] strings) {
            super.onPostExecute(strings);

            for(String s : strings)
                Log.v("@ onPostExecute",s);

            vAdapter = new ArrayAdapter(getActivity(), R.layout.list_item_data,strings);
            vAdapter.notifyDataSetChanged();

            /*if(strings != null){
                vAdapter.clear();
                for(String dayForecast : strings){
                    vAdapter.add(dayForecast);
                }
            }*/

        }
    }

注意:onPostExecute()方法中的注释代码也是通过逻辑正确但生成异常为UnsupportedOperationException而vAdapter.clear()

1 个答案:

答案 0 :(得分:1)

你可以这样做:

onPostExecute()应如下所示:

@Override
protected void onPostExecute(String[] strings) {
super.onPostExecute(strings);

   for(String s : strings)
      Log.v("@ onPostExecute",s);

    vAdapter.clear();
    vAdapter.addAll(strings);
    vAdapter.notifyDataSetChanged();
}

就是这样。