Question

This is Relative Layout view which is created dynamically like this :

Layout

... So my question is how to get the values from those dynamic elements and perform some calculation to show result on textview which is also dynamically created. Thanks in advance guys!!

    public void goButtonClicked(View view) {
    maalContainer.removeAllViews();
    int numberofPlayersTolayout = (Integer) Integer.parseInt((String) numberOfPlayers.getSelectedItem());

    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

    for (int i = 0; i < numberofPlayersTolayout; i++) {
        View dynamicEntryView = inflater.inflate(R.layout.player_entry_item, null);
        maalContainer.addView(dynamicEntryView, params);
    }

}

}

Was it helpful?

Solution 4

     public void goButtonClicked(View view) {
         int numberofPlayersTolayout =       Integer.parseInt(numberOfPlayers.getSelectedItem().toString());
        LayoutInflater inflater = (LayoutInflater)  getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,  LayoutParams.WRAP_CONTENT);
        maalContainer.removeAllViews();

        maalText = new EditText[numberofPlayersTolayout];
        points = new EditText[numberofPlayersTolayout];
        result = new TextView[numberofPlayersTolayout];
        for (int i = 0; i < numberofPlayersTolayout; i++) {
            View dynamicEntryView = inflater.inflate(R.layout.player_entry_item, null);
            maalContainer.addView(dynamicEntryView, params);
            TextView playerName = (TextView) dynamicEntryView.findViewById(R.id.player_name_textview);
            playerName.setText("Player :" + (i + 1));

            maalText[i] = (EditText) dynamicEntryView.findViewById(R.id.player_item_edittext_maal);
            points[i] = (EditText) dynamicEntryView.findViewById(R.id.player_item_edittext_point);
            result[i] = (TextView) dynamicEntryView.findViewById(R.id.player_item_textview_result);
}

OTHER TIPS

for (int i = 0; i < maalContainer.getChildCount(); i++) {
    View view = maalContainer.getChildAt(i);

    EditText ed_item = (EditText) view
            .findViewById(R.id.edittext1);
    EditText ed_value = (EditText) view
            .findViewById(R.id.edittext2);
    EditText ed_value1 = (EditText) view
            .findViewById(R.id.edittext3);

    ed_item.getText().toString().trim();
    ed_value.getText().toString().trim();
    ed_value1.getText().toString().trim();
}

No need to assign/get ID of any View but the best way is to iterate through the child views of LinearLayout:

int childcount = myLinearLayout.getChildCount();
for (int i=0; i < childcount; i++){
      View v = myLinearLayout.getChildAt(i);
    // do whatever you would want to do with this View
}

A few things here:

  1. You shouldn't be using the Application Context to get the LayoutInflater. You should get that from the Activity Context, otherwise your themes and styles will not be respected.

  2. You shouldn't inflate with null as the second parameter (except in special instances where you don't know the View's parent). Just put the android:layout_width="match_parent" and android:layout_height="wrap_content" attributes in player_entry_item.xml and they will be respected upon inflation.

  3. Store the EditText objects upon inflation as a private array.

With that said, my suggested revision:

private EditText[] mEditTextPlayers;

public void goButtonClicked(View view) {
    maalContainer.removeAllViews();
    int numPlayers = Integer.parseInt((String) numberOfPlayers.getSelectedItem());

    LayoutInflater inflater = LayoutInflater.from(view.getContext());
    mEditTextPlayers = new EditText[numPlayers];

    for (int i = 0; i < numPlayers; i++) {
        //Pass the parent as the second parameter to retain layout attributes
        mEditTextPlayers[i] = inflater.inflate(R.layout.player_entry_item, maalContainer, false);
        maalContainer.addView(dynamicEntryView);
    }
}

You can do it by using getId() and setId() methods of the View.

   dynamicEntryView.setId(i);

and access the view as below by getting the

   dynamicEntryView.getId(i);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/player_name_textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10sp"
        android:layout_marginTop="10sp"
        android:text="Player name"
        android:textColor="#202020"
        android:textSize="18sp" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/player_item_edittext_point"
            android:layout_width="0sp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Points"
            android:inputType="number"
            android:textColor="#202020" />

        <EditText
            android:id="@+id/player_item_edittext_maal"
            android:layout_width="0sp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Maal"
            android:inputType="number"
            android:textColor="#202020" />

        <TextView
            android:id="@+id/player_item_textview_result"
            android:layout_width="0sp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="0.00$"
            android:textSize="22sp"
            android:typeface="serif" />
    </LinearLayout>

</LinearLayout>

Only one id is used. It will loop through according to number of players. Hope it will help you. @user2731584

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top