1

I have made an simple android activity,In that i am having a plus button and a minus button,when i click plus button a view(row) with some EditTexts will be inflated into LinearLayout,as many as times add button clicked,So i am done it successfully,Now I want to get each EditText value,but I have searched a lot and am stuck in this,So Please help me in this,My code is as below: row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/hiddenLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <LinearLayout
        android:id="@+id/ll_a"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/iv_del"
        android:layout_marginTop="5dp"
        android:gravity="center_vertical"
        android:weightSum="5" >

        <TextView
            android:id="@+id/lblb"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="Item name/ID  :"
            android:textSize="12dp" />

        <EditText
            android:id="@+id/et_item_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@drawable/item_id_txt"
            android:inputType="number"
            android:paddingLeft="5dp"
            android:singleLine="true"
            android:textSize="12dp" />
    </LinearLayout>

    <TextView
        android:id="@+id/lblz"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll_a"
        android:text="Description :"
        android:textSize="12dp" />

    <EditText
        android:id="@+id/et_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/lblz"
        android:layout_marginTop="5dp"
        android:background="@drawable/desrciption"
        android:gravity="center_vertical"
        android:hint="Description"
        android:inputType="text"
        android:padding="2dp"
        android:paddingLeft="5dp"
        android:singleLine="true"
        android:textSize="14dp" />

    <TextView
        android:id="@+id/tv_qt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_desc"
        android:layout_marginLeft="15dp"
        android:text="Quantity"
        android:textSize="12dp" />

    <EditText
        android:id="@+id/et_qty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_qt"
        android:background="@drawable/amount_txt"
        android:ems="5"
        android:inputType="number"
        android:paddingLeft="5dp"
        android:singleLine="true"
        android:textSize="12dp" />

    <TextView
        android:id="@+id/tv_ut_prz"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_desc"
        android:layout_marginLeft="25dp"
        android:layout_toRightOf="@+id/et_qty"
        android:text="Unit Price"
        android:textSize="12dp" />

    <EditText
        android:id="@+id/et_unit_prize"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_ut_prz"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/et_qty"
        android:background="@drawable/amount_txt"
        android:ems="5"
        android:inputType="number"
        android:paddingLeft="5dp"
        android:singleLine="true"
        android:textSize="12dp" />

    <TextView
        android:id="@+id/tv_amt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_desc"
        android:layout_marginLeft="50dp"
        android:layout_toRightOf="@+id/tv_ut_prz"
        android:text="Amount"
        android:textSize="12dp" />

    <EditText
        android:id="@+id/et_amt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/tv_amt"
        android:layout_marginLeft="15dp"
        android:layout_toRightOf="@+id/et_unit_prize"
        android:background="@drawable/amount_txt"
        android:ems="5"
        android:inputType="number"
        android:paddingLeft="5dp"
        android:singleLine="true"
        android:textSize="12dp" />

    <ImageView
        android:id="@+id/iv_del"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="5dp"
        android:background="@drawable/minus" />
<View 
    android:layout_width="fill_parent"
    android:layout_height="0.75dp"
    android:background="#cecece"
    android:layout_marginTop="10dp"
        android:layout_below="@+id/et_amt"
    />
</RelativeLayout>

Code:

//code to adding layout on buttonClick

case R.id.iv_add:

    LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View addView = layoutInflater.inflate(R.layout.raw_descs, null);
    ImageView buttonRemove = (ImageView) addView.findViewById(R.id.iv_del);
    et_item_id = (EditText) addView.findViewById(R.id.et_item_id);
    et_desc = (EditText) addView.findViewById(R.id.et_desc);
    et_qty = (EditText) addView.findViewById(R.id.et_qty);
    et_unit_prize = (EditText) addView.findViewById(R.id.et_unit_prize);
    et_amt = (EditText) addView.findViewById(R.id.et_amt);

    et_qty.addTextChangedListener(textwatcher);
    et_unit_prize.addTextChangedListener(textwatcher);

    buttonRemove.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            ((LinearLayout) addView.getParent()).removeView(addView);
calculateInvoice();
            if (cnt >= 0) {
                cnt = cnt - 1;
            }

        }
    });
    cnt = cnt + 1;
    listitems.setTag(cnt);
    listitems.addView(addView);
