2015-12-21 80 views
3

我正在开发一个用于食品订购系统的餐厅的Android应用程序。
在我的项目,我有一个Gridview,当点击在GridView中项的button,我希望得到一个弹出窗口,与一些文本框,按钮等为每个gridview项目创建弹出窗口 - Android

我得到的帮助来自this document example

的上面的文档示例单独作为单个应用程序工作。它只是创建基本弹出窗口的一个示例。但我需要它在Gridview上。

my work -->

,而不是上面的文档的main.xmlPopUpWinndowDemoActivity.java(它的主要活动), 我用我的grid_single.xml和它的implementation file Grid_single.java。

但它不适用于我。当我点击每个项目的按钮时,没有显示弹出窗口,因为我希望。所以,请帮我解决这个问题,或给我一个网格视图项目弹出窗口的工作示例。 我附上了我的文件。 等待你的帮助, 谢谢!

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> 

在此先感谢

+0

http://codex2android.blogspot.in/2015/ 11/popupwindow-with-image-and-text-android.html,将其用作参考,并在适配器中执行设计和实现。 – HourGlass

+0

查看此处http://developer.android.com/guide/topics/ui /layout/gridview.html –

+0

我没有与gridview的问题。我的问题是在gridview的弹出窗口.. –

回答

3

这是为我工作,检查一次

在网格适配器:

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

在适配器的getView方法,保持这样的:

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应该实现OnClickListener,并保持这样的:

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

嗨,你可以给我你的工作示例? –

+1

我保留示例代码在'http://hastebin.com/qeloveyufu.avrasm'检查这一点,让我知道万一有任何疑问。 – Srikanth

+1

感谢您的示例代码,我从中获得了帮助。 –