2017-01-30 49 views
0

我試圖填充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,否則,它會出錯。

+0

只想添加文本? – josedlujan

+0

是的。所有4列轉化爲單個文本視圖 –

回答

0

做以下修改:

  1. 在你的佈局XML改變

    <TextView 
        android:id="@+id/address" 
        android:text="@string/format_address" 
        ...... 
        /> 
    
  2. 在你strings.xm添加以下內容:

    <string name="format_address">%1$s, %2$s, %3$s, %4$s</string> 
    
  3. 在你的源代碼添加以下內容:

  4. Resources res = getResources(); 
    String text = String.format(res.getString(R.string.format_address), STREET_VALUE, POSTCODE_VALUE, CITY_VALUE, STATE_VALUE); 
    

STREET_VALUE,POSTCODE_VALUE,CITY_VALUE,STATE_VALUE替換實際值或字符串REF。

+0

做了上述工作? –

+0

nope。我想我必須編輯問題以獲取更多信息。 –