Note that there are some explanatory texts on larger screens.

plurals
  1. POCall AsyncTask in a Fragment
    primarykey
    data
    text
    <p>I have a class that extends a Fragment. I would call my AsyncTask class in the Fragment. I doesn't obtain any error in mylogcat but I doesn't display null in my listview. Below paste the code that I use, the code is more simple, read a json file and insert its in a custom listview. This code works if change the class in Activity, but i want transform my Activity in a Fragment. </p> <p>Thanks in advance.</p> <pre><code> public class MyFragment extends Fragment { JSONParser jsonParser = new JSONParser(); ArrayList&lt;HashMap&lt;String, String&gt;&gt; uniList; JSONArray articles = null; ListView list; ListUniAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); new LoadNews().execute(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { LinearLayout ll = (LinearLayout)inflater.inflate(R.layout.uni_list, container, false); list = (ListView) ll.findViewById(android.R.id.list); return ll; } class LoadNews extends AsyncTask&lt;String, String, String&gt; { @Override protected void onPreExecute() { super.onPreExecute(); } protected String doInBackground(String... args) { List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;(); params.add(new BasicNameValuePair("s", "s")); String json = jsonParser.makeHttpRequest(Variable.URL_UNIVERSITOMETRO, "GET", params); try { uniList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); articles = new JSONArray(json); if (articles != null) { for (int i = 0; i &lt; articles.length(); i++) { JSONObject c = articles.getJSONObject(i); String title = c.getString(Variable.UNI_NAME); String voto = c.getString(Variable.UNI_VOTO); HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); map.put(Variable.UNI_NAME, title); map.put(Variable.UNI_VOTO, voto); uniList.add(map); if (isCancelled()) break; } } else { Log.d("articles: ", "null"); } } catch (JSONException e) { e.printStackTrace(); } return null; } protected void onPostExecute(String file_url) { list.setScrollingCacheEnabled(false); adapter = new ListUniAdapter(getActivity(), uniList); list.setAdapter(adapter); list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; arg0, View view, int arg2, long arg3) { } }); } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload