Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use view to inflate multiple times

I have a some problem regarding inflating and re-using the same TextView.
Its like its trying to overwrite the same textview over and over again or something and it cant do that?

LayoutInflater inflater = LayoutInflater.from(this);
View mainlayout = inflater.inflate(R.layout.days_monday_inflate, null);
View layout_number = inflater.inflate(R.layout.inflate_number, null);

for (int i = 0; i < 10; i++) {
    row = new TableRow(this);
    number = (TextView) layout_number.findViewById(R.id.Number);
    number.setTag(i);
    number.setText(Integer.toString(i));
    row.addView(number);
}
setContentView(mainlayout);

Here is the inflate_number.xml

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Number"
    android:layout_width="3dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center|bottom"
    android:text="1" />

This is the error im getting and its on row: 51, which is: row.addView(number);

07-18 20:54:25.124: E/AndroidRuntime(1166): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
07-18 20:54:25.124: E/AndroidRuntime(1166):     at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
07-18 20:54:25.124: E/AndroidRuntime(1166):     at android.view.ViewGroup.addView(ViewGroup.java:1865)
07-18 20:54:25.124: E/AndroidRuntime(1166):     at android.view.ViewGroup.addView(ViewGroup.java:1822)
07-18 20:54:25.124: E/AndroidRuntime(1166):     at android.view.ViewGroup.addView(ViewGroup.java:1802)
07-18 20:54:25.124: E/AndroidRuntime(1166):     at com.trainingschedule.days.Monday.onCreate(Monday.java:50)
07-18 20:54:25.124: E/AndroidRuntime(1166):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-18 20:54:25.124: E/AndroidRuntime(1166):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

like image 259
JussT Avatar asked Jul 18 '12 21:07

JussT


People also ask

What is view inflate in Android?

Inflate (One Pane) The inflate method inflates the View hierarchy and binds to it all it one step.

What does Inflater inflate do?

inflater.inflate will -Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error. In simple terms inflater. inflate is required to create view from XML .

How do you make a LayoutInflater?

LayoutInflater inflater = getLayoutInflater(); View myLayout = inflater. inflate(R. layout. my_layout, mainLayout, false);


2 Answers

You're never actually adding any Views to your Activity. You're creating new TableRows and adding TextViews to them, but you're never adding the rows to anything. Assuming you have a TableLayout in R.layout.days_monday_inflate, you should first get a reference to that view (TableLayout layout = (TableLayout)mainLayout.findViewById(R.id.my_table_layout_id)) and then add each row to that TableLayout:

TableLayout layout = (TableLayout)mainLayout.findViewById(R.id.my_table_layout_id);

for(int i = 0; i < 10; i++) {
    row = new TableRow(this);
    View layout_number = inflater.inflate(R.layout.inflate_number, layout, false);
    TextView number = (TextView) layout_number.findViewById(R.id.Number);
    number.setTag(i);
    number.setText(Integer.toString(i));
    row.addView(number);
    layout.addView(row);
}

Although I would recommend setting up your layout fully in XML if at all possible, unless needed dynamically. Also, if you're only adding one TextView per row, you're better off just using a LinearLayout, I would suggest.

like image 72
Kevin Coppock Avatar answered Sep 29 '22 06:09

Kevin Coppock


In my case, setTag(i) didn't work, what worked was changing the id after inflating the layout but before adding it to the parent view like this:

LayoutInflater inflater = LayoutInflater.from(this);
View mainlayout = inflater.inflate(R.layout.days_monday_inflate, null);
View layout_number = inflater.inflate(R.layout.inflate_number, null);

for (int i = 0; i < 10; i++) {
    row = new TableRow(this);
    number = (TextView) layout_number.findViewById(R.id.Number);
    number.setId(i); //THIS IS THE KEY STEP
    number.setText(Integer.toString(i));
    row.addView(number);
}
setContentView(mainlayout);
like image 42
unify Avatar answered Sep 29 '22 06:09

unify