gpt4 book ai didi

android - setListAdapter 不适用于 Android 中的 Sherlock Fragment

转载 作者:行者123 更新时间:2023-11-29 21:24:14 26 4
gpt4 key购买 nike

我制作了一个 ListView 来显示来自 url 的已解析 JSON 数据!相同的代码在没有 fragment 的情况下工作正常,但有 fragment 时 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 中有一个 listview 初始化它,然后将适配器设置为 listview。

关于android - setListAdapter 不适用于 Android 中的 Sherlock Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20358731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com