0

doubt regarding my previous

show EditText on condition

I'm new to this, I'm learning. I have a question, as I can get all the data that has been inserted into the text edit? the edit text and numeric type I have, I need a way to get those values ​​and work with them, try using an array but I get errors. could help me?

my code is

package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

    public static final String TAG = MainActivity.class.getSimpleName();

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

        final EditText count = (EditText) findViewById(R.id.count);
        final Button generate = (Button) findViewById(R.id.generate);
        final LinearLayout container = (LinearLayout) findViewById(R.id.container);

        generate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int numberOfControlsToGenerate = 0;

                try {
                    numberOfControlsToGenerate = Integer.parseInt(count.getText().toString().trim());
                } catch (NumberFormatException e) {
                    Log.e(TAG, e.getMessage(), e);
                }

                if (numberOfControlsToGenerate > 0) {

                    if (container.getChildCount() > 0) {
                        container.removeAllViews();
                    }

                    for (int counter = 0; counter < numberOfControlsToGenerate; counter++) {
                        addEditText(container);
                    }
                }
            }
        });
    }

    private void addEditText(LinearLayout container) {
        final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        EditText editTextToAdd = new EditText(this);
        editTextToAdd.setLayoutParams(params);
        container.addView(editTextToAdd);
    }
}
Community
  • 1
  • 1
Yoshi
  • 19
  • 6

2 Answers2

0

You didn't understand your code.

If you tried to log out the "numberOfControlsToGenerate" you'll find immediately that you've already got your input value. And this answer works good.

Community
  • 1
  • 1
MagicFingr
  • 479
  • 6
  • 20
  • I think not explain well, I'm not trying to find the input value to form the loop, but I need to get the data that are entered on each EditText to be formed according to the loop, if 5 are generated EditText for example, need obtain the values ​​that are entered there. but not how. – Yoshi Jul 31 '14 at 04:29
0

Use this:

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

Add this code in your addEditText() method

allEditText.add(editTextToAdd );

Now to get the values of edittexts:

String[] edString = new String[(allEditText.size())];
        for(int i=0; i < allEditText.size(); i++){
               edString[i] = allEditText.get(i).getText().toString();
        }

All values of edittexts are now stored in string array edString. You can get value of specific edittext using its position starting from 0.

Hope this help.

ORIGINAL
  • 179
  • 1
  • 12