【发布时间】:2014-09-30 13:11:08
【问题描述】:
我有一个ListFragment 我需要在其中显示列表项。但我想我无法将我的列表从AsyncTask 传递到ListFragment。这是我的代码。
FragmentTwo.java
package com.apitech.sitebilgi;
import ...
public class FragmentTwo extends ListFragment {
public FragmentTwo (){}
private ProgressDialog pDialog;
// JSON Node Names
private static final String TAG_SITELER = "Siteler";
private static final String TAG_SITE_ID = "siteid";
private static final String TAG_SITE_URL = "siteurl";
private static final String TAG_DIL = "dil";
// Siteler ListView
ListView sitelerLV = null;
// siteler JSONArray
JSONArray siteler = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> siteList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_layout_two, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
siteList = new ArrayList<HashMap<String, String>>();
sitelerLV = getListView();
new GetSiteler().execute();
}
private class GetSiteler extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Siteleriniz Yükleniyor...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
final Thread t = new Thread (new Runnable() {
@Override
public void run() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(getResources().getString(R.string.site_fetch_url) + "?");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("request", "sites"));
nameValuePairs.add(new BasicNameValuePair("userid", "24"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,
"UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
final String siteYanit = EntityUtils.toString(entity);
JSONObject jsonObj = new JSONObject(siteYanit);
siteler = jsonObj.getJSONArray(TAG_SITELER);
for (int i=0; i<siteler.length(); i++) {
JSONObject s = siteler.getJSONObject(i);
String id = s.getString(TAG_SITE_ID);
String siteurl = s.getString(TAG_SITE_URL);
String dil = s.getString(TAG_DIL);
// Temporary HashMap for Single Data
HashMap<String, String> site = new HashMap<String, String>();
// Adding each child node to Hashmap key => value
site.put(TAG_SITE_ID, id);
site.put(TAG_SITE_URL, siteurl);
site.put(TAG_DIL, dil);
// Log.d("Siteler", siteurl);
// Adding site to siteList
siteList.add(site);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.start();
return null;
}
@Override
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(getActivity(), siteList, R.layout.sitelerim_list_item,
new String[] {TAG_SITE_URL}, new int[] {R.id.sitelerim_site_adi});
setListAdapter(adapter);
}
}
}
fragment_layout_two.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal"
android:background="@drawable/bg"
>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:background="#00FF00"
android:divider="#FFCC00"
android:dividerHeight="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/list_item_title"
android:text="new text"
android:id="@android:id/empty"
android:layout_gravity="left|center_vertical"/>
</LinearLayout>
这是我的列表视图的单行 xml
sitelerim_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/sitelerim_site_button" >
<TextView
android:id="@+id/sitelerim_site_adi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:contentDescription="@string/desc_list_item_icon" />
</RelativeLayout>
这些是我的相关代码。我需要添加什么代码才能看到片段上的列表?
【问题讨论】:
-
您需要创建自己的适配器(扩展 BaseAdapter 的类),然后将您的数组列表传递给它,并在类的 getView() 方法中使用数据。 (为您的列表设置此适配器 setListAdapter(适配器))
-
你能详细说明你的答案吗?我不认为我理解它。因为我是android的初学者
-
你为什么要在
doInBackground创建一个线程?你在哪里看到的? -
我从未见过
doInBackground中的线程实现,但我之前在另一个项目的onCreate方法中使用过它,看来我也可以在这里使用它。不建议在doInBackground中使用线程吗?因为我成功获取了我的 JSON,但我无法让它在 ListFragment 中填充我的 ListView -
@jnzk2 我忘了在我的评论中提到你很抱歉
标签: android json android-asynctask fragment android-listfragment