0

I have a LinearLayout (say parent) in whcih i'm inflating another LinearLayout (say child) which contains an EditText & ImageButton.

I am inflating child layout on click of a button in main layout. When one clicks on ImageButton of child layout that layout is gets removed. I have setup this with no problem.

Now I want to get text of all the EditText of child layout,when one click Save button of ActionBar.

Following is the method that I'm using to inflate my view. And mainLayout is my parent layout. Now how can I get text of all added EditText anywhere in my activity ?

private void addEditView(final LinearLayout mainLayout) {

    LayoutInflater inflater = LayoutInflater.from(this);
    View theInflatedView = inflater.inflate(R.layout.layout_add_phone_number_listview, null);

    //Get the EditText in you xml to set the new id 
    EditText editText = (EditText) theInflatedView.findViewById(R.id.et_pn);
    editText.setId(4000+1);//mainLayout.getChildCount()

    ImageButton remove = (ImageButton) theInflatedView.findViewById(R.id.btn_remove_raw);
    remove.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            mainLayout.removeView((View)v.getParent());
        }
    });  
    //add the view into the LinearLayout (global variable)        
    mainLayout.addView(theInflatedView);
}

What have I tried :

LinearLayout ll = (LinearLayout)findViewById(R.id.phone_info);

            int count = ll.getChildCount();
            for(int i =0;i<count;i++)
            {
                Log.e("child count:", "count:" + count + "ID:" + ll.getChildAt(i).getId());
                View vi = ll.getChildAt(i);
                if(vi instanceof EditText)
                {
                    // you got the spinner
                    EditText s = (EditText) findViewById(4000+i);
                    Log.e("Item selected",s.getText().toString());
                }
            }
Uniruddh
  • 4,427
  • 3
  • 52
  • 86

3 Answers3

0

On your parent mainLayout use the findViewById() method to find your child Linear Layout. Than on it find its child elements with the same logic. Every view has the findViewById() method for its childs.

0

try this,

ArrayList<String> edittextValues = new ArrayList<String>();
for(int i = 0; i < mainLayout.getChildCount(); i++){
    View view = (View)mainLayout.getChildAt(i);
    EditText editText = (EditText) view.findViewById(R.id.et_pn);
    edittextValues.add(editText.getText().toString());
}
Akash Moradiya
  • 3,318
  • 1
  • 14
  • 19
0

Your problem here IMO is that instead to use a ListView you try to create your own list appending each View.

My proposed solution: Use a ListView with an ArrayAdapter. You will be able to access EditText and ImageButton for each row inside your ArrayAdapter.

Edit
Following our discussion and the problem being that user inputed text is not saved and lost after adding rows due to the recycling property of ListView, you should customize your ArrayAdapter to save the text typed by the user. This can be done in the getView of your ArrayAdapter as explained here: https://stackoverflow.com/a/9441210/891479.

Community
  • 1
  • 1
L. G.
  • 9,642
  • 7
  • 56
  • 78
  • I had already tried this approach. But in that case I was unable to retain values of previous `EditText` when i add new item. If have working example then i can use it again. – Uniruddh May 23 '14 at 07:30
  • Perhaps I miss something, but you should be able to access all the values of the Object List given in the ArrayAdapter constructor ArrayAdapter(Context context, int resource, int textViewResourceId, List objects). you can also customize ArrayAdapter to match your data model. – L. G. May 23 '14 at 07:40
  • When I add new item into to ArrayAdapter, the values which user have already entered should be retained in view. But when I was updating view with adapter those text of `EditText` was got cleared. – Uniruddh May 23 '14 at 07:43
  • I think it is link to the ListView recycling, other problem bu the cause and solution could be the same here: http://stackoverflow.com/questions/9438676/edittext-in-listview-without-it-recycling-input. Basically what you need to do is save the input for your EditTexts in some data structure/Object list or array. – L. G. May 23 '14 at 07:49