ListView update after complete AsyncTask

谁说我不能喝 提交于 2019-12-12 00:50:41

问题


I want to update TextView's text using AsyncTask.

I have list view in that there is Two TextView and one is ImageView

When user press ImageView button the Song will be played from server side. So i have put code in AsyncTask task

This is an custom Adapter from that i am calling ImageView's on Click event below is my code sample like this

     public View getView(final int position, View convertView,
            ViewGroup parent) {

     holder.imgPlay.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

and onclick event i am passing textview in asynctask, that i want to change after getting data from internet so below is my code

     new MetadataTask2().execute(holder.txtMetadata); 

now AsyncTask's Code is below

    class MetadataTask2 extends AsyncTask<TextView, Void, IcyStreamMeta> {
    TextView txtView;

    @Override
    protected IcyStreamMeta doInBackground(
            TextView... arg0) {
                    //SOME OPERATION
        txtView = arg0[0];
        return streamMeta;
    }

    @Override
    protected void onPostExecute(IcyStreamMeta result) {
            txtView.setText("Hiiiiiiii");
        }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        try {
            PD = ProgressDialog.show(CompaniesList.this, "Tuning...",
                    "Please Wait...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Now here u can see that with textView.setText("Hiii") the text will be updated but it will not reflect in Listview for that i have to update ListView or Adapter but do not know how to do that so i can see text on listview can any body help me

i have used below code for updating listview but still does nothing

      adapter.notifyDataSetChanged(); 
      mainListView.setAdapter(adapter); 
      mainListView.invalidate(); 

回答1:


Have your adapter be a member variable of your activity class. Then you will be able to reference it from onPostExecute.

Also, from your code it's not clear to me if your TextView is actuallly the one that you intend to modify (the one that you inflated presumably) just in case, remember that you have to call setContentView in the main thread before doInBackground attempts to modify any component. Make sure that this Textview variable (arg0[0]??) is the one that you are inflating




回答2:


You should implement you own ListView Adapter that will provide data to the list view. Calling notifyDataSetChanged() in PostExecute() from adapter will force list view to fetch data from the adapter. Updating list view views directly looks strange.



来源:https://stackoverflow.com/questions/14869895/listview-update-after-complete-asynctask

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