0

在我的代码中,我想添加一个上下文菜单。我有一个来自我的数据库的列表。我有一个名为MONEY的表,它有四个字段:IDFIRSTNAMEMONEYDESCRIPTION。我的列表视图工作正常,但上下文菜单不起作用。问题出在onContextItemSelected()方法上。当我长按列表项时,它不显示上下文菜单。

上下文菜单.xml

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

<item
    android:id="@+id/Delete"
    android:title="Delete"/>

</Menu>

钱.java

package com.anurag.main;

public class Money extends Activity {
ArrayList list1, list2, list3, list4;
TextView text1, text2, text3, text4;
ImageView iv;
ListView mList;
SQLiteDatabase db;
private static String DBNAME = "MainDB.db";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.money);
    mList = (ListView) findViewById(R.id.mList);
    list1 = new ArrayList();
    list2 = new ArrayList();
    list3 = new ArrayList();
    list4 = new ArrayList();
    try {
        db = openOrCreateDatabase(DBNAME, MODE_WORLD_READABLE, null);
        Cursor c = db.rawQuery("select * from MONEY", null);
        while (c.moveToNext()) {
            list1.add(c.getString(1));
            list2.add(c.getString(3));
            list3.add(c.getString(5));
            list4.add(c.getString(4));
        }
    } catch (Exception e) {
        // TODO: handle exception
    }
//Problem in this code 
    mList.setAdapter(new MyArrayAdapter(getApplicationContext()));

    mList.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v,
                ContextMenuInfo menuInfo) {
            // TODO Auto-generated method stub
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.contextmenu, menu);

        }
    });

    //end here
}
//problem in this code
@Override
public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item 
            .getMenuInfo(); 
    switch (item.getItemId()) {
    case R.id.Delete:
        Toast.makeText(getApplicationContext(), "Deleted",    Toast.LENGTH_LONG).show();
        return true;

    default:
        Toast.makeText(getApplicationContext(), "HI", Toast.LENGTH_LONG).show();
    }
    return super.onContextItemSelected(item);
}
//End Here
class MyArrayAdapter extends BaseAdapter {
    Context context;

    public MyArrayAdapter(Context c) {
        context = c;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        // return 0;
        return list1.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        // View v = inflater.inflate(R.layout.custom1, null);
        convertView = inflater.inflate(R.layout.custom1, null);
        text1 = (TextView) convertView.findViewById(R.id.text1);
        // text1.setText(list1[position]);
        text1.setText(list1.get(position).toString());
        text2 = (TextView) convertView.findViewById(R.id.text2);
        text2.setText(list2.get(position).toString());
        text3 = (TextView) convertView.findViewById(R.id.text3);
        text3.setText(list3.get(position).toString());
        text4 = (TextView) convertView.findViewById(R.id.text4);
        text4.setText(list4.get(position).toString());
        return convertView;
    }

}

}
4

1 回答 1

1

敢打赌,错误是在解析 .xml 文件时,它是“期待菜单,得到菜单”。去资本化<Menu></Menu>contextmenu.xml看看是否能解决它。

于 2012-10-19T15:41:08.383 回答