0

I am using two editText boxes like "table no" and "no of guest". if I fill the editText box and select some items in the listView then go to the next activity and come back to previous activity again where the EditText box is available the value is empty. My requirement is that when I come back to the previous activity the EditText displays the data I've entered before.

SENDING EDITTEXT DATA:

   b1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

                    //for (int i=0;i<GlobalClass.myval.length;i++){

                   //System .out.println("Clicked-->"+GlobalClass.myval[i]);

                    //}

            String tno = e1.getText().toString();
            int tn = Integer.parseInt(tno);

            Intent i=new Intent(getApplicationContext(),TicketActivity.class);
            Bundle b=new Bundle();
            b.putInt("Table No:", tn);
            i.putExtras(b);
            String et= e2.getText().toString();
            int et1 = Integer.parseInt(et);
            Bundle be=new Bundle();
            be.putInt("Guest:", et1);
            i.putExtras(be); 

            startActivity(i);



        }
    });

SECOND ACTIVITY:

   public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.ticket);


t1=(TextView)findViewById(R.id.textView3);
t2=(TextView)findViewById(R.id.textView5);
l1=(ListView)findViewById(R.id.listView1);
or=(ImageButton)findViewById(R.id.imageButton7);
ho=(ImageButton)findViewById(R.id.imageButton4);
de=(ImageButton)findViewById(R.id.imageButton1);
pl=(ImageButton)findViewById(R.id.imageButton2);
mi=(ImageButton)findViewById(R.id.imageButton3);
pa=(ImageButton)findViewById(R.id.imageButton5);
pr=(ImageButton)findViewById(R.id.imageButton6);
// l1.setItemsCanFocus(false);
//l1.setFocusable(true);
//l1.setClickable(true);

//Get the Table no Value From Edit Text


Intent i1=getIntent();
Bundle b=i1.getExtras();
int num=b.getInt("Table No:");
pno=Integer.toString(num);
t1.setText(pno);

//Get The Guest Value From Edit Text
 Intent i2=getIntent();
 Bundle b1=i2.getExtras();
 int num1=b1.getInt("Guest:");
 pno1=Integer.toString(num1);
 t2.setText(pno1);




// List<String> st=GlobalClass.myval;

   //ArrayAdapter<String> ada = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1,st);

    l1.setAdapter(new EfficientAdapter(TicketActivity.this));

     l1.setOnItemClickListener(new OnItemClickListener(){
 public void onItemClick(AdapterView<?> parent,View view,int position,long id){

 st2=HomeActivity.select1[position];

    Toast.makeText(getApplicationContext(), "Selected:" + st2, Toast.LENGTH_SHORT).show();

    }
    });

 //GO TO PREV ACTIVITY:


    ho.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        Intent o2=new Intent(TicketActivity.this,HomeActivity.class);

        startActivity(o2);

    }
  });

      }

    }
USER5762
  • 277
  • 5
  • 14
  • 1
    use "putExtra" method of the intent when you start the under acitivity: http://stackoverflow.com/questions/5265913/how-to-use-putextra-and-getextra-for-string-data – Bruno Bieri Sep 04 '12 at 06:38
  • OK I ALSO USED THIS. BUT I CAME BACK TO PREV ACTIVITY, THEN THE DATAS ARE CLEARED – USER5762 Sep 04 '12 at 06:45
  • 1
    Try to see [this](http://stackoverflow.com/questions/12226761/using-data-fetched-in-one-activity-in-another-activity/12226967#12226967):) similarly answers your scenario – Manoj Kumar Sep 04 '12 at 06:45
  • 1
    you have to use getExtras when you came back to the previous activity – Bruno Bieri Sep 04 '12 at 06:48

3 Answers3

4

In the calling activity

Intent i = new Intent(A.this, B.class);
i.putExtra("string", editText.getText());
startActivity(i);

In the called activity, have a String to get this extra and pass it again to when onStop()

Bundle extras = getIntent().getExtras();
String str = extras.getString("string");

In home activity :

public void onCreate(Bundle savedInstanceState){
....
  if(getIntent().getExtras()!=null) {
     Bundle extras = getIntent().getExtras();
     if(extras.getInt("TableNo") != null && extras.getInt("Guest") != null) }
        e1.setText(extras.getInt("TableNo").toString());
        e2.setText(extras.getInt("Guest").toString());
     }
  }
....
}


   b1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

                //for (int i=0;i<GlobalClass.myval.length;i++){

               //System .out.println("Clicked-->"+GlobalClass.myval[i]);

                //}

        String tno = e1.getText().toString();
        int tn = Integer.parseInt(tno);
        String et= e2.getText().toString();
        int et1 = Integer.parseInt(et);

        Intent i=new Intent(getApplicationContext(),TicketActivity.class);
        i.putExtras("TableNo", tn);
        i.putExtras("Guest", et1); 

        startActivity(i);



    }
});

