Angela
Angela

Reputation: 33

Transfer data between EditText and Text View in different activities

I am novice for android app development. I have an issue here. In 1st activity, i have created 10 rows. Each row contain a next button which links to second activity. In 2nd activity,i have edittext field to input user details such as account name, password and etc. Each time I update my account name, when i press android back button, the row should contain the updated name. But I am not able to pass the account name to the 1st activity.

Below is my code for 1st activity:

public class AccountSetup extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account_main);
        this.initViews();
    }

    private void initViews(){ 
        TextView user1  = (TextView)findViewById(R.id.user1);
        Button iconNext1  = (Button)findViewById(R.id.iconNext1);  
        iconNext1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent1 = new Intent(AccountSetup.this, AccountSettingActivity1.class);
                Intent1.putExtra("rowid","1"); 
                startActivityForResult(Intent1, 100);
            }
        });

        Button iconNext2  = (Button)findViewById(R.id.iconNext2);       
        iconNext2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent2 = new Intent(AccountSetup.this, AccountSettingActivity2.class);
                startActivity(Intent2);
                finish();
            }
        });

        Button iconNext3  = (Button)findViewById(R.id.iconNext3);       
        iconNext3.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent3 = new Intent(AccountSetup.this, AccountSettingActivity3.class);
                //onNewIntent((Intent) v.getTag());
                Intent3.putExtra("rowid","3");
                startActivity(Intent3);
                finish();
            }
        });

        Button iconNext4  = (Button)findViewById(R.id.iconNext4);       
        iconNext4.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent4 = new Intent(AccountSetup.this, AccountSettingActivity4.class);
                //onNewIntent((Intent) v.getTag());
                Intent4.putExtra("rowid","4");
                startActivity(Intent4);
                finish();
            }
        });

        Button iconNext5  = (Button)findViewById(R.id.iconNext5);       
        iconNext5.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent5 = new Intent(AccountSetup.this, AccountSettingActivity5.class);
                //onNewIntent((Intent) v.getTag());
                Intent5.putExtra("rowid","5");
                startActivity(Intent5);
                finish();
            }
        });
        Button iconNext6  = (Button)findViewById(R.id.iconNext6);       
        iconNext6.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent6 = new Intent(AccountSetup.this, AccountSettingActivity6.class);
                //onNewIntent((Intent) v.getTag());
                Intent6.putExtra("rowid","6");
                startActivity(Intent6);
                finish();
            }
        });
        Button iconNext7  = (Button)findViewById(R.id.iconNext7);       
        iconNext7.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent7 = new Intent(AccountSetup.this, AccountSettingActivity7.class);
                //onNewIntent((Intent) v.getTag());
                Intent7.putExtra("rowid","7");
                startActivity(Intent7);
                finish();
            }
        });

        Button iconNext8  = (Button)findViewById(R.id.iconNext8);       
        iconNext8.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent8 = new Intent(AccountSetup.this, AccountSettingActivity8.class);
                //onNewIntent((Intent) v.getTag());
                Intent8.putExtra("rowid","8");
                startActivity(Intent8);
                finish();
            }
        });
        Button iconNext9  = (Button)findViewById(R.id.iconNext9);       
        iconNext9.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent9 = new Intent(AccountSetup.this, AccountSettingActivity9.class);
                //onNewIntent((Intent) v.getTag());
                Intent9.putExtra("rowid","9");
                startActivity(Intent9);
                finish();
            }
        });
        Button iconNext10  = (Button)findViewById(R.id.iconNext10);       
        iconNext10.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent10 = new Intent(AccountSetup.this, AccountSettingActivity10.class);
                //onNewIntent((Intent) v.getTag());
                Intent10.putExtra("rowid","10");
                startActivity(Intent10);
                finish();
            }
        });
    }

    @Override
    public void onBackPressed() {
        Intent i = new Intent(AccountSetup.this, WelcomeActivity.class);
        startActivity(i);
        finish();
        super.onBackPressed();
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode, Intent data) {
       if (requestCode == 100) {
           if (resultCode == RESULT_OK) {      
               String accountName1 = data.getStringExtra("accountName1");          
           }
       }
    }
}

And the following is the code for 2nd activity.

public class AccountSettingActivity1 extends Activity{

