2014-05-04 5 views
1

Dans un ListActivity, j'ai un "plus" Button sous la liste.Android: Ajouter des éléments à un ListView dynamiquement sans désordre

Lorsque je clique dessus, le résultat s'ajoute correctement à la liste, mais le problème est que le bouton disparaisse après cela! Je veux qu'il reste là et que l'utilisateur puisse recharger plus de données bien sûr!

Le code est:

btnMoreItems.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // some fake data 
      for (int i = 10; i < 20; i++) { 
       data.add(// fake data to attach to the list //); 
      } 

      setListAdapter(new MyCustomArrayAdapter(MyListActivity.this, data)); 
     } 
    }); 

et la mise en page est un peu comme ceci:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ListView 
     android:id="@+id/android:list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 

    <TextView 
     android:id="@+id/android:empty" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/myListEmpty" /> 

    <Button 
     android:id="@+id/btnMoreItemsLoad" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
    → → android:layout_below="@+id/android:list ← ← ← ← ← 
     android:text="@string/moreItems" /> 

</RelativeLayout> 

Lorsque la charge de l'activité pour la première fois, le bouton est au bon endroit. Mais lorsqu'il est cliqué, il disparaît derrière les éléments ajoutés.

Avec cette ligne: android:layout_below="@+id/android:list Je pense que le séjour de bouton dans sa position (je veux dire en dessous du dernier élément de la liste)

Merci pour toute aide ...

Répondre

0

Changer la disposition du suivant:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ListView 
     android:align_parentTop="true" ← ← ← ← ← 
     android:layout_weight="1" ← ← ← ← ← 
     android:id="@+id/android:list" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" > 
    </ListView> 

    <TextView 
     android:id="@+id/android:empty" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/myListEmpty" /> 

    <Button 
     android:layout_weight="0" ← ← ← ← ← 
     android:align_parentBottom="true" ← ← ← ← ← 
     android:id="@+id/btnMoreItemsLoad" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/android:list 
     android:text="@string/moreItems" /> 

</RelativeLayout> 
+0

merci mais il y a un avertissement à ce sujet: "layout_weight" Paramètre de disposition invalide dans un RelativeLayout: layout_weight –

0

essayer ce code

<TextView 
     android:id="@+id/android:empty" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/myListEmpty" /> 

    <ListView 
     android:id="@+id/android:list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/android:empty" 
     android:layout_marginTop="5dp" > 
    </ListView> 

    <Button 
     android:id="@+id/btnMoreItemsLoad" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/android:list" 
     android:text="@string/moreItems" /> 

</RelativeLayout> 
+0

Merci ... mais il n'y a pas de changement significatif dans mon code! Vous ajoutez seulement "layout_marginTop =" 5dp "" à ListView. Etes-vous sûr que cela pourrait résoudre? J'essaie cela, ce n'est pas résoudre le problème ... –

+0

Non seulement la marge supérieure, j'ai ajouté la mise en page: ci-dessous dans listview aussi .. selon ma compréhension, il shl travail – Meghna

+0

J'essaie cela, mais ne fonctionne pas! En fait seulement un d'entre eux (ListView & TextBox) montré! S'il n'y a pas d'éléments dans la liste, ListView est caché et sinon, si la liste n'est pas vide, TextBox va se cacher! –

Questions connexes