-3

I have more than 3 edittext. When i enter something inside edittext i need to save this to another screen. I referred some question got answer. But how to pass through array. I used separate putExtra method of each edittext and another screen i need to display one TextView. Now i created separate TextView for each.

code:

Activity:

 et=(EditText)findViewById(R.id.et);

            et1=(EditText)findViewById(R.id.et1);

            btn=(Button)findViewById(R.id.btn);

            btn.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    Intent intent = new Intent(Save.this, Get.class);
                String[] myStrings = new String[] {"et.getText().toString()", "et1.getText().toString()"};
                intent.putExtra("strings", myStrings);
                startActivity(intent);


SharedPreferences preferences = getDefaultSharedPreferences(this);
                  SharedPreferences.Editor editor = preferences.edit();
                  editor.putString("Name","test");
                  editor.commit();

                    }


            });

Activity1:

    Intent intent = getIntent();
              String[] myStrings = intent.getStringArrayExtra("strings");

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
          String name = preferences.getString("Name","");


              txt=(TextView)findViewById(R.id.txt);
                txt.setText(myStrings); 
user2674668
  • 117
  • 2
  • 13

2 Answers2

2

I'll write a complete answer.

Intent intent = new Intent(Save.this, Get.class);
String[] myStrings = new String[] { et.getText().toString() ,  et1.getText().toString() };
intent.putExtra("strings", myStrings);
startActivity(intent);

and then

Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("strings");

txt1=(TextView)findViewById(R.id.txt1);
txt1.setText(myStrings[0]);

txt2=(TextView)findViewById(R.id.txt2);
txt2.setText(myStrings[1]); 

or you can just join the strings and pass it to a single TextView

Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("strings");

String joined = myStrings[0] + " - " + myStrings[1];

txt=(TextView)findViewById(R.id.txt);
txt.setText(joined);

Hope this will help.


Usually I write a class that will handle the SharedPrenferences with some static method, like:

public class Storage {

    public static String getName(Context context) {
        final SharedPreferences prefs = context.getSharedPreferences("com.my.package", Context.MODE_PRIVATE);
        return prefs.getString("name", "");
    }

    public static void setName(Context context, String name) {
        final SharedPreferences prefs = context.getSharedPreferences("it.enrichman.bolloauto", Context.MODE_PRIVATE);
        prefs.edit().putString("name", name).commit();
    }

}

You can try this storing it the first time in your first activity and then retrive it in the second one. Use a Toast to test.

Enrichman
  • 11,157
  • 11
  • 67
  • 101
  • TextView displays only et.getText().toString(), et1.getText().toString() those text not displaying what im writing – user2674668 Sep 19 '13 at 08:51
  • You can use just a single TextView. Edited. :) – Enrichman Sep 19 '13 at 08:51
  • See my code. You have wrapped `et1.getText().toString()` beetween double quotes, like `"et1.getText().toString()"`. You don't have to! – Enrichman Sep 19 '13 at 08:52
  • It's working. Is this is save to some where place. I need to get this saving data later – user2674668 Sep 19 '13 at 08:59
  • You can save this data in the SharedPreferences. – Enrichman Sep 19 '13 at 09:18
  • Yes, you have just to save it. http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values – Enrichman Sep 19 '13 at 09:48
  • It's not working. Can you edit your answer with SharedPreferences and intent – user2674668 Sep 19 '13 at 10:02
  • Why are you saying that is not working? Have you tried to load them? – Enrichman Sep 19 '13 at 10:35
  • see my edited question. I used above code for load to another activity – user2674668 Sep 19 '13 at 10:41
  • You're keeping the array instead of the loaded string.. `txt.setText(myStrings);`. Change it to `txt.setText(name);`............. – Enrichman Sep 19 '13 at 10:44
  • NullPointerException on SharedPreferences.Editor editor = preferences.edit(); – user2674668 Sep 19 '13 at 11:00
  • You probably should go a bit deeper onn JAVA before Android... It means that preferences is null but without your code is difficult to see where... – Enrichman Sep 19 '13 at 11:06
  • The problem is probably on your `getDefaultSharedPreferences(this);`. Never use `this` because it's not always the Context. In your case it's referring to the OnClickListener class. Use `Activity.this` instead (with ActivityName.this of course). `getDefaultSharedPreferences(ActivityName.this);` – Enrichman Sep 19 '13 at 11:19
0

This code is working properly.

    scrollview=(ScrollView)findViewById(R.id.scrollview1); 
    tb2.setTextSize(30); 
    tb2.setMovementMethod(new ScrollingMovementMethod());
    scrollview.post(new Runnable() { 
    public void run() { 
        scrollview.fullScroll(View.FOCUS_DOWN);  
        }
    }); 
    chat1.setOnClickListener(new OnClickListener() { 
        public void onClick(View v) { 
         {
             textsaring1=edt1.getText().toString();
             tb2.setText(tb2.getText()+" "+textsaring1);
             edt1.setText(" ");
        } 
        }
    }); 
}
Amitsharma
  • 1,577
  • 1
  • 17
  • 29