-2

I have multiple edittext boxes and on the click of the "Save" button,it should be able to get text and retrieve it when the app opens again.

The code for the xml file is as follows:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="false"
                android:focusable="false"
                android:gravity="center"
                android:height="50dip"
                android:text="@string/TabletName"
                android:textColor="@color/white"
                android:textStyle="bold"
                android:width="200dip" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:clickable="false"
                android:focusable="false"
                android:gravity="center"
                android:height="50dip"
                android:text="@string/Quantity"
                android:textColor="@color/white"
                android:textStyle="bold"
                android:width="120dip" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/editText1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="@string/EnterNameHere"
                android:inputType="text"
                android:textColor="@color/black"
                android:width="200dip" >

                <requestFocus />
            </EditText>

            <EditText
                android:id="@+id/editText10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="@string/EnterQuantity"
                android:inputType="number"
                android:textColor="@color/black"
                android:width="120dip" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/editText3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="@string/EnterNameHere"
                android:inputType="text"
                android:textColor="@color/black"
                android:width="200dip" />

            <EditText
                android:id="@+id/editText4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="@string/EnterQuantity"
                android:inputType="number"
                android:textColor="@color/black"
                android:width="120dip" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="160dip"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/editText5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="@string/EnterNameHere"
                android:inputType="text"
                android:textColor="@color/black"
                android:width="200dip" />

            <EditText
                android:id="@+id/editText6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="@string/EnterQuantity"
                android:inputType="number"
                android:textColor="@color/black"
                android:width="120dip" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/editText7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="@string/EnterNameHere"
                android:inputType="text"
                android:textColor="@color/black"
                android:width="200dip" />

            <EditText
                android:id="@+id/editText8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="@string/EnterQuantity"
                android:inputType="number"
                android:textColor="@color/black"
                android:width="120dip" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/editText9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="@string/EnterNameHere"
                android:inputType="text"
                android:textColor="@color/black"
                android:width="200dip" />

            <EditText
                android:id="@+id/editText2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="@string/EnterQuantity"
                android:inputType="number"
                android:textColor="@color/black"
                android:width="120dip" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/editText11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="@string/EnterNameHere"
                android:inputType="text"
                android:textColor="@color/black"
                android:width="200dip" />

            <EditText
                android:id="@+id/editText12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="@string/EnterQuantity"
                android:inputType="number"
                android:textColor="@color/black"
                android:width="120dip" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/editText13"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="@string/EnterNameHere"
                android:inputType="text"
                android:textColor="@color/black"
                android:width="200dip" />

            <EditText
                android:id="@+id/editText14"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="@string/EnterQuantity"
                android:inputType="number"
                android:textColor="@color/black"
                android:width="120dip" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/editText15"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="@string/EnterNameHere"
                android:inputType="text"
                android:textColor="@color/black"
                android:width="200dip" />

            <EditText
                android:id="@+id/editText16"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="@string/EnterQuantity"
                android:inputType="number"
                android:textColor="@color/black"
                android:width="120dip" />

        </TableRow>

        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/Save"
            android:textColor="@color/white" />

    </TableLayout>

The code in the java file is as follows:

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class TabletNotifier extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen2);              
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_screen2, menu);
        return true;
    }
}

I'm using the Eclipse IDe and I'm also fairly new to android,so any answers will be appreciated.. Thanks :)

Ray
  • 21
  • 2
  • 3

2 Answers2

2

First get the values from EditText and AppPreferences class as:

AppPreferences appPrefs;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    appPrefs=new AppPreferences(this);

    EditText editMessage=(EditText)findViewById(R.id.youredittext);
    String messageString1=editMessage.getText().toString();

    //so as for other too

}

And AppPreferences.java is:

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

public class AppPreferences {
    private SharedPreferences appSharedPrefs;
    private Editor prefsEditor;

    public AppPreferences(Context context) {
        this.appSharedPrefs = PreferenceManager
        .getDefaultSharedPreferences(context);
        this.prefsEditor = appSharedPrefs.edit();
    }

    public String getValue(String text) {
        return appSharedPrefs.getString(text, "");
    }

    public void setValue(String key,String text) {
        prefsEditor.putString(key, text);
        prefsEditor.commit();
    }
}

Then on save button :

appPrefs.setValue("message1",messageString1);
//so as save others too

Then you can retrive those values at anytime like:

appPrefs.getValue("message1")
//so as retrive other values 
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
captaindroid
  • 2,868
  • 6
  • 31
  • 45
0

Android allows you to store persistent information about an Activity using a Bundle. This Bundle is provided to you when the activity is launched again as a parameter in the onCreate method. You can have a look at this answered question:

What's onCreate(Bundle savedInstanceState)

Update: if you want persistent storage, then you will have to use a SharedPreferences object, or a SQLite database. You can read more on the Android Developer website.

Community
  • 1
  • 1
Moystard
  • 1,827
  • 2
  • 17
  • 18