开发者

Dynamic Xml (I think)

EDIT: Thank you all for your help. I edited my Database class to contain the following

    final EditText firstName = (EditText) findViewById(R.id.editText1);  // First Name
    final EditText middleName = (EditText) findViewById(R.id.editText2);  // Middle Name
    final EditText birthDate = (EditText) findViewById(R.id.editText4);  // Birth Date

    final String firstname = firstName.getText().toString();  // First Name
    final String middlename = middleName.getText().toString();  // Middle Name
    final String birthdate = birthDate.getText().toString();  // Birth Date

    TextView firstNameText = (TextView)findViewById(R.id.firstname);
    TextView middleNameText = (TextView)findViewById(R.id.middlename);
    TextView birthDateText = (TextView)findViewById(R.id.birthdate);

    firstNameText.setText(firstname);
    middleNameText.setText(middlename);
    birthDateText.setText(birthdate);

and my database.xml now shows

 <TableRow
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:background="#aa0000"            
        android:gravity="center_horizontal"
        android:layout_span="3"
        android:id="@+id/firstname"/>
    <TextView
        android:background="#00aa00"
        android:gravity="center_horizontal" 
        android:layout_span="3"
 开发者_如何学Go       android:id="@+id/middlename"/>
    <TextView
        android:background="#0000aa"          
        android:gravity="center_horizontal"
        android:layout_span="3"
        android:id="@+id/birthdate"/>
    </TableRow>

but when I run the emulator and I try to access that screen (by clicking a button from the previous screen) the application crashes? I set up the button correctly using the OnClickListener so I'm fairly sure the button is not the problem


If you're having trouble understanding the interface here's an example you can cut and paste.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="First Name: "
    android:id="@+id/firstLabel"
    android:layout_marginTop="14dip"
    android:layout_marginBottom="14dip"
    />
<EditText android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/firstEdit" 
    android:text="John"
    android:layout_toRightOf="@id/firstLabel"
    android:layout_alignParentRight="true"></EditText>
    <TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Middle Name: "
    android:id="@+id/middleLabel"
    android:layout_below="@id/firstLabel"
    android:layout_marginTop="14dip"
    android:layout_marginBottom="14dip"/>
<EditText android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/middleEdit" 
    android:text="Phillip"
    android:layout_below="@id/firstEdit"
    android:layout_toRightOf="@id/middleLabel"
    android:layout_alignParentRight="true"></EditText>
    <TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Last Name: "
    android:id="@+id/lastLabel"
    android:layout_below="@id/middleLabel"
    android:layout_marginTop="14dip"
    android:layout_marginBottom="14dip"
    />
<EditText android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/lastEdit" 
    android:text="Doe"
    android:layout_below="@id/middleEdit"
    android:layout_toRightOf="@id/lastLabel"
    android:layout_alignParentRight="true"></EditText>
    <TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Birthdate: "
    android:id="@+id/birthLabel"
    android:layout_below="@id/lastLabel"
    android:layout_marginTop="14dip"
    android:layout_marginBottom="14dip"
    />
<EditText android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/birthEdit" 
    android:text="08/09/1977"
    android:layout_toRightOf="@id/lastLabel"
    android:layout_below="@id/lastEdit"
    android:layout_alignParentRight="true"></EditText>


<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/birthEdit">
    <TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    <TextView
        android:background="#aa0000"            
        android:gravity="center_horizontal"
        android:layout_span="3"
        android:text="something"
        android:id="@+id/firstTable"/>
    <TextView
        android:background="#00aa00"
        android:gravity="center_horizontal" 
        android:layout_span="3"
        android:text="something1"
        android:id="@+id/middleTable"/>
    <TextView
        android:background="#0000aa"          
        android:gravity="center_horizontal"
        android:layout_span="3"
        android:text="something2"
        android:id="@+id/birthTable"/>
    </TableRow>
    </LinearLayout>
    </RelativeLayout>

DatabaseExample.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

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

        //Setup edit fields
        EditText firstEdit = (EditText)findViewById(R.id.firstEdit);
        EditText middleEdit = (EditText)findViewById(R.id.middleEdit);
        EditText lastEdit = (EditText)findViewById(R.id.lastEdit);
        EditText birthEdit = (EditText)findViewById(R.id.birthEdit);


        //Get the text and store in variables
        String firstName = firstEdit.getText().toString();
        String middleName = middleEdit.getText().toString();
        String lastName = lastEdit.getText().toString();
        String birthDate = birthEdit.getText().toString();

        //setup the text fields
        TextView firstTable = (TextView)findViewById(R.id.firstTable);
        TextView middleTable = (TextView)findViewById(R.id.middleTable);
        TextView birthTable = (TextView)findViewById(R.id.birthTable);

        //change the text fields
        firstTable.setText(firstName);
        middleTable.setText(middleName);
        birthTable.setText(birthDate);


    }
}

Once again this would be just the interface for inputting and displaying data. Instead of storing the data in String variables you would use SQLite, SharedPreferences, or write to a file on the SDCard.


Try as follows

final EditText firstName = (EditText) findViewById(R.id.editText1);  
TextView firstNameTxt = (TextView)findViewById(R.id.firstname); 
firstNameTxt.setText(firstName);


You cannot store values from the user in the strings.xml file. You must have a data store such as SharedPreferences or SQLite. Here is another link for help on data storage with Android.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