calculateInvoice();
    break;


  private void calculateInvoice() {

    double QuantyInt = 1;
    double PriceInt = 0;
    double discountInt = 0;
    double shipInt = 0;

    for (int i = 0; i < listitems.getChildCount(); i++) {

        et_qty = (EditText) ((RelativeLayout) listitems.getChildAt(i)).getChildAt(4);

        et_unit_prize = (EditText) ((RelativeLayout) listitems.getChildAt(i)).getChildAt(6);

        if (et_qty != null) {
            QuantyInt = Double.parseDouble(!et_qty.getText().toString().equals("") ? et_qty.getText().toString() : "0");

        }
        if (et_unit_prize != null) {
            PriceInt = Double.parseDouble(!et_unit_prize.getText().toString().equals("") ? et_unit_prize.getText().toString() : "0");

        }
        subtotal = subtotal + (QuantyInt * PriceInt);

    }

    double textResult = subtotal;
    System.out.println("::::::::::::MY TOATAL PRICE::::::::::::::::>>>>>>>>>>>>>>>>" + subtotal);
    et_amt.setText("$" + textResult + "");
    tv_pre.setText("$" + textResult + "");

    if (et_dis != null) {
        discountInt = Double.parseDouble(!et_dis.getText().toString().equals("") ? et_dis.getText().toString() : "0");

    }
    discount = ((subtotal * discountInt) / 100);
    double txtdiscount = discount;
    tv_dis.setText("$" + txtdiscount + "");
    double totl1 = subtotal - discount;
    tv_total.setText("$" + totl1 + "");

    if (et_ship != null) {
        shipInt = Double.parseDouble(!et_ship.getText().toString().equals("") ? et_ship.getText().toString() : "0");

    }
    tv_ship.setText("$" + et_ship.getText().toString());
    double finaltotal = (shipInt + totl1);
    tv_total.setText("$" + finaltotal + "");

}

TextWatcher textwatcher = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub
        calculateInvoice();

    }
};

This way i am inflating layouts,Please help me how to get edittexts values from each inflated layouts?

user3820044
  • 177
  • 1
  • 3
  • 20

3 Answers3

3

When you click the button, add addView to a global ArrayList<ViewGroup> listOfViewGroups.

Edit: This is what you have to do, based on the info you gave me in the comments (I can't make it any clearer than this). Write the below method:

public double getTotal() {

    double total = 0;

    //get each ViewGroup
    for(int i = 0; i < listOfViewGroups.size(); i++) {
         ViewGroup vg = listOfViewGroups.get(i);
         //get each EditText containing the amount
         EditText e = (EditText) v.findViewById(R.id.et_amt);
         //get the string entered in the EditText
         String str = e.getText().toString();
         //change to a double
         double amount = Double.parseDouble(str);
         //Add to total
         total = total + amount;
    }

    return total;
}

You have to call that method when you're ready to add all of the values. I can't tell you WHEN you have to call it because only you know that and I don't know when you want to.

Once you call the method, set the double returned as the text for the TextView that will display the total. For example,

tv_total.setText(""+getTotal());

(I am assuming your TextView to display this is tv_total.

u3l
  • 3,342
  • 4
  • 34
  • 51
  • It depends. When do you need to get the values of your EditTexts? Do you need them before you exit the Activity? Do you need them when the user clicks another button? – u3l Jul 19 '14 at 11:23
  • actually i am having activity in that having two buttons one plus and one minus,when i click plus a new layout will be appended same way in minus click that particular layout will be removed,So after all the layouts appended,I want "amount" edittexts values to make total by adding them all,Can you please give your mailId,Please save me i want to share my code to you ,Please. – user3820044 Jul 19 '14 at 11:43
  • I'm sorry I don't have enough time right now to go over all of your code. I can help you here though. How do you know when all the layouts are appended? Whenever that happens, just call a method that contains the code I mentioned. It would help if you include more code in your question (click the edit button and add whatever else you think is relevant). – u3l Jul 19 '14 at 11:49
  • See my edit. **You need to decide for yourself where to call the method** – u3l Jul 19 '14 at 13:24
1

here is something that you may try

1) find all child views from container where in you are adding Edittext

2) now using a simple loop you can get value

for(int i=0; i<((ViewGroup)v).getChildCount(); ++i) {
View nextChild = ((ViewGroup)v).getChildAt(i);

}

Sourabh Saldi
  • 3,567
  • 6
  • 34
  • 57
0

For that you need to start with for loop.

 for(int i=0; i<(linearLayoutContainer.getChildCount(); ++i) {
   View child= linearLayoutContainer.getChildAt(i);
   EditText uredt = (EditText)child.findViewById(R.id.editText1);
 }
Piyush
  • 18,895
  • 5
  • 32
  • 63