私のコードでは、コンテキスト メニューを追加したいと考えています。データベースから取得したリストがあります。ID、FIRSTNAME、MONEY、およびDESCRIPTIONの4 つのフィールドを持つMONEYという名前のテーブルがあります。リスト ビューは正常に機能していますが、コンテキスト メニューが機能していません。問題は方法にあります。リストアイテムを長押ししてもコンテキストメニューが表示されません。onContextItemSelected()
contextmenu.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>
Money.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;
}
}
}