0

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

Sagar Pawar
  • 465
  • 4
  • 26
  • @Sagar Pawar, would you elaborate the question more?? – Reprator Dec 22 '15 at 10:40
  • @VikramSingh : i have created the textviews and edittext dynamically with different values in each rows. now in edittext i want to assign some integer value. when i submit i want to store the row with its textview value and corresponding edittext value. – Sagar Pawar Dec 22 '15 at 10:44
  • `qty[N].getText()` where N is 0..resultArr1.length - 1 – pskink Dec 22 '15 at 10:45
  • @Sagar Pawar, use settag method with each view, it will as id of the view – Reprator Dec 22 '15 at 10:47
  • @pskink: i have done that in for loop but didn't work. it takes last value for all values – Sagar Pawar Dec 22 '15 at 10:48
  • `"t takes last value for all values"` what does it really mean? – pskink Dec 22 '15 at 10:51
  • where is the array you keep your EditTexts ? (qty[N]) – pskink Dec 22 '15 at 11:07
  • i have removed it and executed. i got this output. but when i assign array it gives nullpointer exception on this line - qty.setLayoutParams(new TableRow.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0.3f)); – Sagar Pawar Dec 22 '15 at 11:11

2 Answers2

1

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
        }
    });
Marius Kaunietis
  • 674
  • 4
  • 17
0

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]);
            }

        }
    });

}
Community
  • 1
  • 1
Sagar Pawar
  • 465
  • 4
  • 26