J'ai un problème avec l'obtention de la position LinearLayout. D'abord j'ajoute dynamiquement un certain nombre de boutons et puis j'essaye de retourner l'index de chaque enfant et l'afficher dans un TextView. Ici, je partage de code:Ajouter dynamiquement un enfant à LinearLayout pour obtenir la position de chaque enfant
source java:
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));
}
};
structure xml:
<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"
/>
ressources:
<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>
L'ajout dynamique est correct, mais la façon dont je définis OnClickListener produit une erreur. Toute aide sera utile! Le but de cela est si je veux ajouter un ou plusieurs bouton (s) au HorizontalScrollView pour ne pas être nécessaire pour éditer plusieurs fichiers, et juste pour aller à sting.xml et créer un nouvel élément dans les catégories -array!
C'est ce que LogCat produit:
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
Pour clarifier - l'application démarre sans erreur, mais lorsque je clique sur un bouton, il produit cette ligne du journal ci-dessus:
07-12 22:37:25.552: WARN/ResourceType(331): No package identifier when getting value for resource number 0x00000000
Et c'est de débogueur:
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)
... ou' tv.setText ("" + ll.indexOfChild (v)); 'de toute façon merci beaucoup, une si petite erreur! – nenito
merci c'est utile :) –