SECOND ACTIVITY:

   public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.ticket);


t1=(TextView)findViewById(R.id.textView3);
t2=(TextView)findViewById(R.id.textView5);
l1=(ListView)findViewById(R.id.listView1);
or=(ImageButton)findViewById(R.id.imageButton7);
ho=(ImageButton)findViewById(R.id.imageButton4);
de=(ImageButton)findViewById(R.id.imageButton1);
pl=(ImageButton)findViewById(R.id.imageButton2);
mi=(ImageButton)findViewById(R.id.imageButton3);
pa=(ImageButton)findViewById(R.id.imageButton5);
pr=(ImageButton)findViewById(R.id.imageButton6);
// l1.setItemsCanFocus(false);
//l1.setFocusable(true);
//l1.setClickable(true);

//Get the Table no Value From Edit Text


Intent i1=getIntent();
Bundle b=i1.getExtras();
int num=b.getInt("TableNo"); 
pno=Integer.toString(num);
t1.setText(pno);

//Get The Guest Value From Edit Text
 Intent i2=getIntent();
 Bundle b1=i2.getExtras();
 int num1=b1.getInt("Guest");
 pno1=Integer.toString(num1);
 t2.setText(pno1);




// List<String> st=GlobalClass.myval;

   //ArrayAdapter<String> ada = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1,st);

    l1.setAdapter(new EfficientAdapter(TicketActivity.this));

     l1.setOnItemClickListener(new OnItemClickListener(){
 public void onItemClick(AdapterView<?> parent,View view,int position,long id){

 st2=HomeActivity.select1[position];

    Toast.makeText(getApplicationContext(), "Selected:" + st2, Toast.LENGTH_SHORT).show();

    }
    });

 //GO TO PREV ACTIVITY:


    ho.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        String tno = t1.getText().toString();
        int tn = Integer.parseInt(tno);
        String et= t2.getText().toString();
        int et1 = Integer.parseInt(et);

        Intent o2=new Intent(TicketActivity.this,HomeActivity.class);
        i.putExtras("TableNo", tn);
        i.putExtras("Guest", et1);
        startActivity(o2);

    }
  });

      }

    }

Also, try to name your variables logically. Naming randomly is not a good programing practice and may hamper code maintainability.

Note: Please rectify some small errors(if any). I dont have android sdk right now so can not compile this code.

harshit
  • 3,788
  • 3
  • 31
  • 54
  • Try the change made. Also, if you have problem, upload your code form second activity – harshit Sep 04 '12 at 07:50
  • 1
    When you are going back to your home activity, and passing new intent o2, don't forget to put in the extras to retreive it in home activity(retrival is to be done in the same way it is done here). Also before retrieving, don't forget to check that these extras exist or not, because your home activity can be started from launcher as well as this activity. If you have more problem, feel free to ask. – harshit Sep 04 '12 at 09:25
  • PLEASE MENTION IN MY CODE.NW AM CONFUCED – USER5762 Sep 04 '12 at 09:34
  • Sorry it was onCreate()... try now – harshit Sep 04 '12 at 12:01
  • nw i have resource not found exception in this line\\ e1.setText(extras.getInt("TableNo").toString());\\ – USER5762 Sep 04 '12 at 12:55
  • If your question is resolved, please mark the answer and close the question – harshit Sep 05 '12 at 06:18
  • which one... can please state again – harshit Sep 07 '12 at 07:27
2

Save the EditText field's value when going to next activity. Then retrieve the value when you came back to previous activity. Two steps:

  1. You need to override onSaveInstanceState(Bundle savedInstanceState) and save EditText field's value:

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState)
    {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString("MyString", "EditText value");
    }

  2. The Bundle is essentially a way of storing a NVP ("Name-Value Pair") map, and it will get passed in to onCreate and also onRestoreInstanceState where you'd extract the values like this:

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState)
    {
    super.onRestoreInstanceState(savedInstanceState);
    String myString = savedInstanceState.getString("MyString");
    }

You'd usually use this technique to store instance values for your application (selections, unsaved text, etc.).

Answer courtesy: Saving Android Activity state using Save Instance State

Community
  • 1
  • 1
User
  • 126
  • 1
  • 3
1

Declare one Class and declare one static variable and store value in static variable Like this

public class globalconstant 
{ 
  public static String edtStr; 
}

Store value

globalconstant.edtStr = edt.getText().toString();

And when u want to use just call globalconstant.edtStr

Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95