Changing a TextView at runtime from an xml file


Last Updated:

  1. chris_uk

    chris_uk Member This Topic's Starter

    Joined:
    Nov 1, 2009
    Messages:
    6
    Likes Received:
    0
    Hi folks, I'm somewhat of an android coding noob, but I thought the good folk here at androidforums.com might be able to help me or at least point me in the right direction to solve my problem.

    I'm currently trying to develop a (rather simplistic) scoring app for archery. I've made the scoring code work properly, it even puts the scores into a database and everything! I'm having trouble displaying the scores though. I want to use an activity that displays a score table, which is updated after every round is finished with a new row containing all the scores from each shot in that round, and a total column.

    I've used a TableLayout to create my table, declared in the main.xml file and I've written another xml file TableRow, which is to be added to the TableLayout each time the "finish" button is clicked. This works well. However, all I can manage at the moment is to add the row, I can't edit any of the values in it; this means I end up with a beautiful row full of "-" (which is my default string for an empty cell, so I know it's worked).

    When I try to change the values by using setText() (after using findViewById() to change the relevent text) the app force closes.

    Please help!

    Here's my code for the row to be added

    <?xml version="1.0" encoding="utf-8"?>
    <TableRow
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/row1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView
    android:layout_gravity="center"
    android:id="@+id/gap_filler"
    android:text="-"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent" />
    <TextView
    android:layout_gravity="center"
    android:id="@+id/my_textview1"
    android:text="-"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent" />
    <TextView
    android:layout_gravity="center"
    android:id="@+id/my_textview2"
    android:text="-"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent" />
    <TextView
    android:layout_gravity="center"
    android:id="@+id/my_textview3"
    android:text="-"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent" />
    <TextView
    android:layout_gravity="center"
    android:id="@+id/my_textview4"
    android:text="-"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent" />
    <TextView
    android:layout_gravity="center"
    android:id="@+id/my_textview5"
    android:text="-"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent" />
    <TextView
    android:layout_gravity="center"
    android:id="@+id/my_textview6"
    android:text="-"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent" />
    <TextView
    android:layout_gravity="center"
    android:id="@+id/total_textview"
    android:text="-"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent" />


    </TableRow>

    and here's the .java file which is supposed to change the text of the row. At the moment it's just a proof of concept program, I'll integrate it into my actual software when I get it working.

    All those declared variables are meant to be the textViews I want to change, but I haven't got one working yet so I haven't implemented changing them all.

    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TableLayout;
    import android.widget.TextView;


    public class messing extends Activity {

    private int endNo = 1;
    private int shot1 = 0;
    private int shot2 = 0;
    private int shot3 = 0;
    private int shot4 = 0;
    private int shot5 = 0;
    private int shot6 = 0;

    private int total = 0;

    private TextView tvEnd;
    private TextView tvShot1;
    private TextView tvShot2;
    private TextView tvShot3;
    private TextView tvShot4;
    private TextView tvShot5;
    private TextView tvShot6;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);

    final LayoutInflater inflater = (LayoutInflater) this.getSystemService(
    Context.LAYOUT_INFLATER_SERVICE);

    final TableLayout tl = (TableLayout) findViewById(R.id.myTableLayout);

    //This button adds a row to the table. The row is a seperate xml file.
    Button newRow = (Button) findViewById(R.id.new_row_button);
    newRow.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    inflater.inflate(R.layout.tablerow, tl);

    ;
    endNo++;
    }

    });

    //The code I'm trying to use to add text to my score row
    Button fillText = (Button) findViewById(R.id.fill_button);
    fillText.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {

    TextView peas = (TextView) findViewById(R.id.gap_filler);
    peas.setText(endNo);
    endNo++;
    }

    });
    }
    }
     

    Advertisement
  2. gauntface

    gauntface Member

    Joined:
    Nov 2, 2009
    Messages:
    11
    Likes Received:
    0
    Heya Chris,

    In terms of your problem I had a similar problem but once you find out the solution it is quite obvious how to tackle the problem.

    Sssooooo here it goes:

    When you click the finish button, you need to create/inflate a table row, creating a local variable of this table row view. From this you findViewByID on the table row i.e (This is just pseudo code to get the idea across)

    tablerow = View.Inflate(. . . . . );
    TextView tx = tablerow.findViewByID(. . . . .);

    tx.setText("Yay");

    This should be the approach you need to take. The way I'm thinking this is required is that the findViewByID method is only looking at the elements it starts with, views added onto it's structure may be viewed as seperate structures. Anyway I'm not sure how you would then go about extracting the TableRow from the TableLayout after you've added it, as I haven't played around with the TableLayout too much before.

    Hope that helps some how.

    Cheers,
    Matt
     

Share This Page

Loading...