iSandeep
iSandeep

Reputation: 725

ArrayAdapter is changed but not reflected in UI

I have been working on ListView and ArrayAdapter. Now I got Stuck while making change in my ArrayAdapter.

I'm using an AsyncTack to update the UI element i.e. ListView, an adapter to the ListView is set in onCreate() method initially, but I'm about to change the array adapter in onPostExecute() method of AsyncTask.

Here is the code bellow

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

        }
    }

NOTE: the commented code in onPostExecute() method is also correct by means of logic but generating exception as UnsupportedOperationException while vAdapter.clear()

Upvotes: 0

Views: 254

Answers (2)

iSandeep
iSandeep

Reputation: 725

finally I found the solution. I have used array while creating adapter in onCreate() method and trying to clear the adapter as vAdapter.clear() which is not possible. so to do this I just change the array with the List

at onCreate() method

String[] mArray = {"element1","element2",.....};
List<String> mList = new ArrayList<String>(Arrays.asList(mArray));
vAdapter = new ArrayAdappter(getActivity(),R.layout.list_item_data,mList);

at onPostExecute() method

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

thank you to all.

Upvotes: 0

Hiren Patel
Hiren Patel

Reputation: 52810

You can do this way:

onPostExecute() should looks like below:

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

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

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

That's it.

Upvotes: 1

Related Questions