我的片段Note
正在访问片段待办事项列表的删除方法,而不是执行自己的代码行。我怎样才能纠正它?其余的方法都能正常工作。
哪里出错?
片段中的删除代码注意:
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int listpos = info.position;
String io = list1.getItemAtPosition(listpos).toString();
StringTokenizer s=new StringTokenizer(io);
if (item.getTitle()=="Delete") {
ArrayList<String> all = new ArrayList<String>();
while (s.hasMoreTokens()) {
all.add(s.nextToken());
}
Toast.makeText(getContext(), all.get(0).toString(), Toast.LENGTH_SHORT).show();
String query = "delete from notes where heading = '"+all.get(0).toString()+"';";
database.execSQL(query);
//database.close();
Intent n = new Intent(getContext(), MainActivity.class);
startActivity(n);
}
片段代码To-doList:
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int listpos = info.position;
String io = list.getItemAtPosition(listpos).toString();
StringTokenizer s=new StringTokenizer(io);
if (item.getTitle()=="Delete") {
ArrayList<String> al = new ArrayList<String>();
while (s.hasMoreTokens()) {
al.add(s.nextToken());
Toast.makeText(getContext(), al.get(0).toString(), Toast.LENGTH_SHORT).show();
String query1 = "delete from todolist where elist = '" + al.get(0).toString() + "';";
database.execSQL(query1)
Intent n = new Intent(getContext(), MainActivity.class);
startActivity(n);
}
答案 0 :(得分:1)
使用.equals()
方法比较字符串。因此,您的if
条件正在false
。所以替换
item.getTitle()=="Delete"
带
"Delete".equals(item.getTitle());
答案 1 :(得分:0)
要比较String
您需要equals()
或matches()
方法。
因此,在您的情况下,您需要更正以下if
条件。
if (item.getTitle().matches("Delete")) {
}
或者在另一种情况下,您可以使用
if (item.getTitle().equals("Delete")) {
}