【发布时间】:2017-01-30 13:36:36
【问题描述】:
我正在尝试将 sqlite 记录填充到 listview 中。 在我的表格中,我将地址拆分为列,例如 街道、邮政编码、城市和州。 我正在使用 SimpleCursorAdapter 将结果加载到如下代码所示的视图中: 这是我的 ListActivity.java
private DBManager dbManager;
private ListView listView;
private SimpleCursorAdapter adapter;
DbHelper myDb;
final String[] from = new String[] {
DbHelper.COL_NAME,
DbHelper.COL_STREET,
DbHelper.COL_POSTCODE,
DbHelper.COL_CITY,
DbHelper.COL_STATE };
final int[] to = new int[] { R.id.name, R.id.address };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_emp_list);
dbManager = new DBManager(this);
myDb = new DbHelper(this);
dbManager.open();
Cursor cursor = dbManager.fetch();
listView = (ListView) findViewById(R.id.list_view);
listView.setEmptyView(findViewById(R.id.empty));
adapter = new SimpleCursorAdapter(this, R.layout.activity_view_record, cursor, from, to, 0);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
这是我的看法:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/id"
android:maxLines="1"
android:padding="3dp"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="@+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/nama"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/id"
android:ellipsize="end"
android:maxLines="2"
android:padding="3dp"
android:visibility="visible" />
</RelativeLayout>
我的问题是,如何将STREET、POSTCODE、CITY、STATE组合起来放入R.id.address? 使用上述方法,我必须为每一列创建一个textview,否则会出错。
【问题讨论】:
-
只想添加文字?
-
是的。所有 4 列到单个文本视图中
标签: java android sqlite simplecursoradapter