title: How to update listview calling Asynctask to call a webservice
body: <p>I know that there is this question, but, I can't get that it works to me,  I think it could be a little different, I have a webservice to get the datas, and I have a listView in my activity. I want to update this listview.</p>
  
  <p>I have been trying with a Handler, but, it's not working,, although, I appreciate others options. I don't really know, if I should access directly to the list from my AsyncTask class or how I could update. </p>
  
  <p>UPDATE:
  So, I'm not using a nested class to my AsyncTask, it could be easier on this way, but I don't want to do it because I use this class in more places on my code. I'd like knowing how I could access to the activity and load my ListView with my datas what I get from my WS. I guess that it's a bad idea to use a Handler to do it. </p>
  
  <p>My Activity class is:</p>
  
  <pre><code>public class GameOnlineListActivity extends Activity {
  
  
  private static final String TAG = "GameOnlineListActivity";
  
  
  // List view
  private ListView lv;
  
  // Listview Adapter
  ArrayAdapter&lt;String&gt; adapter;
  
  // Search EditText
  EditText inputSearch;
  
  // ArrayList for Listview
  ArrayList&lt;HashMap&lt;String, String&gt;&gt; productList;
  
  // List with datas
  String users[];
  
  // Handler on the main (UI) thread that will receive messages from the 
  // second thread and update the progress.
  final Handler handler = new Handler() {
      public void handleMessage(Message msg) {
          // Get the current value of the variable total from the message data
          // and update the progress bar.         
          users = msg.getData().getStringArray("users");
          adapter.notifyDataSetChanged();
          }
  
  };
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_game_online_list);
      users = new String[0];
      callWebService();
      // Listview Data
  
      lv = (ListView) findViewById(R.id.list_view);
      inputSearch = (EditText) findViewById(R.id.inputSearch);
  
  
  
      // Adding items to listview
  
      adapter = new ArrayAdapter&lt;String&gt;(this, R.layout.list_item, R.id.product_name, users);
      lv.setAdapter(adapter);
  </code></pre>
  
  <p>My webservice is a little long code, because I use the same class to call several webservices,, </p>
  
  <pre><code> public class WebServiceTask extends AsyncTask&lt;String, Integer, String&gt; {
  
  
  private static final String TAG = "WebServiceTask";
  
  // connection timeout, in milliseconds (waiting to connect)
  private static final int CONN_TIMEOUT = 3000;
  
  // socket timeout, in milliseconds (waiting for data)
  private static final int SOCKET_TIMEOUT = 5000;
  
  private int taskType;
  private Context context = null;
  private Activity activity = null;
  private Handler handler;
  
  private String processMessage = "Processing...";
  
  private List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
  private ProgressDialog pDlg = null;
  
  
  public WebServiceTask(int taskType, Activity context, String processMessage, Handler h)     {
  
      this.taskType = taskType;
      this.activity = context;
      this.context = context;
      this.processMessage = processMessage;
      this.handler = h;
  
  .....
  
  private void handleResponse(String response) {
      try {
  
  
     ......
  
          case ConstantsTrivial.GET_USERS:
  
              respJSONArray = new JSONArray(response);
              listUsers = new ArrayList&lt;Usuario&gt;();
  
              for(int i=0; i&lt;respJSONArray.length(); i++)
              {               
                  JSONObject obj = respJSONArray.getJSONObject(i);                
                  id = obj.getInt(ConstantsTrivial.USER_ID);
                  userName = obj.getString(ConstantsTrivial.USER_NAME);
                  keyId = obj.getString(ConstantsTrivial.USER_KEY_ID);
                  usu = new Usuario(id, userName, keyId);
  
                  listUsers.add(usu);
  
                  String[] names = new String[listUsers.size()];
                  int z = 0;                  
                  for (Usuario u : listUsers){
                      names[z] = u.getUserName(); 
                      z++;
                  }
  
  
                  Message msg = handler.obtainMessage();
                  Bundle b = new Bundle();
                  b.putStringArray("users", names);            
                  msg.setData(b);
                  handler.sendMessage(msg);
  
  
              }
              Log.d(TAG,"Lista recuperada de Usuarios por webservice:" + listUsers);
              break;
          }
  
     } catch (Exception e) {
         Log.e(TAG, e.getLocalizedMessage(), e);
     }
  
   }
  
  @Override
  protected void onPostExecute(String response) {
  
      handleResponse(response);
  
      //pDlg.dismiss();
  
  }
  </code></pre>
  
  <p>I got a solution, but I don't really like it, and I don't want to do it like that... it's really messy.. This code is really dependent of that Activity, I'm looking for something no too much dependent because as I said,, the WebServicesTask is a class that it could call from anywhere. :(</p>
  
  <pre><code>  case ConstantsTrivial.GET_USERS:
  
              respJSONArray = new JSONArray(response);
              listUsers = new ArrayList&lt;Usuario&gt;();
              String[] names = null;
  
              for(int i=0; i&lt;respJSONArray.length(); i++)
              {               
                  JSONObject obj = respJSONArray.getJSONObject(i);                
                  id = obj.getInt(ConstantsTrivial.USER_ID);
                  userName = obj.getString(ConstantsTrivial.USER_NAME);
                  keyId = obj.getString(ConstantsTrivial.USER_KEY_ID);
                  usu = new Usuario(id, userName, keyId);
  
                  listUsers.add(usu);
  
                  names = new String[listUsers.size()];
                  int z = 0;                  
                  for (Usuario u : listUsers){
                      names[z] = u.getUserName(); 
                      z++;
                  }
  
  
              }
              Log.d(TAG,"Lista recuperada de Usuarios por webservice:" + listUsers);
              //NEW CODE, UPDATE!!!!
              ((GameOnlineListActivity)activity).adapter = new ArrayAdapter&lt;String&gt;(activity, R.layout.list_item, R.id.product_name, names);               
              ((GameOnlineListActivity)activity).lv.setAdapter(((GameOnlineListActivity)activity).adapter);
              ((GameOnlineListActivity)activity).adapter.notifyDataSetChanged();
  
              break;
  
          }
  </code></pre>