[英]Fragment accessing another Java method
我的片段Note
正在訪問片段待辦事項列表的delete方法,而不執行其自己的代碼行。 我該如何糾正? 其余方法可以正常工作。
哪里出了問題?
片段中的刪除代碼注意:
@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);
}
片段待辦清單代碼:
@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);
}
使用.equals()
方法比較字符串。 由於這個原因,你的if
條件越來越false
。 所以更換
item.getTitle()=="Delete"
同
"Delete".equals(item.getTitle());
要比較String
您需要equals()
或matches()
方法。
因此,根據您的情況,您需要更正以下if
條件。
if (item.getTitle().matches("Delete")) {
}
或者在另一種情況下,您可以使用
if (item.getTitle().equals("Delete")) {
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.