nullUser
nullUser

Reputation: 1169

AsyncTask with ListView

I am using a customListAdapter ( extends BaseAdapter) in my application. I have used this adapter inside a ListFragment.

For prototyping I have hard-coded some values in a string array and used those to populate the lists. I am overriding getView and returning the view after inflating.

Now I need to get some data from my webservice call, which I am planning to do inside an AsyncTask.

What is the recommended way to do this?

Current Code ( Pseudo )

public class customListAdapter extends BaseAdapter {

  @Override
  public View getView(int position, View MyconvertView, ViewGroup parent) {

    // Inflating view 
    // Other view operations
    return MyconvertView;
  }

  class SomeTask extends AsyncTask<params,progress ,Result > {

  @Override
  protected View doInBackground(... params) {
  }


  @Override
        protected void onPostExecute(View result) {

        }
  }
 }
}

Modification required:

Option 1:

  @Override
  public View getView(int position, View MyconvertView, ViewGroup parent) {

    // Inflating view 
    // Other view operations

    return new SomeTask.execute(); // should return the view , the onPostExecute of SomeTask should return this. 
  }

Option 2:

Please suggest.

Upvotes: 0

Views: 189

Answers (1)

SweetWisher ツ
SweetWisher ツ

Reputation: 7306

In PostExecute method of Async Task, call method

list.setAdapter(data);

So when you get all data, it will be set in list..

Upvotes: 1

Related Questions