繁体   English   中英

如何在Android中将popupMenu添加到listView的项目?

[英]How can add popupMenu to items of listView in Android?

我想在Android中显示onLongClick上的popupMenu,所以我这样写:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    listView = (ListView) findViewById(R.id.listView);
    adapter = new MyAdapter(getApplicationContext());

}

@Override
protected void onResume() {
    super.onResume();
    connectToDatabase();
}

private void connectToDatabase() {
    //Create database
        listView.setAdapter(adapter);
    }

我的适配器:

        @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final MyHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_row, null);
            holder = new MyHolder();
            holder.vocabs = (TextView) convertView.findViewById(R.id.textViewVocabs);
            holder.points = (TextView) convertView.findViewById(R.id.textViewPoints);
            holder.tick = (ImageButton) convertView.findViewById(R.id.tick);
            holder.cross = (ImageButton) convertView.findViewById(R.id.cross);
            convertView.setTag(holder);
        } else {
            holder = (MyHolder) convertView.getTag();
        }

        holder.id = id.get(position);
        holder.vocabs.setText(vocabs.get(position));
        holder.points.setText(points.get(position) + "");

        holder.tick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(parent.getContext(), "Yes... :D ... You learn it :D", Toast.LENGTH_SHORT).show();
                points.set(position, points.get(position) + 1);
                database.execSQL("UPDATE VOCABS SET POINTS=" + points.get(position) + " WHERE ID=" + id.get(position) + ";");
                notifyDataSetChanged();
            }
        });

        holder.cross.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(parent.getContext(), "Yes... :D ... You learn it :D", Toast.LENGTH_SHORT).show();
                if (points.get(position) != 0) {
                    points.set(position, points.get(position) - 1);
                    database.execSQL("UPDATE VOCABS SET POINTS=" + points.get(position) + " WHERE ID=" + id.get(position) + ";");
                }
                notifyDataSetChanged();
            }
        });

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(parent.getContext(), means.get(position), Toast.LENGTH_LONG).show();
            }
        });

        final View finalConvertView = convertView;
        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //popup-menu for delete item.
                PopupMenu popupMenu = new PopupMenu(parent.getContext(),finalConvertView);
                popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
                popupMenu.show();

                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        if (item.getTitle().equals("Delete")) {
                            database.execSQL("DELETE FROM VOCABS WHERE ID=" + holder.id + ";");
                            id.remove(position);
                            vocabs.remove(position);
                            means.remove(position);
                            points.remove(position);
                            notifyDataSetChanged();
                        }
                        return false;
                    }
                });
                //Toast.makeText(parent.getContext(), "onLongClickFunction! " + position, Toast.LENGTH_LONG).show();
                return false;
            }
        });
        return convertView;
    }

我使用Itellij IDEA 13.1.4。 我添加了appcompat以在11之前的API上支持popup_Menu,现在当我运行应用程序时,一切正常。 但是当长按listView中的项目时,出现以下错误:

04-01 15:50:29.749  17078-17078/net.motameni.apps.vocabs_box E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: net.motameni.apps.vocabs_box, PID: 17078
java.lang.RuntimeException: Failed to resolve attribute at index 6
        at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:603)
        at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6423)
        at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6591)
        at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:735)
        at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:679)
        at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:62)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.support.v7.internal.view.menu.MenuPopupHelper$MenuAdapter.getView(MenuPopupHelper.java:370)
        at android.support.v7.internal.view.menu.MenuPopupHelper.measureContentWidth(MenuPopupHelper.java:219)
        at android.support.v7.internal.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:153)
        at android.support.v7.internal.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:125)
        at android.support.v7.widget.PopupMenu.show(PopupMenu.java:193)
        at net.motameni.apps.vocabs_box.MainActivity$MyAdapter$4.onLongClick(MainActivity.java:154)
        at android.view.View.performLongClick(View.java:4795)
        at android.view.View$CheckForLongPress.run(View.java:19723)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

怎么了???

我怀疑您在调用PopupMenu构造函数时无法获得正确的视图。

更改代码自:

PopupMenu popupMenu = new PopupMenu(parent.getContext(),finalConvertView);

至:

PopupMenu popupMenu = new PopupMenu(parent.getContext(), v);

我承认也许是相同的观点,但取决于布局设计。 TO下的建议代码更加准确。 View参数v指向ListView中的某个行项目。

请同时发布您的菜单XML R.menu.popup_menu。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM