Max
Max

Reputation: 21

How to show user input from dynamic created EditTexts in TextViews into another activity?

In main activity I have button which onClick method creates EditText every time when user clicks this button. I wanted to pass user input from each EditText to TextView into another activity. But TextView shows user input only from last EditText. My code may look strange, because I tried to solve problem on my own. How can I show user input from each created EditText? I am new to programming. Thank you in advance.

Java code

EditText code

int i=1;
int numberOfCreated = 2;
public void buttonCreate(View view){     // button onClick method
    for(i=1;i<numberOfCreated;i++){
        LinearLayout createdEditText1Layout=findViewById(R.id.createdEditText1Layout);
        editText1 = new EditText(this);
        LinearLayout.LayoutParams paramsForEditText1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        editText1.setLayoutParams(paramsForEditText1);
        editText1.setInputType(InputType.TYPE_CLASS_TEXT);
        editText1.setHint(R.string.editText1Hint);
        editText1.setTextSize(16);
        createdEditText1Layout.addView(editText1);
    }
}

Intent to pass string to second activity

    public void openResult() {
    String productName = editText1.getText().toString();
    Intent intent2 = new Intent(this, ResultActivity.class);
    intent2.putExtra("product", productName);
    startActivity(intent2);
}

public  void buttonResultClick(View view){
    openResult();
}

Second activity

    int i=1;
    int numberOfButtons = 2;
    for(i=1;i<numberOfButtons;i++) {
        LinearLayout layoutForProductName = findViewById(R.id.layoutForProductName);
        TextView productNameTextView = new TextView(this);
        LinearLayout.LayoutParams layoutParamsForProductNameTextView = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        productNameTextView.setLayoutParams(layoutParamsForProductNameTextView);
        productNameTextView.setTextSize(40);
        String productName = getIntent().getStringExtra("product");
        productNameTextView.setText(productName);
        layoutForProductName.addView(productNameTextView);
    }

Upvotes: 1

Views: 60

Answers (1)

Scott Johnson
Scott Johnson

Reputation: 715

From what I can tell it's because you're adding only one String productName

You can solve this by passing a String Array aka String[]

    public void openResult() {
    ArrayList<String> productNames = new ArrayList<String>();
    for(int y=0; y < createdEditText1Layout.getChildCount(); y++)
    {
        if((View)layout.getChildAt(y) instanceof TextView){
            String productName = editText1.getText().toString();
            productNames.add(productName);
        }
    }
    
    Intent intent2 = new Intent(this, ResultActivity.class);
    intent2.putStringArrayListExtra("product", productNames);
    startActivity(intent2);
}

And then in the second Activity you would recalled like

int i=1;
int numberOfButtons = 2;
for(i=1;i<numberOfButtons;i++) {
    LinearLayout layoutForProductName = findViewById(R.id.layoutForProductName);
    TextView productNameTextView = new TextView(this);
    LinearLayout.LayoutParams layoutParamsForProductNameTextView = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    productNameTextView.setLayoutParams(layoutParamsForProductNameTextView);
    productNameTextView.setTextSize(40);
    String productName = getIntent().getExtras().getStringArrayList("product").get(i);
    productNameTextView.setText(productName);
    layoutForProductName.addView(productNameTextView);
}

Upvotes: 1

Related Questions