    private EditText etAccountName;
    private EditText etWanIp;
    private EditText etLocalIp;
    private EditText etPort;
    private EditText etPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account_tab_content_setting);
        this.initViews();

    }
        private void initViews(){
            etAccountName = (EditText)this.findViewById(R.id.etAccountName);
            etWanIp = (EditText)this.findViewById(R.id.etWanIp);
            etLocalIp = (EditText)this.findViewById(R.id.etLocalIp);
            etPort = (EditText)this.findViewById(R.id.etPort);
            etPassword = (EditText)this.findViewById(R.id.etPassword);

            // Assigns value
            SharedPreferences  sp = PreferenceManager.getDefaultSharedPreferences(this);
            etAccountName.setText(sp.getString("accountName1", ""));
            etWanIp.setText(sp.getString("wanIp1", ""));
            etLocalIp.setText(sp.getString("localIp1", ""));
            etPort.setText(sp.getString("port1", ""));
            etPassword.setText(sp.getString("password1", ""));

            etWanIp.setOnFocusChangeListener(new OnFocusChangeListener(){
                @Override
                public void onFocusChange(View arg0, boolean hasFocus) {
                    if(!hasFocus){
                        System.out.println("lost focus");
                        AccountSettingActivity1.this.saveSettings();
                    }
                }
            });
        }

        private void saveSettings(){
            String accountName1 = etAccountName.getText().toString();
            String wanIp1 = etWanIp.getText().toString();
            String localIp1 = etLocalIp.getText().toString();
            String port1 = etPort.getText().toString();
            String password1 = etPassword.getText().toString();

            accountName1 = (accountName1.trim().length() == 0)? "User": accountName1;
            wanIp1 = (wanIp1.trim().length() == 0)? "0.0.0.0": wanIp1;
            localIp1 = (localIp1.trim().length() == 0)? "0.0.0.0": localIp1;
            port1 = (port1.trim().length() == 0)? "8000": port1;
            password1 = (password1.trim().length() == 0)? "xxxx": password1;

            etAccountName.setText(accountName1);
            etWanIp.setText(wanIp1);
            etLocalIp.setText(localIp1);
            etPort.setText(port1);
            etPassword.setText(password1);

            SharedPreferences.Editor editor = PreferenceManager
                    .getDefaultSharedPreferences(this).edit();
            editor.putString("accountName1", etAccountName.getText().toString());
            editor.putString("wanIp1", etWanIp.getText().toString());
            editor.putString("localIp1", etLocalIp.getText().toString());
            editor.putString("port1", etPort.getText().toString());
            editor.putString("password1", etPassword.getText().toString());
            editor.commit();
        }

/*  @Override
    public void onBackPressed() {
        saveSettings();
        Intent i = new Intent(AccountSettingActivity1.this, AccountSetup.class);
        startActivity(i);
        finish();
        super.onBackPressed();
    }*/

        public void onBackPressed() {

             saveSettings();
             //final EditText Eclass1;

            EditText et = (EditText)findViewById(R.id.etAccountName);
            String s= et.getText().toString();
            Intent i = new Intent(AccountSettingActivity1.this, AccountSetup.class);
            i.putExtra("accountName1" ,s);
            setResult(RESULT_OK,  i);     
            finish();
            super.onBackPressed();
        }   

    @Override
    protected void onPause() {
        // When user leaves this tab, saves the values
        this.saveSettings();
        super.onPause();
    }
}

