我正在逐步通过从 mySQL 服务器收集数据,通过 PHP 将其转换为 JSON,使用 AQuery 异步获取 JSON,将结果转换为字符串/对象,但现在如何将字符串获取到我的 lisview 项目和在我的列表片段活动中显示它?
( PS:我知道 JSON 数据通过 Log.i() 存在,并且没有捕获 JSONException 错误)
谢谢你的输入
在我的主要活动中:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager fm = getFragmentManager();
Fragment vC = fm.findFragmentById(R.id.viewContainer);
Fragment lC = fm.findFragmentById(R.id.listContainer);
if (vC == null)
{
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.viewContainer, new WebViewFragment());
ft.commit();
}
if (lC == null)
{
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.listContainer, new NavigationListFragment());
ft.commit();
}
...
列出 Frag 活动:
public class NavigationListFragment extends ListFragment
{
Context context;
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
AsyncTasks.getJSONArray(context);// asynchronous task for getting JSON array
}
@Override
public void onSaveInstanceState(Bundle outState)
{
// TODO
}
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
// TODO
}
static int q_id;
static String label;
static String title;
static String description;
static String gotoURL;
public static void jsonListCallback(JSONArray json)
{
try
{
Log.i("NavigationListFragment", json.toString());
// Parse the string to a JSON object
for (int i = 0; i < json.length(); i++)
{
JSONObject json_data = json.getJSONObject(i);
q_id = json_data.getInt("_id");
label = json_data.getString("label");
title = json_data.getString("title");
description = json_data.getString("description");
gotoURL = json_data.getString("gotoURL");
}
} catch (JSONException e)
{
Log.e("log_tag", "Error parsing data " + e.toString());
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
final View view = inflater.inflate(R.layout.listview, container, false);
return view;
}
}
异步任务类:
public class AsyncTasks
{
public static void getJSONArrays(final Context c)
{
// initialize
final AQuery aq = new AQuery(c);
String url = "http://192.168.1.11/Andaero/php/regulatory_list.php";
// long expire = -1; TODO
aq.ajax(url, JSONArray.class, new AjaxCallback<JSONArray>()
{
public void callback(String url, JSONArray json, AjaxStatus status)
{
if (json != null)
{
NavigationListFragment.jsonListCallback(json);// Send the JSON
// Array back to
// the list
// fragment
// activity
}
switch (status.getCode())
{
case AjaxStatus.TRANSFORM_ERROR:
Log.i("GetJSONArray", "TRANSFORM_ERROR");
break;
case AjaxStatus.NETWORK_ERROR:
Log.i("GetJSONArray", "NETWORK_ERROR");
// TODO Create Alert Dialog
case AjaxStatus.AUTH_ERROR:
Log.i("GetJSONArray", "AUTH_ERROR");
break;
}
}
});
}
}
我的列表视图 XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#003399"
android:dividerHeight="1dp"
android:layoutAnimation="@anim/layout_animation_row_left_slide" />
</LinearLayout>
列表项 XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/acItem"
style="@style/listItem" >
<TextView
android:id="@+id/listLabel"
style="@style/listAcronym" />
<TextView
android:id="@+id/listTitle"
style="@style/listTitle" />
<TextView
android:id="@+id/listDiscription"
style="@style/listDiscription" />
<TextView
android:id="@+id/dummy"
style="@style/listDiscription" />
<TextView
android:id="@+id/dateAdded"
style="@style/listDates" />
</RelativeLayout>