我做了一个列表视图,以显示从URL解析的JSON数据!相同的代码在没有片段的情况下也可以正常工作,但在使用片段的情况下,setListAdapter不起作用!我也尝试过getActivity()。setListAdapter!请检查我的代码!

public class Uploads extends SherlockFragment {

    // Progress Dialog
    private ProgressDialog pDialog;

    // Creating JSON Parser object
    JSONParser jsonParser = new JSONParser();

    ArrayList<HashMap<String, String>> uploadsList;

    // products JSONArray
    JSONArray uploads = null;

    // Inbox JSON url
    private static final String UPLOADS_URL = "http://my_url";

    // ALL JSON node names
    private static final String TAG_UPLOADS = "uploads";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_DATE = "date";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragmenttab2, container,
                false);


        // Hashmap for ListView
        uploadsList = new ArrayList<HashMap<String, String>>();

        // Loading INBOX in Background Thread
        new LoadInbox().execute();

        return rootView;
    }

    /**
     * Background Async Task to Load all INBOX messages by making HTTP Request
     * */
    class LoadInbox extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Loading Content...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        /**
         * getting Inbox JSON
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            // getting JSON string from URL
            JSONObject json = jsonParser.makeHttpRequest(UPLOADS_URL, "GET",
                    params);

            // Check your log cat for JSON reponse
            Log.d("Inbox JSON: ", json.toString());

            try {
                uploads = json.getJSONArray(TAG_UPLOADS);
                for (int i = 0; i < uploads.length(); i++) {
                    JSONObject c = uploads.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_NAME);
                    String date = c.getString(TAG_DATE);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_ID, id);
                    map.put(TAG_NAME, name);
                    map.put(TAG_DATE, "Date: " + date);

                    // adding HashList to ArrayList
                    uploadsList.add(map);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            Toast.makeText(getActivity(), "Content Loaded!", Toast.LENGTH_LONG)
                    .show();
            // updating UI from Background Thread
            getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(getActivity(),
                            uploadsList, R.layout.list_items, new String[] {
                                    TAG_NAME, TAG_DATE }, new int[] {
                                    R.id.tvUploadTitle, R.id.tvUploadDate });
                    // updating listview
                    setListAdapter(adapter);
                }
            });

        }

    }

}

最佳答案

你需要改变

 public class Uploads extends SherlockFragment




 public class Uploads extends SherlockListFragment


或者您需要在R.layout.fragmenttab2中具有一个列表视图,将其初始化,然后将适配器设置为列表视图。

09-11 02:06