1st activity xml code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:AccountSetup="http://schemas.android.com/apk/res/com.example.play"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<!--     Account Toolbar -->
    <RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#324F85" >
        <Button
            android:layout_width="54dp"
            android:layout_height="30dp"
            android:layout_alignParentLeft="true"
            android:background="@drawable/ic_btn_done"
            android:layout_centerVertical="true"/>
        <TextView 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="@string/selectAccount" 
            android:textAppearance="?android:attr/textAppearanceMedium" 
            android:textSize="15dp"
            android:layout_centerHorizontal="true" 
            android:layout_centerVertical="true"
            android:gravity="center"
            android:textStyle="bold" />
        <Button
            android:id="@+id/btnAdd" 
            android:layout_width="54dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:background="@drawable/ic_btn_add_account"
            android:layout_centerVertical="true" />

        <ImageView
            android:layout_width="4dp"
            android:layout_height="30dp"
            android:layout_alignTop="@+id/btnDone"
            android:layout_toRightOf="@+id/btnDone"
            android:src="@drawable/toolbar_seperator" />

        <ImageView
            android:layout_width="4dp"
            android:layout_height="30dp"
            android:layout_alignTop="@+id/btnAdd"
            android:layout_toLeftOf="@+id/btnAdd"
            android:src="@drawable/toolbar_seperator" />
    </RelativeLayout>

        <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="15dp" 
        android:paddingRight="15dp" 
        android:background="@drawable/logo_small_white" >

        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <TableLayout
                android:id="@+id/tlStatus"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="1" >

                <TableRow
                    style="@style/tableRow"
                    android:id="@+id/row1"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical" >

                    <TextView
                        android:id="@+id/user1"
                        style="@style/textSettingLabel"
                        android:layout_weight="1" />
                    <Button
                        android:id="@+id/iconNext1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:background="@drawable/icon_next" />
                </TableRow>

                <View style="@style/tableRowBorder" />

                <TableRow
                    style="@style/tableRow"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical" >

                    <TextView
                        android:id="@+id/user2"
                        style="@style/textSettingLabel"
                        android:layout_weight="1" />
                    <Button
                        android:id="@+id/iconNext2"
                        android:layout_width="wrap_content"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:scaleType="fitEnd"
                        android:background="@drawable/icon_next" />
                </TableRow>

                <View style="@style/tableRowBorder" />

                <TableRow
                    style="@style/tableRow"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical" >

                    <TextView
                        android:id="@+id/user3"
                        style="@style/textSettingLabel"
                        android:layout_weight="1" />
                    <Button
                        android:id="@+id/iconNext3"
                        android:layout_width="wrap_content"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:scaleType="fitEnd"
                        android:background="@drawable/icon_next" />
                </TableRow>

                <View style="@style/tableRowBorder" />

                <TableRow
                    style="@style/tableRow"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical" >

                    <TextView
                        android:id="@+id/user4"
                        style="@style/textSettingLabel"
                        android:layout_weight="1" />
                    <Button
                        android:id="@+id/iconNext4"
                        android:layout_width="wrap_content"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:scaleType="fitEnd"
                        android:background="@drawable/icon_next" />
                </TableRow>

                <View style="@style/tableRowBorder" />

                <TableRow
                    style="@style/tableRow"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical" >

                    <TextView
                        style="@style/textSettingLabel"
                        android:id="@+id/user5"
                        android:layout_alignBaseline="@+id/etZone_1_1"
                        android:text="User 1" />
                    <Button
                        android:id="@+id/iconNext5"
                        android:layout_width="wrap_content"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:scaleType="fitEnd"
                        android:background="@drawable/icon_next" />
                </TableRow>

                <View style="@style/tableRowBorder" />

                <TableRow
                    style="@style/tableRow"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical" >

                    <TextView
                        style="@style/textSettingLabel"
                        android:id="@+id/user6"
                        android:layout_alignBaseline="@+id/etZone_1_1"
                        android:text="User 1" />
                    <Button
                        android:id="@+id/iconNext6"
                        android:layout_width="wrap_content"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:scaleType="fitEnd"
                        android:background="@drawable/icon_next" />
                </TableRow>

                <View style="@style/tableRowBorder" />

                <TableRow
                    style="@style/tableRow"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical" >

                    <TextView
                        style="@style/textSettingLabel"
                        android:id="@+id/user7"
                        android:layout_alignBaseline="@+id/etZone_1_1"
                        android:text="User 1" />
                    <Button
                        android:id="@+id/iconNext7"
                        android:layout_width="wrap_content"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:scaleType="fitEnd"
                        android:background="@drawable/icon_next" />
                </TableRow>

                <View style="@style/tableRowBorder" />

                <TableRow
                    style="@style/tableRow"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical" >

                    <TextView
                        style="@style/textSettingLabel"
                        android:id="@+id/user8"
                        android:layout_alignBaseline="@+id/etZone_1_1"
                        android:text="User 1" />
                    <Button
                        android:id="@+id/iconNext8"
                        android:layout_width="wrap_content"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:scaleType="fitEnd"
                        android:background="@drawable/icon_next" />
                </TableRow>

                <View style="@style/tableRowBorder" />

                <TableRow
                    style="@style/tableRow"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical" >

                    <TextView
                        style="@style/textSettingLabel"
                        android:id="@+id/user9"
                        android:layout_alignBaseline="@+id/etZone_1_1"
                        android:text="User 1" />
                    <Button
                        android:id="@+id/iconNext9"
                        android:layout_width="wrap_content"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:scaleType="fitEnd"
                        android:background="@drawable/icon_next" />
                </TableRow>

                <View style="@style/tableRowBorder" />

                <TableRow
                    style="@style/tableRow"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_weight="1"
                    android:gravity="center_vertical" >

                    <TextView
                        style="@style/textSettingLabel"
                        android:id="@+id/user10"
                        android:layout_alignBaseline="@+id/etZone_1_1"
                        android:text="User 1" />
                    <Button
                        android:id="@+id/iconNext10"
                        android:layout_width="wrap_content"
                        android:layout_gravity="right"
                        android:layout_weight="1"
                        android:scaleType="fitEnd"
                        android:background="@drawable/icon_next" />
                </TableRow>             
                <View style="@style/tableRowBorder" />              
            </TableLayout>
        </ScrollView>
    </RelativeLayout>
