Charles Charles - 1 month ago 12
Android Question

Binding strings to TableView/TextView IDs

I have an Android activity that displays a list of log entries (using a cursor adapter and listview). When one of the entries is touched it kicks off an intent (passed with a bundle object containing the log details as strings) to another activity. The new activity is supposed to display the details in a custom TableView xml file I created, but I can not figure out how to bind the bundle strings to the id's defined in the TextView of the TableView.

I have included most my code below so you can see what I am trying to accomplish.

ViewEntry Class:

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);

}


}

Here is view_list.xml I am trying to bind to:

<?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" />

























I know what I am trying to do isn't right but hopefully it helps illustrate my intention.

Answer Source

I never did find out if my approach above is possible, but I went a different direction to get the intended results. The solution I ended up using was to get the specific textview id and then set the the text with a string by using the setText() method in the code. This is probably the accepted way to approach this design issue, but it would be nice to be able to bind strings to xml instead of manipulating it with code.

I have pasted the code below for future coders reference to a solution:

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);
    }         
}