-2

I'm just starting to learn android app development as well as java, and I'm a bit confused how to get this to work. I created user input fields in the UI with the following tablelayout:

    public void inputRow( TableLayout tl, String label, int inputSize, int inputID )
    {
        TableRow inputRow = new TableRow(this);
        TextView tv = new TextView(this);
        EditText edit = new EditText(this);

        // some margin
        inputRow.setPadding( 20,10,20,0);
        tv.setText(label);
        edit.setMaxWidth( inputSize*7)  ;
        edit.setMinimumWidth(inputSize*7);
        edit.setId( inputID );
        edit.setGravity(Gravity.RIGHT);
        inputRow.addView(tv);
        inputRow.addView(edit);

        tl.addView(inputRow);
    }

I then filled out the inputRows with the following:

inputRow( myTableLayout, "Name", 30, 10000);
inputRow( myTableLayout, "Last Name", 20, 10001);
inputRow( myTableLayout, "Age", 3, 10002);
inputRow( myTableLayout, "Street Address", 20, 10003);
inputRow( myTableLayout, "State", 15, 10004);

Created the button

 Button btAccept = new Button(this);
        btAccept.setText( "Save");
        btAccept.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v)
            {

                alertDialog.show();

            }
        });

And the alert dialogue:

final AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Save");
alertDialog.setMessage("Saved..");

Rather than just displaying "Saved.." in the alert dialogue, how do I get it to display the actual info the user submitted once they click the button? While I have the id's set for each input row, I'm unsure how to actually retrieve them. Would it make the most sense for me to also place all the input within an aray to then display?

EDIT I've been searching online for possible solutions and thought maybe I need to look at each TableRow from myTableLayout.

TableRow row1 = (TableRow)myTableLayout.getChildAt(0);
TextView tv1 = (TextView) row1.getChildAt(0);
String input = tv1.getText().toString(); 

I figured that maybe I would be able to access the first row from my table to retrieve the textview output. However it still doesn't work. I really thought I could perhaps access each line with the id I've set with each editText, but nothing I'm trying works.

trungnt
  • 1,111
  • 1
  • 7
  • 19

2 Answers2

1

Create an ArrayList of EditText in your class and put editText objects in it while creating row.

List<EditText> editTextList = new ArrayList<>();

inside inputRow(.....) method

//your code
EditText edit = new EditText(this);
editTextList.add(edit);

Get Text of every input from arrayList and display it in alert dialog:

String message = "Saved: ";
for(EditText edit:editTextList) {
   message+=edit..getText().toString()+" ";
}
alertDialog.setMessage(message);
Shailendra Singh Rajawat
  • 8,172
  • 3
  • 35
  • 40
  • When I filled out the fields and pressed "save", it only displays "save: " I still don't see see any of the user input. This makes sense to me though. I can constantly add each edittext within the ArrayList and add each entry into method. http://pastebin.com/gLmDzkbs – trungnt Mar 05 '17 at 03:05
  • I just tried putting the for loop into onClick(view v) for "accept" because that should capture that particular moment. Still nothing :/ – trungnt Mar 05 '17 at 03:30
  • 1
    Nevermind, I finally got it to work. I needed to move the alertDialogue into the button. Thanks so much for the help! – trungnt Mar 05 '17 at 06:11
0

Have you Tried to change Saved.. with

alertDialog.setMessage("Name, 30, 10000");

or any other way you need to display them?

  • That's not what I'm looking to have displayed though. That would just show "Name, 30, 1000" on screen once I press the save button. I need to actually have it display user input. There's fields in the app that requires the user to fill out. In this case, it'll be Name, Last Name, Age, Street and State. Let's say the user fills out Sally, Jane, 23, 30 Westchester St, CA. Once the user presses the save button, I want it to display all of those. – trungnt Mar 04 '17 at 00:59
  • you can use `obj.getText()` to get data user fill out with and pass them to `alertDialog.setMessage( param1 , param2 , param3 );` –  Mar 04 '17 at 01:02
  • While I basically understand what you're saying, I don't think I'm doing it right. Is there a way for me to retrieve the text displayed from the input for "Name" using its inputID? I was told to set an ID which I did (10000 for name) yet I don't know how to utilize it. – trungnt Mar 04 '17 at 01:42
  • i think you need to read [this](http://stackoverflow.com/questions/7200108/android-gettext-from-edittext-field) it will help you understand how to get text user input –  Mar 04 '17 at 02:07
  • From my understanding that post has suggestions to use findViewById(int, but according to documentations I'm reading, that finds a view that was identified by the id attribute from the XML. I'm not using XML however and just trying to program it with Java. EditText text = (EditText) findViewById(R.id.10000); String value = text.getText().toString(); doesn't work – trungnt Mar 04 '17 at 02:47
  • if you mean that you built your UI using java only .. this mean you don't need to use `findViewById()` you just do `getText()` on your variable of EditText used to build your UI –  Mar 04 '17 at 16:02
  • it's ok ^_^ waiting you <3 –  Mar 04 '17 at 20:51