【发布时间】: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