2015-12-21 1 views
3

Je développe une application Android pour un restaurant pour un système de commande de nourriture.
Dans mon projet, j'ai Gridview, quand cliquez sur un button sur l'élément dans gridview, je veux obtenir une fenêtre pop-up avec des champs de texte, boutons etc.Créer une fenêtre contextuelle pour chaque élément de grille - Android

Je suis l'aide de this document example

la Exemple de document ci-dessus fonctionne seul comme application unique. C'est juste un exemple pour créer une fenêtre pop-up de base. Mais j'en ai besoin sur Gridview.

my work -->

au lieu de main.xml et PopUpWinndowDemoActivity.java (il est l'activité principale) de doc ci-dessus, -je utiliser mon grid_single.xml et il est implementation file Grid_single.java.

Mais cela ne fonctionne pas pour moi.Lorsque je clique sur le bouton de chaque élément, ne pas afficher la fenêtre pop-up comme je l'espère. Donc, s'il vous plaît aidez-moi à résoudre ceci ou donnez-moi un exemple de travail pour une fenêtre pop-up sur les éléments de la grille. J'ai joint mes fichiers avec ceci. en attente de votre aide, Merci!

Grid_single.java

Button btnClosePopup; 
    Button btnCreatePopup; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.grid_single); 
     btnCreatePopup = (Button) findViewById(R.id.addToCart); 
     btnCreatePopup.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       initiatePopupWindow(); 
      } 
     }); 


     btnCreatePopup.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       initiatePopupWindow(); 
      } 
     }); 

    } 

    private PopupWindow pwindo; 
    private void initiatePopupWindow() { 
     try { 
      // We need to get the instance of the LayoutInflater 
      LayoutInflater inflater = (LayoutInflater) Grid_Single_Popup.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View layout = inflater.inflate(R.layout.screen_popup, (ViewGroup) findViewById(R.id.popup_element)); 
      pwindo = new PopupWindow(layout, 300, 370, true); 
      pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0); 

      btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup); 
      btnClosePopup.setOnClickListener(cancel_button_click_listener); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private OnClickListener cancel_button_click_listener = new OnClickListener() { 
     public void onClick(View v) { 
      pwindo.dismiss(); 

     } 
    }; 

grid_single.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_marginTop="15dp" 
       android:padding="5dp" 
       android:id="@+id/grid_single_back" 
     > 
    <ImageView 
      android:id="@+id/grid_image" 
      android:layout_width="150dp" 
      android:layout_height="150dp"> 
    </ImageView> 

    <TextView 
      android:id="@+id/grid_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="2dp" 
      android:textSize="24dp" > 
    </TextView> 
    <Button 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="wrap_content" 
      android:layout_height="35dp" 
      android:text="Cutomize &amp; Add" 
      android:id="@+id/addToCart" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginTop="5dp" 
      android:layout_marginRight="5dp" 
      android:textSize="18dp" 
      android:padding="5sp"/> 

</LinearLayout> 

Merci à l'avance

+0

http://codex2android.blogspot.in/2015/ 11/popupwindow-with-image-and-text-android.html, utilisez ceci comme référence et faites la conception et l'implémentation dans l'adaptateur .. – HourGlass

+0

Regardez ici http://developer.android.fr/guide/topics/ui /layout/gridview.html –

+0

Je n'ai pas de problème avec gridview. mon problème est avec la fenêtre pop-up sur gridview .. –

Répondre

3

Cela fonctionne pour moi, vérifier une fois par

adaptateur de réseau:

public CustomGrid(String[] web, int[] Imageid, Grid_single activity) { 
    this.Imageid = Imageid; 
    this.web = web; 
    this.activity = activity; 
} 

Dans la méthode getView de votre adaptateur, gardez comme ceci:

grid = inflater.inflate(R.layout.grid_single, null); 
TextView textView = (TextView) grid.findViewById(R.id.grid_text); 
ImageView imageView = (ImageView) grid.findViewById(R.id.grid_image); 
Button btnPopup = (Button) grid.findViewById(R.id.btnPopup); 
btnPopup.setOnClickListener(activity); 

Grid_single devrait mettre en œuvre OnClickListener et garder comme ceci:

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.btnPopup: 
     initiatePopupWindow(web[grid.getPositionForView(v)]); 
     break; 
    default: 
     break; 
    } 
} 
+0

Salut, pouvez-vous m'a envoyé votre exemple de travail? –

+1

J'ai gardé mon code d'exemple à 'http://hastebin.com/qeloveyufu.avrasm' vérifiez ceci et faites le moi savoir en cas de doute. – Srikanth

+1

Merci pour l'exemple de code, j'ai reçu de l'aide. –