-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); 
6
  • possible duplicate of Passing String array between two class in android application Commented Sep 19, 2013 at 8:28
  • setText gives error: The method setText(CharSequence) in the type TextView is not applicable for the arguments (String[]) Commented Sep 19, 2013 at 8:33
  • myStrings is an array, so you have to access the values as myStrings[0], myStrings[1] and so on. Commented Sep 19, 2013 at 8:36
  • I didn't understand. Where to write? Commented Sep 19, 2013 at 8:38
  • I've written an answer, so you can see an example. If you need further help just ask. :) Commented Sep 19, 2013 at 8:43

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

13 Comments

TextView displays only et.getText().toString(), et1.getText().toString() those text not displaying what im writing
You can use just a single TextView. Edited. :)
See my code. You have wrapped et1.getText().toString() beetween double quotes, like "et1.getText().toString()". You don't have to!
It's working. Is this is save to some where place. I need to get this saving data later
You can save this data in the SharedPreferences.
|
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(" ");
        } 
        }
    }); 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.