简体   繁体   中英

Android - Activity to Fragment exchange - ListAdapter

I would like to change my Activity to Fragment, but I have problem with this:

 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(
                BusTimetableActivity.this, departuresList,
                R.layout.bus_timetable_layout, new String[] { TAG_TYPE,
                TAG_HOUR }, new int[] { R.id.type, R.id.hour });

        setListAdapter(adapter);
    }

How can I exchange this part:

    ListAdapter adapter = new SimpleAdapter(
                BusTimetableActivity.this, departuresList,
                R.layout.bus_timetable_layout, new String[] { TAG_TYPE,
                TAG_HOUR }, new int[] { R.id.type, R.id.hour });

        setListAdapter(adapter);
    }

Because it works well on Activity, but when I exchange Activity to Fragment it doesn't work. How I can exchange this part to Fragment?

Because creating SimpleAdapter Adapter object from Fragment,so pass getActivity() instead of BusTimetableActivity.this as first paramter to SimpleAdapter constructor

ListAdapter adapter = new SimpleAdapter(
        getActivity(), departuresList,
          R.layout.bus_timetable_layout, new String[] { TAG_TYPE,
                TAG_HOUR }, new int[] { R.id.type, R.id.hour });

And also extend ListFragment instead of Fragment to call:

setListAdapter(adapter);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM