1

In My App i have 10 Edittext and 10 TextView

I have created instance for the both EditText and TextView like

String[] messageText=new String[10];
String[] messageEdit=new String[10];
TextView oTextView1 = (TextView) findViewById(R.id.Tab_common1_EditText);
    TextView oTextView2 = (TextView) findViewById(R.id.Tab_common2_EditText);
    TextView oTextView3 = (TextView) findViewById(R.id.Tab_common3_EditText);
    TextView oTextView4 = (TextView) findViewById(R.id.Tab_common4_EditText);
    TextView oTextView5 = (TextView) findViewById(R.id.Tab_common5_EditText);
    TextView oTextView6 = (TextView) findViewById(R.id.Tab_common6_EditText);
    TextView oTextView7 = (TextView) findViewById(R.id.Tab_common7_EditText);
    TextView oTextView8 = (TextView) findViewById(R.id.Tab_common8_EditText);
    TextView oTextView9 = (TextView) findViewById(R.id.Tab_common9_EditText);
    TextView oTextView10 = (TextView) findViewById(R.id.Tab_common10_EditText);

    EditText oEditTextHiden1 = (EditText) findViewById(R.id.Tab_Hidencommon1_EditText);
    EditText oEditTextHiden2 = (EditText) findViewById(R.id.Tab_Hidencommon2_EditText);
    EditText oEditTextHiden3 = (EditText) findViewById(R.id.Tab_Hidencommon3_EditText);
    EditText oEditTextHiden4 = (EditText) findViewById(R.id.Tab_Hidencommon4_EditText);
    EditText oEditTextHiden5 = (EditText) findViewById(R.id.Tab_Hidencommon5_EditText);
    EditText oEditTextHiden6 = (EditText) findViewById(R.id.Tab_Hidencommon6_EditText);
    EditText oEditTextHiden7 = (EditText) findViewById(R.id.Tab_Hidencommon7_EditText);
    EditText oEditTextHiden8 = (EditText) findViewById(R.id.Tab_Hidencommon8_EditText);
    EditText oEditTextHiden9 = (EditText) findViewById(R.id.Tab_Hidencommon9_EditText);
    EditText oEditTextHiden10 = (EditText) findViewById(R.id.Tab_Hidencommon10_EditText);

now i need to get all the TextView getText() and EditText getText()

I have try some method but it is not working

 for (int i = 0; i < 10; i++) {

        messageText[i] = (oTextView+i).getText().toString();//it says oTextView cannot be resolved to a variable
        messageEdit[i] = (oEditTextHiden+i).getText().toString();
      }
      Log.i("message", "message :"+Arrays.deepToString(messageText)+" "+Arrays.deepToString(messageEdit));
6
  • put all oTextView on one List<TextView> and work with that Commented Feb 25, 2014 at 12:46
  • You have to create two array-lists one for text-view and other for Edit-text and then work with that. Commented Feb 25, 2014 at 12:48
  • can i have any sample Commented Feb 25, 2014 at 12:48
  • Try changing (oTextView+i) to ("oTextView"+i) Commented Feb 25, 2014 at 12:49
  • @gilonm i have tried it it is not working Commented Feb 25, 2014 at 12:50

6 Answers 6

7

Store the ids of the EditText and loop over them, since the View ids are integers, you can do that easily like so :

ArrayList<String> values = new ArrayList<String>();
int[] ids = new int[]{R.id.editText1,R.id.editText2,R.id.editText3};//and so on

for(int id : ids){
    EditText t = (EditText) findViewById(id);
    values.add(t.getText().toString());
}

Or if you only want the text and don't want to do anything else with the EditText view itself, you could make it in one line this:

for(int id : ids) values.add(((EditText)findViewById(id)).getText().toString());
Sign up to request clarification or add additional context in comments.

4 Comments

That's what i am talk in about! +1 up
You can also use something like: EditText text = (EditText) getActivity().findViewById(getResources().getIdentifier("generalname" + number, "id", context.getPackageName()));
@Ayoub according to the docs: Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
Your missing a bracket. Should be: values.add(t.getText().toString());
3

Add all TextView and EdiText in Array Like this

TextView[] oTextView = { oTextView1, oTextView2, oTextView3,
            oTextView4, oTextView5, oTextView6, oTextView7, oTextView8,
            oTextView9, oTextView10 };

EditText[] oEditTextHiden = { oEditTextHiden1, oEditTextHiden2,
            oEditTextHiden3, oEditTextHiden4, oEditTextHiden5,
            oEditTextHiden6, oEditTextHiden7, oEditTextHiden8,
            oEditTextHiden9, oEditTextHiden10 };

Then getText() Should look like this

    for (int i = 0; i < 10; i++) {

        messageText[i] = oTextView[i].getText().toString();
        messageEdit[i] = oEditTextHiden[i].getText().toString();
    }

Comments

2
String[] messageText=new String[10];
String[] messageEdit=new String[10];
TextView[] textViews = new TextView[10];
textViews[0] = (TextView) findViewById(R.id.Tab_common1_EditText);
textViews[1] = (TextView) findViewById(R.id.Tab_common2_EditText);
textViews[2] = (TextView) findViewById(R.id.Tab_common3_EditText);
textViews[3] = (TextView) findViewById(R.id.Tab_common4_EditText);
textViews[4] = (TextView) findViewById(R.id.Tab_common5_EditText);
textViews[5] = (TextView) findViewById(R.id.Tab_common6_EditText);
textViews[6] = (TextView) findViewById(R.id.Tab_common7_EditText);
textViews[7] = (TextView) findViewById(R.id.Tab_common8_EditText);
textViews[8] = (TextView) findViewById(R.id.Tab_common9_EditText);
textViews[9] = (TextView) findViewById(R.id.Tab_common10_EditText);

EditText[] editTexts = new EditText[10];
editTexts[0] = (EditText) findViewById(R.id.Tab_Hidencommon1_EditText);
editTexts[1] = (EditText) findViewById(R.id.Tab_Hidencommon2_EditText);
editTexts[2] = (EditText) findViewById(R.id.Tab_Hidencommon3_EditText);
editTexts[3] = (EditText) findViewById(R.id.Tab_Hidencommon4_EditText);
editTexts[4] = (EditText) findViewById(R.id.Tab_Hidencommon5_EditText);
editTexts[5] = (EditText) findViewById(R.id.Tab_Hidencommon6_EditText);
editTexts[6] = (EditText) findViewById(R.id.Tab_Hidencommon7_EditText);
editTexts[7] = (EditText) findViewById(R.id.Tab_Hidencommon8_EditText);
editTexts[8] = (EditText) findViewById(R.id.Tab_Hidencommon9_EditText);
editTexts[9] = (EditText) findViewById(R.id.Tab_Hidencommon10_EditText);

for (int i = 0; i < 10; i++) {
    messageText[i] = textViews[i].getText().toString();
    messageEdit[i] = editTexts[i].getText().toString();
  }
  Log.i("message", "message :"+Arrays.deepToString(messageText)+" "+Arrays.deepToString(messageEdit));

Comments

1

The best way to do this is to use the tag associated with the textview.

For example :

In Xml you need to create TextView and editText as following :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/llParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:tag="tv1"
    android:text="TextView1" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:tag="et1"
    android:text="EditText1" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:tag="tv2"
    android:text="TextView2" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:tag="et2"
    android:text="EditText2" />

  ===== Similarly 10 Textviews and EditTexts========

</LinearLayout>

In Code :

String[] messageText = new String[10];
String[] messageEdit = new String[10];

LinearLayout llParent = (LinearLayout) findViewById(R.id.llParent);

    for (int i = 1; i <= 10; i++) {
        messageText[i - 1] = ((TextView) llParent.findViewWithTag("tv" + i))
                .getText().toString();
        messageEdit[i - 1] = ((EditText) llParent.findViewWithTag("et" + i))
                .getText().toString();
    }

Happy Coding.... :-)

Comments

0

Try Like This :-

    ArrayList<TextView> arrayListTextViews = new ArrayList<TextView>();
    TextView oTextView1 = (TextView) findViewById(R.id.Tab_common1_EditText);
    TextView oTextView2 = (TextView) findViewById(R.id.Tab_common2_EditText);
    TextView oTextView3 = (TextView) findViewById(R.id.Tab_common3_EditText);
    TextView oTextView4 = (TextView) findViewById(R.id.Tab_common4_EditText);
    TextView oTextView5 = (TextView) findViewById(R.id.Tab_common5_EditText);
    TextView oTextView6 = (TextView) findViewById(R.id.Tab_common6_EditText);
    TextView oTextView7 = (TextView) findViewById(R.id.Tab_common7_EditText);
    TextView oTextView8 = (TextView) findViewById(R.id.Tab_common8_EditText);
    TextView oTextView9 = (TextView) findViewById(R.id.Tab_common9_EditText);
    TextView oTextView10 = (TextView) findViewById(R.id.Tab_common10_EditText);
    arrayListTextViews.add(oTextView1);
    arrayListTextViews.add(oTextView2);
    arrayListTextViews.add(oTextView3);
    arrayListTextViews.add(oTextView4);
    arrayListTextViews.add(oTextView5);
    arrayListTextViews.add(oTextView6);
    arrayListTextViews.add(oTextView7);
    arrayListTextViews.add(oTextView8);
    arrayListTextViews.add(oTextView9);
    arrayListTextViews.add(oTextView10);

and then access textviews from arrayListTextViews in the loop. Also do the same for Your Edittext.

Comments

0

the best way is to use the getIdentifier method in the Resources class

something like that

messageText[i] = (TextView) getActivity().findViewById(getResources().getIdentifier("Tab_common"+i+"_EditText", "id", context.getPackageName()));
messageEdit[i] = (EditText) getActivity().findViewById(getResources().getIdentifier("Tab_Hidencommon"+i+"_EditText", "id", context.getPackageName()));

2 Comments

getIdentifier Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name. (From the docs)
I know it is less efficient, but it's 1 time initialization is not a problem and avoids having to keep track of all those IDs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.