我制作了一个列表视图,以显示来自网址的已解析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);
}
});
}
}
}
答案 0 :(得分:2)
您需要更改
public class Uploads extends SherlockFragment
要
public class Uploads extends SherlockListFragment
或者您需要在R.layout.fragmenttab2
中使用listview初始化它,然后将适配器设置为listview。