- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我制作了一个 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/
import android.app.Activity; import android.os.Bundle; import android.app.ListActivity; import andro
我正在尝试构建 android 应用程序,它似乎将 Force Close 保持在同一位置 (setListAdapter/SimpleCursorAdapter) 现在不管我做什么。这是 DBAda
我有一个 Activity,其布局包含一个 DrawerLayout(来自支持的库)和一个 FrameLayout + ListView。在我的 FrameLayout 中,我有一个 ListFrag
这里我有一个奇怪的问题,有一种方法可以刷新我的 ListView : setListAdaptor(); 每当我从我的服务中调用此方法(以正确的方式)时,每当存在更改以更新列表时: @Overr
我尝试在 fragment 中显示 listView。但是方法 setListAdapter - 没有解决。我认为,我必须获取 listView 的 id (android.R.id.list);然后
在我的应用程序中,4 个 fragment 附加到一个 Activity 类。在我的 Activity 类中,我使用这个设置了根内容 View setContentView(R.layout.frag
我的 ListActivity 有问题,我使用一个线程来显示 ProgressDialog,其中获取所有已安装应用程序的列表。我把它变成一个列表适配器,然后我想设置 Activity 的列表适配器,但
正如标题所述,我在将我的数据从数据库链接到 ListView 时遇到了一些问题,问题在于适配器将仅设置返回的最后一行,而不是数据库表中的每一行。 我正在努力实现的一个例子: 餐 table 动物:猴猫
嗨,我正在尝试让我的 ListView 在我的 MainActivity 中使用 json。我的 onPostExecute(string result) 方法 出现错误。它给我以下错误:“MainA
我不知道发生了什么我首先在扩展 Activity 的类中发布了它。但即使将其更改为 ListActivity 后它仍然不起作用。我会把它简化到我认为问题所在的地方。 public class Pa
我正在开发一个应用程序,该应用程序(在 AsyncTask 中)执行对远程服务器的查询以获取 JSON 字符串。为了在我的 ListView 上显示数据,我扩展了一个 ArrayAdapter。 当我
我有一个 ListFragment,其数据由自定义适配器填充(在我的例子中是 SimpleAdapter)。我在扩展 ListFragment 的类中使用 notifyDataSetChanged()
我的程序具有 listArray 功能和 extends AppCompatActivity 但我的代码有错误 我的代码 新闻 Activity public class NewsActivity e
list_item.xml:http://pastebin.com/bn56L3NN 在 onCreate() 和创建 Comm 对象之后发生的情况是,我收到一条“已建立连接”消息,该消息在另一个线程
protected void onPostExecute(String file_url) { // dismiss the dialog after getting all prod
我有一个 Activity 类,其中包含一个扩展 AsyncTask 的内部 protected 类。当我单击一个按钮然后显示基于该数据的列表时,我试图让 AsyncTask 在后台加载数据。 这是我
我有一个 ListView ,它只是这样填充的: setListAdapter(new ArrayAdapter(this, android.R.layout.simp
我在 MainAvtivity 中有以下代码: private DBOperations DataDBoperation; @Override protected void onCre
当我尝试使用标题中列出的任一方法(getListView() 和 setListAdapter())时,我收到一条错误消息,指出这些方法无法解析。 这是我的代码: public void onRes
我制作了一个 ListView 来显示来自 url 的已解析 JSON 数据!相同的代码在没有 fragment 的情况下工作正常,但有 fragment 时 setListAdapter 不工作!我
我是一名优秀的程序员,十分优秀!