如何从ArrayAdapter中注册ForContextMenu

时间:2013-02-21 20:21:56

标签: android

我正在显示消息列表,使用显示来自Adaptar的ListView的Activity,如下所示。

我需要在下面DiscussArrayAdapter类的ImageView中注册ContextMenu菜单。

如果我只是从registerForContextMenu(arrowImage);致电DiscussArrayAdapter,我就不会接受它。

另外,我不知道在哪里覆盖方法onCreateContextMenu

我的messageActivity如下:

public class MessagesActivity extends Activity  {
        private ListView listView = null;

        /** Called when the activity is first created. */
        private DiscussArrayAdapter adapter;
        private ListView lv;
@Override
       public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_discuss);

            registerReceiver(mHandleMessageReceiver, new IntentFilter(
                            DISPLAY_MESSAGE_ACTION));

            adapter = new DiscussArrayAdapter(getApplicationContext(), R.layout.listitem_discuss);

            lv.setAdapter(adapter);

我有DiscussArrayAdapter课程:

public class DiscussArrayAdapter extends ArrayAdapter<OneMessage>{

    private /*TextView*/EditText msgTextView;
    private  TextView sender;

    private LinearLayout wrapper;

    private SqlHelper dbHelper = null;
    private Context context;
    @Override
    public void add(OneMessage object) {

        dbHelper.addNewMessage(object);
        super.add(object);
    }

    public DiscussArrayAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        this.context=context;

        if (dbHelper == null) {
            dbHelper = new SqlHelper(context);

        }


    }

    public int getCount() {

        return  dbHelper.getContactsCount();
    }

    public OneMessage getItem(int index) {

        return dbHelper.getOneComment(index+dbHelper.lastRawID());
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.listitem_discuss, parent, false);
        }

        wrapper = (LinearLayout) row.findViewById(R.id.wrapper);

        ImageView arrowImage = (ImageView) row.findViewById(R.id.aquaplayicon);
    //  this .registerForContextMenu(arrowImage);
        OneMessage coment = getItem(position);

        msgTextView =/*(TextView)*/(EditText) row.findViewById(R.id.comment);

为简洁起见,我省略了其余的代码。

2 个答案:

答案 0 :(得分:3)

context菜单函数都是为类Activity定义的,Adapter类不存在。

因此,您需要将adapter作为activity的子类,或者您需要将activity传递给adapter,以便它可以调用注册函数,活动将需要处理context菜单(或将事件传递给适配器来处理它)。

答案 1 :(得分:3)

您必须使用Activity类转换上下文变量:

private Context context;

public ExpandibleListViewAdapter(Context context) {
        this.context = context;
    }

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parentView) {

    if (convertView == null) {
       ......
    }

    .........

    ((Activity)context).registerForContextMenu(convertView);

    return convertView;
}