Update ListView Textview vom Asyntask

☆樱花仙子☆ 提交于 2019-12-25 02:47:06

问题


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 entry from my Adapter. How can i update the textview each second from the asynctask?

Thanks for help :)


回答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.

private final Handler mHandler = new Handler(); //intialize in main thread

public void test() {
    mHandler.postDelayed(new Runnable() {

        @Override
        public void run() {
            mTextView.setText("hello");

        }
    }, 1000);
}



回答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 {

    @Override
    protected Void doInBackground(Void... params) {
        for(String item : items) {
            publishProgress(item);
            SystemClock.sleep(1000);
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(String... item) {
        adapter.add(item[0]);
    }

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


来源:https://stackoverflow.com/questions/22698464/update-listview-textview-vom-asyntask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!