为Android中每个GridView项目创建弹出窗口

3
我正在为一家餐厅开发Android应用程序,用于食品订购系统。
在我的项目中,我有一个Gridview,当单击网格视图中项目上的button时,我想要获取一个带有一些文本字段、按钮等的弹出窗口。

我从这个文档示例得到了帮助。

上面的文档示例作为单独的应用程序可以正常工作。它只是用于创建基本弹出窗口的示例,但我需要在Gridview上使用它。

我的工作-->

我使用了我的grid_single.xml和它的实现文件Grid_single.java,而不是上述文档中的main.xmlPopUpWinndowDemoActivity.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>

提前致谢


请参考以下链接:http://codex2android.blogspot.in/2015/11/popupwindow-with-image-and-text-android.html,在适配器中进行设计和实现。 - HourGlass
我对GridView没有问题。我的问题是在GridView上的弹出窗口。 - Kasun Gamage
1个回答

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;
    }
}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接