1

我正在显示消息列表,使用显示来自 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);

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

4

2 回答 2

4

context菜单功能都是为 class 定义的,Activity它们不存在于Adapterclass 中。

所以要么你需要让你adapter的子类activity,或者你需要传递activity给它adapter以便它可以调用注册函数,并且活动将需要处理context菜单(或将事件传递给适配器来处理它) .

于 2013-02-21T20:23:46.093 回答
4

您必须使用 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;
}
于 2015-02-16T22:56:37.637 回答