RunawayMartian
RunawayMartian

Reputation: 11

Get a reference to same view in multiple inflated layouts

I tried to find an answer to this question, and I thought my code would work as I wanted but.. it's not.

Problem: I have a "Parent" LinearLayout to which I add several nested inflated "Child" LinearLayout's. This works. Each CHILD layout has two views, a custom ChipView and a TextView. After I inflate each child I want to be able to modify the ChipView and TextView of each CHILD "whenever I want" during my Activity.

I created a simple project to play with, but I can only manage to access the ChipView and TextView of the FIRST INFLATED child layout. All the subsequent ones are inserted correctly in the Parent but apparently I can't get a variable to reference them.

I was doing this earlier by creating ChipView's at runtime and it worked flawlessly, but I wanted a more elegant approach with an XML I can control separately.

In my activity I have a button that creates children and one that should call a method in the current ChipView (i.e. last inflated OR the one I click on).

ACTIVITY:

public class SandboxActivity extends Activity {
    private Button okbtn;
    private Button add;
    private EditText count;
    private ChipsView chips;
    private LinearLayout pots;
    private TextView amount;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

       okbtn = (Button) findViewById(R.id.ok); //calls the method in customviews
       add = (Button) findViewById(R.id.add);  //adds a child
       count = (EditText) findViewById(R.id.count); //the value to call methods with
       pots = (LinearLayout) findViewById(R.id.pots); //the PARENT layout

       add.setOnClickListener(new Button.OnClickListener() {

        public void onClick(View v) {
            LinearLayout ll = (LinearLayout) getLayoutInflater().inflate(R.layout.pottemplate, pots);
            chips = (ChipsView) ll.findViewById(R.id.chips);
            amount = (TextView) ll.findViewById(R.id.amount);

               //this should allow me to set the activity.chips variable to the last clicked custom view
            chips.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    chips = (ChipsView) v;
                }
            });         
            }

        });


        okbtn.setOnClickListener(new Button.OnClickListener() {

            public void onClick(View v) {
                chips.setCount(Double.parseDouble(count.getText().toString()));         
            }

        });

    }
}

INFLATED XML:

<?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" >
    <com.ded.sandbox.ChipsView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="100dp"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:id="@+id/chips">    

    </com.ded.sandbox.ChipsView>

    <TextView
        android:id="@+id/amount"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="TextView" android:gravity="center_horizontal" android:textSize="12dp" android:textStyle="bold"/>

</LinearLayout>

Upvotes: 1

Views: 1214

Answers (1)

nvasilescu
nvasilescu

Reputation: 124

View oneHappyRow = mInflater.inflate(R.layout.one_happy_hour_row,null);
TextView dayOfWeekTextView = (TextView) oneHappyRow.findViewById(R.id.dayOfWeekTextView);
TextView hoursOfDayTextView = (TextView) oneHappyRow.findViewById(R.id.hoursOfDayTextView);

get references here for each inflated layout , set texts etc. then:

this.addView(oneHappyRow);

The class in my case extends LinearLayout.

Each reference is working now, and you will not get always the last reference.

Upvotes: 1

Related Questions