Dynamically add a child to LinearLayout with each child position

I have a problem getting a LinearLayout child position. First, I dynamically add a few buttons, and then I try to return each child index and display it in a TextView. Here I use the code:

java source :

private String[] categories; private LinearLayout ll; private TextView tv; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); categories = getResources().getStringArray(R.array.categories); tv = (TextView) findViewById(R.id.text); ll = (LinearLayout) findViewById(R.id.hsvLinearLayout); for(int i = 0; i < categories.length; i++) { Button btn = new Button(this); btn.setText(categories[i]); btn.setOnClickListener(buttonClick); ll.addView(btn); } } OnClickListener buttonClick = new OnClickListener() { public void onClick(View v) { tv.setText(ll.indexOfChild(v)); } }; 

xml structure :

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <HorizontalScrollView android:id="@+id/Footer" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scrollbars="none" android:fadingEdge="none" > <LinearLayout android:id="@+id/hsvLinearLayout" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" > </LinearLayout> </HorizontalScrollView> <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" /> 

Resources

 <string name="today">Today</string> <string name="life">Life</string> <string name="corner">Corner</string> <string name="banks">Banks</string> <string name="it">IT</string> <string name="fun">Fun</string> <array name="categories"> <item>@string/today</item> <item>@string/life</item> <item>@string/corner</item> <item>@string/banks</item> <item>@string/it</item> <item>@string/fun</item> </array> 

Dynamically adding is fine, but the way I set OnClickListener is causing some error. Any help would be helpful! The purpose of this is that if I want to add one or more buttons to the HorizontalScrollView so that there is no need to edit many files, just go to sting.xml and create a new element in category-array!

This is what LogCat produced:

 07-12 22:37:17.680: INFO/System.out(331): waiting for debugger to settle... 07-12 22:37:17.900: INFO/System.out(331): debugger has settled (1441) 07-12 22:37:20.870: INFO/ActivityManager(61): Displayed activity com.test/.TestDynam: 10203 ms (total 10203 ms) 07-12 22:37:25.552: WARN/ResourceType(331): No package identifier when getting value for resource number 0x00000000 07-12 22:37:26.871: DEBUG/dalvikvm(132): GC freed 190 objects / 8976 bytes in 901ms 

To clarify, the application starts without any errors, but when I click the button, it displays this line from the above log:

 07-12 22:37:25.552: WARN/ResourceType(331): No package identifier when getting value for resource number 0x00000000 

And this is from the debugger :

 TestDynam [Android Application] DalvikVM[localhost:8611] Thread [<3> main] (Suspended (exception Resources$NotFoundException)) ViewRoot.handleMessage(Message) line: 1704 ViewRoot(Handler).dispatchMessage(Message) line: 99 Looper.loop() line: 123 ActivityThread.main(String[]) line: 4203 Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method] Method.invoke(Object, Object...) line: 521 ZygoteInit$MethodAndArgsCaller.run() line: 791 ZygoteInit.main(String[]) line: 549 NativeStart.main(String[]) line: not available [native method] Thread [<13> Binder Thread #2] (Running) Thread [<11> Binder Thread #1] (Running) 
+7
source share
2 answers

Your problem is resources .... you use the setText (int) method (this index is int ...), which searches for resources, not a string, this is not StringBuilder, for which you can throw any type and get a string. you need to replace

tv.setText (ll.indexOfChild (v));

from

tv.setText (Integer.toString (ll.indexOfChild (v)));

and if you want even a little efficiency:

 public class TestActivity extends Activity { private String[] categories; private LinearLayout ll; private TextView tv; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); categories = getResources().getStringArray(R.array.categories); tv = (TextView) findViewById(R.id.text); ll = (LinearLayout) findViewById(R.id.hsvLinearLayout); for(int i = 0; i < categories.length; i++) { Button btn = new Button(this); btn.setText(categories[i]); btn.setOnClickListener(buttonClick); ll.addView(btn); int idx = ll.indexOfChild(btn); btn.setTag(Integer.toString(idx)); } } OnClickListener buttonClick = new OnClickListener() { public void onClick(View v) { String idxStr = (String)v.getTag(); tv.setText(idxStr); } }; } 
+12
source

You must set upContentView before running findViewById

 setContentView(R.layout.main); tv = (TextView) findViewById(R.id.text); 
+2
source

All Articles