-3

I posted this question before, however i didnt explain myself good. I will have the other question removed. Here is my current situation:

I have a regular xml page with a textView which when clicked opens a popup dialog. This dialog contains 2 editText. Currently my code (OnClick – Done button) gets the value of both edit texts and puts them into the single TextView. However when i open the pop-up again, instead of the two strings being listed in its own editText (Where each string was originally inputted) the combined string which was stored in the text view appears in one edit text. The issue is that although i’m getting the strings from 2 different editText’s and storing them into one textView. I cannot get each string back individually. I understand that i may have to store the string from each editText into variables and then i can use the variables to show the strings combined in the textView (and the editText – when i open the popup dialog again) How would i go about this? Thank for your help

The code:

public class MainActivity extends Activity {
 /** Called when the activity is first created. */

    TextView showPopUpButton;
    EditText getInput;
    EditText getInput2;
    String myvalue = "";
    String myvalue2 = "";

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


  showPopUpButton = (TextView) findViewById(R.id.buttonShowPopUp);

  showPopUpButton.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    showPopUp3();              }

  });
 }




 private void showPopUp3() {


  AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
  helpBuilder.setTitle("Enter PU Builder");


  LayoutInflater inflater = getLayoutInflater();
  View checkboxLayout = inflater.inflate(R.layout.popuplayout, null);
  helpBuilder.setView(checkboxLayout);


  getInput = (EditText)  checkboxLayout.findViewById(R.id.editText1);
  getInput2 = (EditText)  checkboxLayout.findViewById(R.id.editText2);


  getInput.setText(myvalue);
  getInput2.setText(myvalue2);



  helpBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {


 @Override   
 public void onClick(DialogInterface dialog, int which) 

    {
     myvalue =  getInput.getText().toString();
     showPopUpButton.setText(myvalue + ", " + myvalue2);
    }

    });


  AlertDialog helpDialog = helpBuilder.create();
  helpDialog.show();
 }
}
manlio
  • 18,345
  • 14
  • 76
  • 126
user3247335
  • 181
  • 2
  • 5
  • 16
  • i don't get the problem , are you want to save the edit text value in variable , so try this String value = getInput.getText().toString() ; – mohammed momn Feb 19 '14 at 22:33
  • i tried that, however then becomes an issue with the onClickListener.. What exactly would I have to replace the onClick Listner method with? – user3247335 Feb 19 '14 at 22:36
  • could you post onClickListener code and what's the problem in it ? – mohammed momn Feb 19 '14 at 22:38
  • it's very messy could you edit your question with the new code ? – mohammed momn Feb 19 '14 at 22:42
  • The issue is with " showPopUpButton.setText(value); " – user3247335 Feb 19 '14 at 22:44
  • my apologies, i have changed the main code – user3247335 Feb 19 '14 at 22:47
  • what's the issue in this code ?? showPopUpButton.setText(value); – mohammed momn Feb 19 '14 at 22:55
  • yes thats the problem i have replaced - showPopUpButton.setText(getInput.getText() + ", " + getInput2.getText());//NEW.............with........showPopUpButton.setText(value) ; – user3247335 Feb 19 '14 at 22:59
  • You seemed to have made a right mess of this question also. You are clearly not ready to build apps, you seem to be trying to build an app by editing your question, one problem at a time. I have 4 recommendations. 1) Read, and learn, some basic programming tutorials. Until you understand what a variable is, what an assignment is, what a method is etc, you should forget Android. 2) After 1, do some basic Java tutorials. 3) Same for Android. 4) Learn how to write a good question. Only then can you hope to get good answers. – Simon Feb 20 '14 at 00:20
  • HI, thanks for your advice iam new to android and java. i will take your points onboard – user3247335 Feb 20 '14 at 00:44

2 Answers2

0

This is fairly straightforward.

Create the variables to placehold your strings.

String inputText;

Where applicable, get and set.

inputText = editText.getText().toString();
textView.setText(inputText);

Did I understand this correctly? Is this what you are trying to accomplish?

