我创建了一个上下文菜单。当我长按列表项时,会出现上下文菜单。到现在为止还挺好...
但是当我点击一个上下文项时,什么也没有发生。有谁知道这个问题?这里有什么问题?
使用 listview 打开对话框的按钮:
Button cmd_fav = (Button) findViewById(R.id.cmd_main_fav);
cmd_fav.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
List<String> valueList = new ArrayList<String>();
db = SQLiteDatabase.openDatabase("/data/data/spicysoftware.abugrundwissen/databases/questions", null,
SQLiteDatabase.OPEN_READWRITE);
Cursor c_ = db.rawQuery("SELECT question, _id, answer FROM tbl_questions"+
" where favourite = 1", null);
if (c_ != null ) {
if (c_.moveToFirst()) {
do {
String str_question = c_.getString(c_.getColumnIndex("question"));
valueList.add(str_question);
} while (c_.moveToNext());
}
// custom dialog
dialog = new Dialog(MainSite.this);
dialog.setContentView(R.layout.dialog_list);
dialog.setTitle("Favoriten:");
adapter = new ArrayAdapter<String>(MainSite.this, android.R.layout.simple_list_item_1, valueList);
final ListView lv = (ListView)dialog.findViewById(R.id.list_search);
lv.setAdapter(adapter);
registerForContextMenu(lv);
lv.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.add(Menu.NONE, CONTEXT_MENU_DELETE_ITEM, Menu.NONE, "Favorit entfernen");
menu.add(Menu.NONE, CONTEXT_MENU_FINISH_ITEM, Menu.NONE, "Frage abschliessen!");
}
});
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
String item = (String) lv.getItemAtPosition(position).toString();
Cursor c_2 = db.rawQuery("SELECT answer FROM tbl_questions"+
" where question = '"+item+"'", null);
if (c_2 != null ) {
if (c_2.moveToFirst()) {
answer = c_2.getString(c_2.getColumnIndex("answer"));
}
}
// custom dialog
final Dialog dialog = new Dialog(MainSite.this);
dialog.setContentView(R.layout.dialog_answer);
dialog.setTitle("Antwort:");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.txt_answer);
//text.setText(answer);
text.setText(Html.fromHtml(answer), TextView.BufferType.SPANNABLE);
Button dialogButton = (Button) dialog.findViewById(R.id.cmd_close_dialog);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
Button dialogButton = (Button) dialog.findViewById(R.id.cmd_close_dialog2);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
});
OnContextItemSelected:
@Override
public boolean onContextItemSelected(MenuItem item) {
Log.v("tst", "lol");
switch (item.getItemId()) {
case CONTEXT_MENU_DELETE_ITEM:
Log.v("DELETED", "TRUE");
return true;
case CONTEXT_MENU_FINISH_ITEM:
Log.v("FINISHED", "TRUE");
return true;
}
Log.v("FINISHED", "LOL");
return false;
}
最好的问候 MSeiz5