简体   繁体   中英

Getting content from editText

In my application, i have a tableLayout with many editTexts in it. When i click "save"button, i want to access all the values entered in editTexts. I have assigned IDs in runtime while creating the table. Now how can i access the values from editTexts when "save" button is clicked...? I have used something like below to assign IDs,

for(int i=0;i< no_of_rows ;i++)
  for(int j=0;j<5;j++)
  {   
    ...............
    assignment.setId(i+j);
    .............
  }

Could anyone suggest a solution..?

How about something like:

ArrayList<String> strings = new ArrayList<String>();
for(int i=0;i< no_of_rows ;i++)
  for(int j=0;j<5;j++)
  {   
    EditText text = (EditText)ActivityName.this.findViewById(i+j);
    strings.add(text.getText().toString());
  }
}

This would give you all of the values from all of the texts in one big array. If that's not what you want, let me know and I'll see if I can adjust the code.

Other nice solution is to cycle through all childrens of some Layout, so you don't need to set any special IDs. Just try:

LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
child_count = layout.getChildCount();

for (int i=0; i<child_count; i++)
{
    EditText text = (EditText) layout.getChildAt(i);
    // do work with text
}

With some other code, you can do this for any other layout hierarchy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM