【问题标题】:List from MySQL via JSON inside a parent layout?在父布局中通过 JSON 从 MySQL 列出?
【发布时间】:2015-11-16 02:13:02
【问题描述】:

我是 android 开发的初学者,甚至不确定我在这里问的是否正确,但它就在这里。

我有一个活动(扩展 ListActivity),它使用 JSON 生成基于 MySQL 数据库的列表。它生成的表格使用一个布局文件作为在屏幕上生成数据库的模板。

这是活动中的相关部分:

private void ReadDataFromDB() {

PD.show();
JsonObjectRequest jreq = new JsonObjectRequest(Method.GET, url, null,
new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
try {
int success = response.getInt("success");

if (success == 1) {
JSONArray ja = response.getJSONArray("orders");

for (int i = 0; i < ja.length(); i++) {

JSONObject jobj = ja.getJSONObject(i);
HashMap<String, String> item = new HashMap<String, String>();
item.put(ITEM_SCAN, jobj.getString(ITEM_SCAN));
item.put(ITEM_PRODUCT,
jobj.getString(ITEM_PRODUCT));

Item_List.add(item);

} // for loop ends

String[] from = { ITEM_SCAN, ITEM_PRODUCT };
int[] to = { R.id.item_scan, R.id.item_product };

adapter = new SimpleAdapter(
getApplicationContext(), Item_List,
R.layout.list_items2,from, to);

setListAdapter(adapter);


PD.dismiss();

}

这里是相关的 XML (list_items2):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp">

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/item_scan"
android:layout_column="10"
android:textSize="30sp"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/item_product"
android:layout_column="11"
android:textSize="30sp" />

</TableRow>
</TableLayout>
</LinearLayout>

活动本身没有布局(没有 setContentView)...我相信它使用 XML 文件作为模板,这就是它允许它随着相关 MySQL 表的增长而增长的原因。

如何将所有这些都放在一个母版布局中以使其与我的其他活动保持一致?我会使用 FrameLayout 吗?我所有的尝试都产生了奇怪的结果。事实上,这段代码只产生了一个由列表填充的白色背景。

提前致谢。

【问题讨论】:

    标签: android android-layout android-activity methods


    【解决方案1】:

    我相信您使用的是ListActivity

    这是一个实用程序类,与 Android 中的许多其他类一样。它的目的是为您提供一种非常简单的方法来创建某个 Android 概念的原型。这是一个知道它将处理 ListView 甚至定义默认布局的活动。因此,您不需要setContentView

    你在这里做的另一件事:

    adapter = new SimpleAdapter(getApplicationContext(), Item_List, R.layout.list_items2,从,到);

    adpaters 是将您的对象转换为您的列表视图的对象。您提供的最后两个参数告诉 Android API 您的对象和关联的视图 ID 之间的对应关系。基于此,Android 会自动进行映射。

    构建此映射器(适配器)后,所有内容都在此行之后可视化:setListAdapter(adapter);

    我写了所有这些,只是为了让您更好地理解代码中发生的事情。

    那么你需要做什么:

    • 最简单的方法是将ListActivity 更改为ListFragment,您将获得完全相同的功能,但在片段中。
    • 当然,您需要定义一个 Activity 以将该片段保存在 FrameLayout 中,就像您对所有其他片段所做的一样(或者,您也可以重用现有的 Activity 替换其片段)。

    但是,这仍然会使您无法控制自己拥有的东西。如果在某些时候您想要更改片段的默认行为,则需要一些额外的步骤。

    • ListFragment 更改为 Fragment(我相信您在所有其他情况下都会使用它们)。
    • 为片段定义您自己的布局。在此布局中添加单个元素 - ListView。将此布局设置为片段的内容视图(现在您将需要它,因为您不使用实用程序 API)
    • ListView 定义一个@+id。从主代码中选择列表视图并设置其适配器:listView.setAdapter(adapter)

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2012-02-17
      • 2015-03-06
      • 1970-01-01
      • 1970-01-01
      • 2013-07-26
      • 2020-12-04
      相关资源
      最近更新 更多