Sagar Pawar
Sagar Pawar

Reputation: 475

How to fetch edittext and text values created dynamically?

Am trying to create edittext and textview widgets dynamically into program. it works fine. now i want to access the textvalue with its corresponding edittext value. how to do it?

Here is what i have tried.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_callreport);

    dcname = ProductdetailsEnd.doctor;
    resultArr1 = ProductdetailsEnd.resultArr;

    callreportbtn = (Button) findViewById(R.id.callreportbtn);

    arraysize = resultArr1.length;
    TableLayout tb = (TableLayout) findViewById(R.id.tablelayout);

    for (int i = 0; i < arraysize; i++) {

        res = resultArr1[i];
        TableRow tr = new TableRow(this);
        TableRow.LayoutParams pl = new TableRow.LayoutParams(
                TableRow.LayoutParams.MATCH_PARENT);
        tr.setLayoutParams(pl);

        tr.setWeightSum(1.0f);
        product = new TextView(this);
        product.setLayoutParams(new TableRow.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0.7f));
        product.setId(i);

        qty = new EditText(this);
        qty.setLayoutParams(new TableRow.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0.3f));
        qty.setId(i);

        qty.setWidth(50);
        product.setText(res);
        tr.addView(product);
        tr.addView(qty);

        tb.addView(tr, i);

        Log.d("Call Report name : ", "" + dcname);
        Log.d("Call Report prod :", "" + res);
    }

    Log.d("res length : ", "" + arraysize);

    for (int i = 0; i < arraysize; i++) {

        String product1 = resultArr1[i];

        String qty1 = qty.getText().toString();

        Log.d("products &&: ", "" + product1 + ":" + qty1);
    }

    callreportbtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Hello",
                    Toast.LENGTH_LONG).show();
            for (int i = 0; i < arraysize; i++) {

                String product1 = resultArr1[i];

                String qty1 = qty.getText().toString();

                Log.d("products &&: ", "" + product1 + ":" + qty1);
            }

        }
    });

}

after entering the value, when i say submit it should display the textview value along with edittext value.

For more convenience i have added a snap of my output.

first image to enter the value

first screen

second image is output in logcat. logcat output

Upvotes: 0

Views: 91

Answers (2)

Sagar Pawar
Sagar Pawar

Reputation: 475

Finally i got the answer. referred this post - Creation of EditText Dynamically and get their text from each of EditText

Created an array of the EditText and accessed it in the button onclick method. this is my code and its working.

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

    dcname = ProductdetailsEnd.doctor;
    resultArr1 = ProductdetailsEnd.resultArr;

    callreportbtn = (Button) findViewById(R.id.callreportbtn);
    quantity = new ArrayList<EditText>();

    arraysize = resultArr1.length;
    TableLayout tb = (TableLayout) findViewById(R.id.tablelayout);

    for (int i = 0; i < arraysize; i++) {

        res = resultArr1[i];
        TableRow tr = new TableRow(this);
        TableRow.LayoutParams pl = new TableRow.LayoutParams(
                TableRow.LayoutParams.MATCH_PARENT);
        tr.setLayoutParams(pl);

        tr.setWeightSum(1.0f);
        product = new TextView(this);
        product.setLayoutParams(new TableRow.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0.7f));
        product.setId(i);

        qty = new EditText(this);
        qty.setLayoutParams(new TableRow.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0.3f));
        //qty.setId(i);

        qty.setWidth(50);
        product.setText(res);
        tr.addView(product);
        tr.addView(qty);

        tb.addView(tr, i);

        quantity.add(qty);

        Log.d("Call Report name : ", "" + dcname);
        Log.d("Call Report prod :", "" + res);
    }

    Log.d("res length : ", "" + arraysize);
    Log.d("qty length : ", "" + qty.length());
    String[] items1 = new String[quantity.size()];
    for (int i = 0; i < arraysize; i++) {

        String product1 = resultArr1[i];

        items1[i] = quantity.get(i).getText().toString();


        Log.d("qty id ", "" +qty.toString());
        Log.d("products &&: ", "" + product1 + ":" + items1);
    }

    callreportbtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Hello",
                    Toast.LENGTH_LONG).show();

            String[] items = new String[quantity.size()]; 
            for (int i = 0; i < arraysize; i++) {

                String product1 = resultArr1[i];

                 items[i] = quantity.get(i).getText().toString();

                Log.d("products &&: ", "" + product1 + ":" + items[i]);
            }

        }
    });

}

Upvotes: 0

Marius Kaunietis
Marius Kaunietis

Reputation: 704

There are many ways to do what you want.

  1. Place your views inside a List object, and read the list when you submit.
  2. Implement TextWatcher interface and record your String values as they change.
  3. Assign an Id to your views, and later retrieve your views by Id.

Third option required you to carefully assign id's, because I think they must be unique. Second option, I think, is best one in many cases, since you may also want to provide some feedback to user.

EDIT:

TextWatcher is interface that receives callbacks from TextView. Simply call addTextChangedListener method and provide a TextWatcher implementation.

aTextView.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {

            // TODO Auto-generated method stub
        }
    });

Upvotes: 1

Related Questions