我試圖填充sqlite記錄到列表視圖。 在我的表格中,我將一個地址分成若干列,例如Street,Postcode,City & State。 我使用SimpleCursorAdapter加載成果轉化的觀點就像下面的代碼: 這是我ListActivity.java如何結合android SimpleCursorAdapter結果?
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,否則,它會出錯。
只想添加文本? – josedlujan
是的。所有4列轉化爲單個文本視圖 –