Matt Clark
  • 27,671
  • 19
  • 68
  • 123
  • I want to store inputText1 to a variable. Then view the variable in an editText. And also when i open the editText again the string in the variable will show. – user3247335 Feb 19 '14 at 23:01
  • I do apologize, but I still fail to understand. "I want to store iT1 to a variable." `inputText1`, is, a `String` variable. "View variable in edittext". `inputText2.setText("some text");`. It is as simple as that. – Matt Clark Feb 19 '14 at 23:07
  • Basically, when i press the 'done' button. I want the contents of the edittext to store in a variable. I can then use this variable to see the value from both the editText and the TextView – user3247335 Feb 19 '14 at 23:21
  • I apologize, my code was slightly confusing, but I fixed it up. Create the variable to place hold your text `line 1`. In the done button, assign the value `line 2`, and set it to the other text view `line 3`. You will now have the variable `inputText` with the assigned value, to use wherever else needed. – Matt Clark Feb 19 '14 at 23:25
  • im sorry could you elaborate more on the line 2 and 3 – user3247335 Feb 19 '14 at 23:33
  • not really... line 2, the variable is defined in line 1. the variable is assigned the value of the string of the editable text of your input edit text. In line 3, the text view, or the other edttext, or whatever your other container is, you set the text, the the string value, of the variable, that you defined and assigned... I really can not get any clearer. – Matt Clark Feb 19 '14 at 23:35
0

First of all you need string to save your EditText value in it declare it like this

public class MainActivity extends Activity {
  /** Called when the activity is first created. */
  TextView showPopUpButton; //NEW
  EditText getInput; //NEW
  EditText getInput2; //NEW
  // declare string to save the dialog edittext
  String myValue = "" ;

then you need to show the last value of dialog in the EditText so try this :

private void showPopUp3() {
        AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
        helpBuilder.setTitle("Enter PU Builder");
        LayoutInflater inflater = getLayoutInflater();
        View checkboxLayout = inflater.inflate(R.layout.popuplayout, null);

        getInput = (EditText)  checkboxLayout.findViewById(R.id.editText1); //MISTAKE
        getInput2 = (EditText)  checkboxLayout.findViewById(R.id.editText2); //MISTAKE
        getInput.setText(showPopUpButton.getText()); //New to keep the text in the editText when done is pressed
        getInput2.setText(getInput2.getText()); //New test

        // here set the my value to edit text  , note firs time will be empty 
        getInput.setText(myValue) 

and last thing when you click in done button in dialog you need to save the EditText value like that :

@Override   
        public void onClick(DialogInterface dialog, int which){
        //showPopUpButton.setText(getInput.getText() + ", " + getInput2.getText());//NEW
       //showPopUpButton.setText(value) ;

          // save the edit text value into myvalue string 
          myvalue =  getInput.getText().toString();
        }

        });

feed me back

mohammed momn
  • 3,230
  • 1
  • 20
  • 16
  • Sorry you have confused me. Whats going to be in the "" ? All i want is the user to be able to enter a string in a editText, when they select the Done button, it stores the string in a variable. They variable can then be used to show in the editText and the TextView. Thanks – user3247335 Feb 19 '14 at 23:22
  • "" this mean that the string will be empty first time your begin the activity but you will initialize it after the done button in dialog then if you open the dialog again you will set the text with the last value – mohammed momn Feb 19 '14 at 23:26
  • is that what are you need ? – mohammed momn Feb 19 '14 at 23:29
  • thats correct how would i do that – user3247335 Feb 19 '14 at 23:33
  • i edited my answer look on it and feed me back \ – mohammed momn Feb 19 '14 at 23:41
  • Hi thanks soo much. the string entered comes back on the editText, when the popup is open. However i have tried another i.e. myvalue2 - for some reason, it doesnt show in the textview nor the editText... So we have it working for myvalue but not myvalue2... i have updated the code above with my new code. Thanks – user3247335 Feb 20 '14 at 00:14
  • because you don't initialize myvalue2 with any value try this , myvalue2 = getInput2.getText().toString(); – mohammed momn Feb 20 '14 at 00:21
  • thank you soo much for your help.. i would vote but it doesnt allow me as i need 15 reputation. tanks anywa – user3247335 Feb 20 '14 at 00:46