2010-08-13 70 views
1

我有一個Android活動,顯示日誌條目列表(使用遊標適配器和列表視圖)。當其中一個條目被觸動時,它會將一個意圖(包含日誌細節的捆綁對象作爲字符串傳遞給另一個活動)啓動。新的活動應該在我創建的自定義TableView xml文件中顯示詳細信息,但我無法弄清楚如何將捆綁字符串綁定到在TableView的TextView中定義的id。將字符串綁定到TableView/TextView ID

我已經包含了大部分我的代碼,所以你可以看到我想要完成的事情。

VIEWENTRY類:

public class ViewEntry extends Activity{ 

public void onCreate(Bundle icicle) 
{ 
    super.onCreate(icicle); 
    setContentView(R.layout.view_list); 
    setTitle(R.string.view_entry_title); 
    TableView lv= (TableView)findViewById(R.id.viewlayout); 


    Bundle extras = getIntent().getExtras(); 
    if (extras != null){ 
     String date = extras.getString(plbDbAdapter.KEY_DATE); 
     String ident = extras.getString(plbDbAdapter.KEY_IDENT); 
     String type = extras.getString(plbDbAdapter.KEY_TYPE); 
     String from = extras.getString(plbDbAdapter.KEY_FROM); 
     String to = extras.getString(plbDbAdapter.KEY_TO); 
     String remark = extras.getString(plbDbAdapter.KEY_REMARK); 

     String[] from = new String[] { "date_h", "ident_h", "type_h", "from_h", "to_h", "remark_h"}; 
     int[] to = new int[] { R.id.v_date, R.id.v_ident, R.id.v_type, R.id.v_from, R.id.v_to, R.id.v_remark }; 
     ArrayAdapter details = new ArrayAdapter(this, R.layout.view_list, from, to); 
     setAdapter(details); 

     List<HashMap<String, String>> fillList = new ArrayList<HashMap<String, String>>(); 
     HashMap<String, String> map = new HashMap<String, String>(); 
        map.put("date_h", date); 
        map.put("ident_h", ident); 
        map.put("type_h", type); 
        map.put("from_h", from); 
        map.put("to_h", to); 
        map.put("remark_h", remark); 
        fillList.add(map); 
     SimpleAdapter viewadapt = new SimpleAdapter(this, fillList, R.layout.view_list, from, to); 
     lv.setAdapter(viewadapt); 

    }   

}

這裏是view_list.xml我試圖綁定到:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/viewlayout" 
    android:stretchColumns="1"> 
<TableRow> 
    <TextView 
     android:gravity="left" 
     android:text="Date:" 
     android:padding="3dip" /> 
    <TextView 
     android:id="@+id/v_date" 
     android:gravity="right" 
     android:padding="3dip" /> 

我知道我想要做的是不對的,但希望它有助於說明我的意圖。

+0

嗯,看起來像我的XML得到切斷,但你可以看到第一個ID(v_date)我想結合。 – Charles 2010-08-13 21:24:30

回答

0

我從來沒有發現過我的方法是否可行,但我走了另一個方向來獲得預期的結果。我最終使用的解決方案是獲取特定的textview id,然後使用代碼中的setText()方法將字符設置爲字符串。這可能是接受這種設計問題的可接受方式,但能夠將字符串綁定到xml而不是使用代碼來處理它會很好。

我已經粘貼下面的代碼爲未來的程序員參考的解決方案:

public class ViewEntry extends Activity{ 

    public void onCreate(Bundle icicle) 
    { 
     super.onCreate(icicle); 
     setContentView(R.layout.view_list); 
     setTitle(R.string.view_entry_title); 
     TextView tv_date = (TextView)findViewById(R.id.tv_date); 
     TextView tv_ident = (TextView)findViewById(R.id.tv_ident); 
     TextView tv_type = (TextView)findViewById(R.id.tv_type); 
     TextView tv_from = (TextView)findViewById(R.id.tv_from); 
     TextView tv_to = (TextView)findViewById(R.id.tv_to); 
     TextView tv_remark = (TextView)findViewById(R.id.tv_remark); 

    Bundle extras = getIntent().getExtras(); 
    if (extras != null){ 
     String date = extras.getString(plbDbAdapter.KEY_DATE); 
     String ident = extras.getString(plbDbAdapter.KEY_IDENT); 
     String type = extras.getString(plbDbAdapter.KEY_TYPE); 
     String from = extras.getString(plbDbAdapter.KEY_FROM); 
     String to = extras.getString(plbDbAdapter.KEY_TO); 
     String remark = extras.getString(plbDbAdapter.KEY_REMARK); 

     tv_date.setText(date); 
     tv_ident.setText(ident); 
     tv_type.setText(type); 
     tv_from.setText(from); 
     tv_to.setText(to); 
     tv_remark.setText(remark); 
    }   
}