</LinearLayout>

2nd activity's xml code:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout 
       xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:ToolBar="http://schemas.android.com/apk/res/com.example.play"
       xmlns:TextViewMyRiadPro="http://schemas.android.com/apk/res/com.example.play"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:orientation="vertical">

       <ViewSwitcher
            android:id="@+id/switcher"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <ScrollView
                android:id="@+id/settingsView"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="@drawable/logo_small_white" >

                <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="565dp"
                    android:padding="15dp" >

                    <TextView
                        android:id="@+id/tvAccount"
                        android:text="Name"
                        android:layout_alignBaseline="@+id/etAccountName"
                        style="@style/textSettingLabel" />

                    <EditText
                        android:id="@+id/etAccountName"
                        style="@style/textSettingEdit"
                        android:layout_alignLeft="@+id/etWanIp"
                        android:inputType="text" />

                    <TextView
                        android:id="@+id/tvAccount"
                        android:layout_below="@+id/etAccountName"
                        android:text="User Account Name"
                        style="@style/textSettingHint"  />

                    <TextView
                        android:id="@+id/tvWan"
                        android:text="Wan IP"
                        android:layout_alignBaseline="@+id/etWanIp"
                        style="@style/textSettingLabel" />
                    <EditText
                        android:id="@+id/etWanIp"
                        android:layout_below="@+id/tvAccount"
                        android:layout_toRightOf="@+id/tvWan"
                        style="@style/textSettingEdit" android:inputType="text"/>
                    <TextView
                        android:id="@+id/tvWanHint"
                        android:layout_below="@+id/etWanIp"
                        android:text="Port forwarding is required in order to establish connection from the internet"
                        style="@style/textSettingHint"  />      

                    <TextView
                        android:id="@+id/tvLocal"
                        android:text="Local IP"
                        android:layout_alignBaseline="@+id/etLocalIp"
                        style="@style/textSettingLabel" />
                    <EditText
                        android:id="@+id/etLocalIp"
                        android:layout_below="@+id/tvWanHint"
                        android:layout_toRightOf="@+id/tvLocal"
                        style="@style/textSettingEdit" android:inputType="text"/>   
                    <TextView
                        android:id="@+id/tvLocalHint"
                        android:layout_below="@+id/etLocalIp"
                        android:text="The IP address of the alarm system in the local area network. 192.168.1.234 is the default address."
                        style="@style/textSettingHint" />   

                    <TextView
                        android:id="@+id/tvPort"
                        android:text="Port"
                        android:layout_alignBaseline="@+id/etPort"
                        style="@style/textSettingLabel" />
                    <EditText
                        android:id="@+id/etPort"
                        android:layout_below="@+id/tvLocalHint"
                        android:layout_toRightOf="@+id/tvPort"
                        android:inputType="number"
                        style="@style/textSettingEdit" />   
                    <TextView
                        android:id="@+id/tvPortHint"
                        android:layout_below="@+id/etPort"
                        android:text="The connection port of the alarm system. 8000 is the default port."
                        style="@style/textSettingHint" />

                    <TextView
                        android:id="@+id/tvPassword"
                        android:text="Password"
                        android:layout_alignBaseline="@+id/etPassword"
                        style="@style/textSettingLabel" />
                    <EditText
                        android:id="@+id/etPassword"
                        android:layout_below="@+id/tvPortHint"
                        android:layout_toRightOf="@+id/tvPassword"
                        style="@style/textSettingEdit" android:inputType="textPassword"/>   
                    <TextView
                        android:layout_below="@+id/etPassword"
                        android:id="@+id/tvPasswordHint"
                        android:text="Your 4 digits password to access the alarm system."
                        style="@style/textSettingHint" />
                </RelativeLayout>
            </ScrollView>           
       </ViewSwitcher>
</LinearLayout>

Upvotes: 0

Views: 501

Answers (2)

Chintan Khetiya
Chintan Khetiya

Reputation: 16162

deepdroid's Answer is true but you have one other alternative

Set public static String value=null; So , its will not depend on whole activity.

Upvotes: 0

deepdroid
deepdroid

Reputation: 633

use startActivityForResult() to start your 2nd activity

startActivityForResult(intent, requestcode)

When the second activity is finished do the following, the intent can hold the values that you need to pass back to activity1

setResult(RESULT_OK, intent)
finish();

now on Activity1, override onActivityResult()

onActivityResult()
{
  //update your textview here.
}

Upvotes: 1

Related Questions