Skip to content Skip to sidebar Skip to footer

Update Listview Textview Vom Asyntask

i need to update a textView from my asynctask. I have an custom adapter for the listview and there i want to have a countdown for each entry. I will start the asynctask for each en

Solution 1:

If you post your code, I can give you a better answer. However, a common way to update views periodically is by using Handlers.

privatefinalHandlermHandler=newHandler(); //intialize in main threadpublicvoidtest() {
    mHandler.postDelayed(newRunnable() {

        @Overridepublicvoidrun() {
            mTextView.setText("hello");

        }
    }, 1000);
}

Solution 2:

You can do something like this (this will add an entry to a list view every one second). I have used the normal ArrayAdapter to add a string. You can use your custom adapter to do something similar. The publishProgress() method basically triggers the onProgressUpdate() method which hooks to the UI thread and displays the elements getting added.:

class AddStringTask extends AsyncTask {

@OverrideprotectedVoiddoInBackground(Void... params) {
        for(String item : items) {
            publishProgress(item);
            SystemClock.sleep(1000);
        }
        returnnull;
    }

    @OverrideprotectedvoidonProgressUpdate(String... item) {
        adapter.add(item[0]);
    }

    @OverrideprotectedvoidonPostExecute(Void unused) {
        Toast.makeText(getActivity(), "Done adding string item", Toast.LENGTH_SHORT).show();
    }
}

Post a Comment for "Update Listview Textview Vom Asyntask"