gpt4 book ai didi

java - fragment 访问另一个 Java 方法

转载 作者:行者123 更新时间:2023-12-02 12:04:40 25 4
gpt4 key购买 nike

我的 fragment 注意正在访问 fragment 待办事项列表的删除方法,而不是执行其自己的代码行。我怎样才能纠正它?其余方法工作正常。

哪里出了问题?

fragment中删除代码注:

@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);

}

fragment 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);

}

最佳答案

使用.equals()方法来比较字符串。因此,您的 if 条件变得 false。所以替换

item.getTitle()=="Delete"

"Delete".equals(item.getTitle());

关于java - fragment 访问另一个 Java 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46989016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com