片段访问另一个Java方法

时间:2017-10-28 11:06:40

标签: java android android-fragments

我的片段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);

        }

2 个答案:

答案 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")) {

}
相关问题