1

i have follow this link how to get edittext value and store it on textview its work perfectly. see this code

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.feedback);



    youButton=(Button)findViewById(R.id.btn1);

    youButton.setOnClickListener(new OnClickListener() {

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

            myEditText=(EditText)findViewById(R.id.edittext1);
            textView=(TextView)findViewById(R.id.textview3);
            String result = myEditText.getText().toString();
            textView.setText(result);

        }
    }); 
}} 

i have write my name it print my name "sanjay".second time i enter "kumar" it should be print like this

kumar
sanjay

3rd time i have write inside edittext "King". it should be print

King
Kumar
sanjay

Please help me.

Community
  • 1
  • 1
sanjay kumar
  • 71
  • 2
  • 10

4 Answers4

1

Write following code.

youButton=(Button)findViewById(R.id.btn1);
myEditText=(EditText)findViewById(R.id.edittext1);
textView=(TextView)findViewById(R.id.textview3);

youButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        String result = myEditText.getText().toString()+"\n"+textView.getText().toString();
        textView.setText(result);
    }
});

Advice

You should not initialize any view every time onClick performed. Its better to initialize before that as you have done for Button.

Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
  • Glad to help. Give Googler good notice by accept answer and up-vote if helped. :) – Chintan Rathod Jun 18 '13 at 12:56
  • hi @chintan thanks for valuable reply. but what i trying 1st time the o/p should be sanjay it will be permanently. and second time "kumar".it will appear after sanjay and any name should appear like this king after kumar then sanjay reply soon....... – sanjay kumar Jun 18 '13 at 13:03
  • @user1679432 , I changed my code in `onClick`. Just have a look. – Chintan Rathod Jun 18 '13 at 13:29
0

Try the following:

textView.setText(result.replace(" ", "\n"));
TronicZomB
  • 8,667
  • 7
  • 35
  • 50
  • This would be for when multiple names are entered at once in the EditText. ChntanRathod's answer seems to appear to answer your question best (so far). – TronicZomB Jun 18 '13 at 12:54
  • @user1679432 You're welcome, I had misread the first time thus the comment for clarification. – TronicZomB Jun 18 '13 at 13:09
  • no @TronicZomB i have entered only my name sanjay. this good but next time i have entered my surname kumar. this is single world itshould be print before sanjay – sanjay kumar Jun 18 '13 at 13:13
0

use StringBulider and append everytime when click listener called

StringBuilder sb = new StringBuilder();

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

            myEditText=(EditText)findViewById(R.id.edittext1);
            textView=(TextView)findViewById(R.id.textview3);
            String result = myEditText.getText().toString();
            sb.append(result+"\n");
            textView.setText(sb.toString());

        }
TronicZomB
  • 8,667
  • 7
  • 35
  • 50
Ashish Agrawal
  • 1,977
  • 2
  • 18
  • 32
  • but i want this king kumar sanjay 1st i have entered sanjay. this coming but after kumar the order is sanjay kumar according your code. i need sanjay should goes below every new entry........ – sanjay kumar Jun 18 '13 at 13:27
  • 1
    use this StringBuilder sb = new StringBuilder(); sb.append(""); @Override public void onClick(View v) { // TODO Auto-generated method stub myEditText=(EditText)findViewById(R.id.edittext1); textView=(TextView)findViewById(R.id.textview3); String result = myEditText.getText().toString(); String temp = sb.toString(); sb.setLength(0); sb.append(result+"\n"+temp); textView.setText(sb.toString()); } – Ashish Agrawal Jun 19 '13 at 04:42
  • hi ashish i want store o/p inside SQlite how can i do. i have read many tutorials but faild plz help...... – sanjay kumar Jun 19 '13 at 10:47
  • please firstly accept the answer by clicking on right arrow below number like in your case "0" – Ashish Agrawal Jun 19 '13 at 12:05
0

Try the following code.

String s = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            EditText e = (EditText) findViewById(R.id.editText1);
            TextView tv = (TextView) findViewById(R.id.textView1);              
            s = e.getText().toString() + "\n" + s;
            tv.setText(s);
            e.setText("");
        }
    });
}

I hope this will help you.

Gunaseelan
  • 14,415
  • 11
  • 80
  • 128