我正在通过从mySQL服务器收集数据,通过PHP将其转换为JSON,使用AQuery异步获取JSON,将结果转换为字符串/对象,但是现在如何将字符串转换为my lisview项目并将其显示在我的列表片段活动中?
(PS:我知道JSON数据是通过Log.i()存在的,并且没有捕获到JSONException错误)
Thnx供您输入
我的主要活动:
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;
}
}
AsyncTasks类:
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;
}
}
});
}
}
我的Listview 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>
答案 0 :(得分:1)
使用FragmentManager
查找您的片段(通过id或tag),将其强制转换为NavigationListFragment
并在其上调用(非静态)方法来设置JSON对象。
假设Fragment lC = fm.findFragmentById(R.id.listContainer);
是您所指的片段,您只需执行以下操作:
NavigationListFragment navListFragment = (NavigationListFragment)lC;
navListFragment.someNonStaticMethod();