如何获得项目的位置从popupMenu的一个自定义的ListView [英] How to get the position of an Item from a PopupMenu in a custom ListView

查看:508
本文介绍了如何获得项目的位置从popupMenu的一个自定义的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的问题:我有一个自定义ArrayAdapter(重写getView)。每个项目都得到了

Here's the problem: I've got a custom ArrayAdapter (overriden getView). Each item has got

  • 2的ImageView
  • 2的EditText

其中的一个ImageView的是可以点击的,使PopupMenu的,所以我有一个小的PopupMenu对列表中的每个项目。现在,该菜单,我需要从项目的一些参数至极锚定。因此,如何通过信息(如位置)从PopupMenu的声音调用的方法?附加的XML文件。

One of those ImageView is clickable and enables a PopupMenu, so I've got a little PopupMenu for each item of the list. Now, for that menu I need some parameters from the item to wich is anchored. So, how to pass informations (like position) to the method called from the PopupMenu voice? Attached the xml files.

item_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="6dip"
        android:contentDescription="TODO"
        android:src="@drawable/ic_launcher" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/firstLine"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:maxHeight="60dp"
            android:text="Title"
            android:textSize="16sp" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:contentDescription="TODO"
            android:onClick="showMenu"
            android:src="@drawable/abc_ic_menu_moreoverflow_normal_holo_light" />

        <TextView
            android:id="@+id/secondLine"
            android:layout_width="fill_parent"
            android:layout_height="26dip"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/firstLine"
            android:ellipsize="marquee"
            android:singleLine="true"
            android:text="Author"
            android:textSize="12sp" />

    </RelativeLayout>

</LinearLayout>

menu_item.xml

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

        <item
            android:id="@+id/preview"
            android:onClick="preview"
            android:title="@string/preview"/>
        <item
            android:id="@+id/download"
            android:onClick="download"
            android:title="@string/download"/>

    </menu>

CustomArrayAdapter.java

public class CustomArrayAdapter extends ArrayAdapter<Custom>{

    private final Context context;
    private final List<Custom> values;
    private final int resource;
    private final ImageCache imgCache = new ImageCache();

    public CustomArrayAdapter(Context context, int resource, List<Custom> values) {
        super(context, resource, values);
        this.context = context;
        this.values = values;
        this.resource = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        Custom Custom = values.get(position);
        if (convertView == null) {
            vh = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(resource, parent, false);
            vh.title = (TextView) convertView.findViewById(R.id.firstLine);
            vh.author = (TextView) convertView.findViewById(R.id.secondLine);
            vh.albumImage = (ImageView) convertView.findViewById(R.id.icon);
            convertView.setTag(vh);
        }
        else {
            vh = (ViewHolder)convertView.getTag();
        }
        vh.requestedCustom = Custom;
        vh.title.setText(Custom.getTitle());
        vh.author.setText(Custom.getAuthor());
        String imgUrl = Custom.getImgUrl();
        if (imgUrl != null) {
            imgCache.getImage(Custom, vh);
        }
        else {
            vh.albumImage.setImageResource(R.drawable.no_album);
        }
        return convertView;
    }

    // This is a ViewHolder that helps in the view recycling
    public static class ViewHolder {
        public Custom requestedCustom;
        public TextView title;
        public TextView author;
        public ImageView albumImage;
    }
}

preVIEW和下载的方法需要从相关的列表项的参数,我不知道如何让他们。再次感谢。

"preview" and "download" methods need parameters from the associated list item and I don't know how to get them. Thanks again.

修改1

这似乎应该使用上下文菜单,而不是(对吗?),但我想它锚定到每一个项目作为一种出现弹出菜单。那么,如何实现这一目标? 对于那些谁不明白我想要什么(由于我最糟糕的英文):谷歌Play应用,弹出菜单,让你安装或添加到whishlist之间进行选择。

It seems that a Contextual Menu should be used instead (right?) but I would like to anchor it to each item as a sort of popup menu that appears. So how can I achieve this? For those who don't understand what I want (due to my worst english): Google Play app, popup menu that let you choose between "Install" or "Add to whishlist".

推荐答案

您可以创建监听器类是这样的:

You can create your listener class like this:

class myClickListener implements OnClickListener, OnMenuItemClickListener {

        long id;

        public myClickListener(long id) {
            this.id = id;
        }
        @Override
        public void onClick(View v) {
            PopupMenu popup = new PopupMenu(appContext, v);
            popup.setOnMenuItemClickListener(this);
            MenuInflater inflater = popup.getMenuInflater();
            inflater.inflate(R.menu.menu_item, popup.getMenu());
            popup.show();
        }
        @Override
        public boolean onMenuItemClick(MenuItem arg0) {
            Toast.makeText(appContext, "ID of selected row : " + id , Toast.LENGTH_SHORT).show();
            return false;
        }

    }

和添加此监听到你的ImageView的适配器类的重写getView方法:

and add this listener to your imageview at your override getView method of adapter class:

imageView.setOnClickListener(new myClickListener(getItemId(position)));

这篇关于如何获得项目的位置从popupMenu的一个自定义的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