title: Pass Asynctask JSON result to ListFragment
body: <p>I have a <code>ListFragment</code> I need to show list items in it. But I guess I cannot pass my list from <code>AsyncTask</code> to <code>ListFragment</code>. Here is my code.</p>
  
  <p>FragmentTwo.java </p>
  
  <pre><code>package com.apitech.sitebilgi;
  import ...
  
  public class FragmentTwo extends ListFragment {
  
      public FragmentTwo (){}
  
      private ProgressDialog pDialog;
      // JSON Node Names
      private static final String TAG_SITELER = "Siteler";
      private static final String TAG_SITE_ID = "siteid";
      private static final String TAG_SITE_URL = "siteurl";
      private static final String TAG_DIL = "dil";
  
      // Siteler ListView
      ListView sitelerLV = null;
  
      // siteler JSONArray
      JSONArray siteler = null;
  
      // Hashmap for ListView
      ArrayList&lt;HashMap&lt;String, String&gt;&gt; siteList;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          return inflater.inflate(R.layout.fragment_layout_two, container, false);
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onActivityCreated(savedInstanceState);
  
          siteList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
          sitelerLV = getListView();
          new GetSiteler().execute();
  
      }
  
      private class GetSiteler extends AsyncTask&lt;Void, Void, Void&gt; {
  
          @Override
          protected void onPreExecute() {
              // TODO Auto-generated method stub
              super.onPreExecute();
  
              // Showing progress dialog
              pDialog = new ProgressDialog(getActivity());
              pDialog.setMessage("Siteleriniz Yükleniyor...");
              pDialog.setCancelable(false);
              pDialog.show();
          }
  
          @Override
          protected Void doInBackground(Void... params) {
              // TODO Auto-generated method stub
  
              final Thread t = new Thread (new Runnable() {
  
                  @Override
                  public void run() {
                          HttpClient httpclient = new DefaultHttpClient();
                          HttpPost httppost = new HttpPost(getResources().getString(R.string.site_fetch_url) + "?");
  
                          List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;();
                          nameValuePairs.add(new BasicNameValuePair("request", "sites"));
                          nameValuePairs.add(new BasicNameValuePair("userid", "24"));
  
                          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,
                                  "UTF-8"));
  
                          HttpResponse response = httpclient.execute(httppost);
                          HttpEntity entity = response.getEntity();
                          final String siteYanit = EntityUtils.toString(entity);
                          JSONObject jsonObj = new JSONObject(siteYanit);
                          siteler = jsonObj.getJSONArray(TAG_SITELER);
  
                          for (int i=0; i&lt;siteler.length(); i++) {
                              JSONObject s = siteler.getJSONObject(i);
  
                              String id = s.getString(TAG_SITE_ID);
                              String siteurl = s.getString(TAG_SITE_URL);
                              String dil = s.getString(TAG_DIL);
  
                              // Temporary HashMap for Single Data
                              HashMap&lt;String, String&gt; site = new HashMap&lt;String, String&gt;();
  
                              // Adding each child node to Hashmap key =&gt; value
                              site.put(TAG_SITE_ID, id);
                              site.put(TAG_SITE_URL, siteurl);
                              site.put(TAG_DIL, dil);
                              // Log.d("Siteler", siteurl);
                              // Adding site to siteList
                              siteList.add(site);
                          }
  
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  }
              });
              t.start();
              return null;
          }
  
          @Override
          protected void onPostExecute(Void result) {
              super.onPostExecute(result);
              // Dismiss the progress dialog
              if (pDialog.isShowing())
                  pDialog.dismiss();
              /**
               * Updating parsed JSON data into ListView
               */
              ListAdapter adapter = new SimpleAdapter(getActivity(), siteList, R.layout.sitelerim_list_item,
                  new String[] {TAG_SITE_URL}, new int[] {R.id.sitelerim_site_adi});
              setListAdapter(adapter);
          }
      }
  }
  </code></pre>
  
  <p>fragment_layout_two.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:baselineAligned="false"
      android:orientation="horizontal"
      android:background="@drawable/bg"
      &gt;
  
      &lt;ListView
          android:id="@android:id/list"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:scrollbars="vertical"
          android:background="#00FF00"
          android:divider="#FFCC00"
          android:dividerHeight="10dp" /&gt;
  
      &lt;TextView 
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textColor="@color/list_item_title"
          android:text="new text"
          android:id="@android:id/empty"
          android:layout_gravity="left|center_vertical"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>This is my single row xml for the listview</p>
  
  <p>sitelerim_list_item.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="@drawable/sitelerim_site_button" &gt;
  
      &lt;TextView 
          android:id="@+id/sitelerim_site_adi"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerVertical="true"
          /&gt;
  
      &lt;ImageView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerVertical="true"
          android:layout_alignParentEnd="true"
          android:layout_alignParentRight="true"
          android:contentDescription="@string/desc_list_item_icon" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>These are my relevant codes. What do I need to add these codes so that I can see the list on my fragment?</p>