0

have a row contains 14 edittext.have two buttons for adding and deleting the row dynamically.somehow i can able to do this ,my problem is every time when i add a row ,all the edittext of row have same id and name.So my questions is. 1.how to change the name and id of edittext on add button click. 2.add how to store the value of multiple row into database. enter image description here my code is..
row.xml

<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"                
            android:background="@drawable/table_back"     
            android:layout_gravity="center_vertical"    >

        <EditText
                android:id="@+id/req"
                android:layout_width="85dp"
                android:layout_height="wrap_content"                    
                android:background="#00000000">
                <requestFocus />
        </EditText>

        <EditText
                android:id="@+id/fm"
                android:layout_width="82dp"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:background="#00000000"
                android:ems="10" />
        <EditText
                android:id="@+id/effects"
                android:layout_width="82dp"
                android:inputType="text"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:ems="10" />
        <EditText
                android:id="@+id/sev"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:layout_marginRight="10dp"
                android:inputType="text"
                android:ems="10" />
        <EditText
                android:id="@+id/causes"
                android:layout_width="82dp"
                android:layout_height="wrap_content"
                 android:inputType="text"
                android:background="#00000000"
                android:ems="10" />
        <EditText
                android:id="@+id/occ"
                android:layout_width="35dp"
                 android:inputType="text"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:layout_marginRight="7dp"
                android:ems="10" />
        <EditText
                android:id="@+id/process"
                android:layout_width="85dp"
                 android:inputType="text"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:ems="10" />
        <EditText
                android:id="@+id/det"
                android:layout_width="35dp"
                 android:inputType="text"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:layout_marginRight="3dp"
                android:ems="10" />
        <EditText
                android:id="@+id/rpn"
                android:layout_width="35dp"
                 android:inputType="text"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:layout_marginRight="5dp"
                android:ems="10" />
        <EditText
                android:id="@+id/recommended"
                android:layout_width="82dp"
                 android:inputType="text"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:layout_marginRight="2dp"
                android:ems="10" />
        <EditText
                android:id="@+id/sev2"
                android:layout_width="35dp"
                 android:inputType="text"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:layout_marginRight="3dp"
                android:ems="10" />
        <EditText
                android:id="@+id/occ2"
                android:layout_width="35dp"
                 android:inputType="text"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:layout_marginRight="8dp"
                android:ems="10" />
        <EditText
                android:id="@+id/det2"
                android:layout_width="35dp"
                 android:inputType="text"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:layout_marginRight="3dp"
                android:ems="10" />
        <EditText
                android:id="@+id/new_rpn"
                android:layout_width="35dp"
                 android:inputType="text"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:layout_marginRight="6dp"
                android:ems="10" />
        <EditText
                android:id="@+id/resp"
                android:layout_width="85dp"
                 android:inputType="text"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:ems="10" />
        <EditText
                android:id="@+id/target_date"
                android:layout_width="40dp"
                android:inputType="date"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:ems="10" />

Create.class

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

            mContainerView = (LinearLayout) findViewById(R.id.parentView);
            mAddButton = (ImageButton) findViewById(R.id.btnAddNewItem);
            mDeleteButton=(ImageButton)findViewById(R.id.btnDelete);

            // Add some examples
            inflateEditRow(count);
    }
          public void onAddNewClicked(View v) {


count++;
    inflateEditRow(count);      
    v.setVisibility(View.VISIBLE);
//  System.out.println("value of count "+count);
}

    public void onDeleteClicked(View v) {
        // remove the row by calling the getParent on button
    //  mContainerView.removeView((View) v.getParent());



                int del=count;
                if(del-->=1){
                mContainerView.removeViewAt(del);
                count--;

        } 
        else{
            Toast.makeText(getApplicationContext(), "Add row to delete", Toast.LENGTH_SHORT).show();
        }
              }

private void inflateEditRow(int id) { 
        alleds=new ArrayList<EditText>();
        int et_id=1;

        for(int i=1;i<=id;){


        //  System.out.println("value of i "+i);
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              rowView = inflater.inflate(R.layout.row1, null);


            rowView.setId(count);
            i++;        

        }      

thanks in advance.

sunil_patidar
  • 203
  • 1
  • 3
  • 13
  • Here, read up on SQLite. www.vogella.com/articles/AndroidSQLite/article.html – Razgriz Sep 28 '13 at 07:02
  • @Razgriz thanks for the reply. but i need to insert multiple rows at a time.i know how to insert single row into database. – sunil_patidar Sep 28 '13 at 07:12
  • @sunil_patidar can you tell me, have you found solution for your problem? if yes can you please put the code? thanks – Aoyama Nanami Nov 28 '13 at 03:40
  • @AoyamaNanami use id in for loop to increment the id of each edittext....using this id you can set and get the value of edittext dynamically... – sunil_patidar Nov 28 '13 at 04:43
  • @sunil_patidar what about store it into sqlite? have you found a solution? because i have same problem with you – Aoyama Nanami Nov 28 '13 at 04:56
  • for (int i = 1; i < mContainerView.getChildCount(); i++) { String j = i + "00"; int k = Integer.parseInt(j); EditText req = (EditText) findViewById(k++); } – sunil_patidar Nov 28 '13 at 05:01
  • @AoyamaNanami use this code in submit button and create appropriate method in dataHelper class.. – sunil_patidar Nov 28 '13 at 05:03
  • @sunil_patidar i still don't get it.. :( would you like to take a look my post? http://stackoverflow.com/questions/20260281/android-give-id-into-widget-such-as-textview-edittext-spinner-in-dynamic-row – Aoyama Nanami Nov 28 '13 at 07:55

2 Answers2

0

Single Row:

Put all your EditText objects in a list.

When you want to store the data simply iterate them.

for (EditText et: editTextList) {
    addSingleRow(et.getText().toString() ; 
} 

Using this it does not matter what id or name they have.

Multiple Rows:

Map<Integer, List<EditText>> editTextRows = new TreeMap<Integer, List<EditText>>();

for (Integer row: editTextRows.keySet()) {

    List<EditText> editTextList = editTextRows.get(row);

    for (EditText et: editTextList) {
        addSingleRow(et.getText().toString() ; 
    } 
} 

Why TreeMap you may ask. It just has the nice feature that it sorts the keys, so that you get the rows in order as you iterate them.

cYrixmorten
  • 7,110
  • 3
  • 25
  • 33
0

this is possible, with add dynamically edittext and buttons and image. check this post

venkat
  • 157
  • 3
  • 15