Skip to content Skip to sidebar Skip to footer

How To Update Textviews Of List Inside AsyncTask Class Method?

Stuck with this. I have FirstAsyncTask which updates array arr[] Iam updating images using another ImageAdapter . Please see code for details I want to update each textview of Lis

Solution 1:

we can update UserInterface inside doInBackground() by using runOnUiThread concept. but this is bad practice. as sam said kindly update user inferface in onPostExe which will give smooth interaction.

else you can use runOnUiThread by

Sample.this.runOnThrea..{ public void run(){ . update ui here.} }


Solution 2:

Do it in the postExecute method of the AsyncTask class

After doInBackground(), return the object that you want to populate the list.

This will then be passed to onPostExecute() and you can initialise the list adapter there.

private class LoadList extends AsyncTask<Void, Void, List<String>> {
        @Override protected List<String> doInBackground(Void... params) {
            ArrayList<String> listContents = new ArrayList<String>();

            listContents.add("Item one");
            listContents.add("Item two");
            listContents.add("Item three");

            return listContents;
        }

        @Override protected void onPostExecute(List<String> listContents) {
            ListAdapter adapter = new SimpleAdapter(MainActivity.this, listContents,
                    R.layout.list_item, arr, new int[] { R.id.name,
                    R.id.email, R.id.mobile });

            mListView.setAdapter(adapter);
        }
    }

Post a Comment for "How To Update Textviews Of List Inside AsyncTask Class Method?"