将字符串绑定到TableView/TextView ID

将字符串绑定到TableView/TextView ID

问题描述:

我有一个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

我从来没有发现过我的方法是否可行,但我走了另一个方向来获得预期的结果。我最终使用的解决方案是获取特定的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); 
    }   
}