【问题标题】:Fragment accessing another Java method片段访问另一个 Java 方法
【发布时间】:2017-10-28 11:06:39
【问题描述】:

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

        }

【问题讨论】:

  • Fragment Note & todolist 扩展了什么? @AkshraGupta,他们也有相同的活动吗?如果是,请参阅:stackoverflow.com/questions/5297842/…
  • @AshishRanjan 感谢您提供链接。虽然它没有回答我的问题,但它把我引向了另一个我找到答案的问题。
  • 在使用片段时,我不得不使用 android.view.MenuItem,而不是使用 MenuItem。

标签: java android android-fragments


【解决方案1】:

使用.equals() 方法比较字符串。由于这个原因,您的if 条件变为false。所以替换

item.getTitle()=="Delete"

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

【讨论】:

    【解决方案2】:

    要比较String,您需要equals()matches() 方法。

    因此,在您的情况下,您需要更正以下if 条件。

    if (item.getTitle().matches("Delete")) {
    
    }
    

    或者在另一种情况下,你可以使用

    if (item.getTitle().equals("Delete")) {
    
    }
    

    【讨论】:

    • 如果 getTitle 返回 null,item.getTitle().equals("Delete") 将给出 NullPointerExceptionmatches() 不用于比较确切的字符串。它用于正则表达式。如果getTitle 返回类似.+ 的内容,那么您的条件将为真。看到这个:"Delete".matches(".+") 返回 true。
    • @Kaushal28 因此,您应该保留一个条件来检查item.getTitle() != null,然后在其中实现上述条件。另外,请确保将其写在 try/catch 块下
    • 但不需要 try catch 和额外的条件,如果你这样写:"Delete".equals(item.getTitle());
    • @Kaushal28 正如我在您的情况下所说的,您可以使用.equals()。希望对你有帮助
    • 当我在 todolist 片段中实现该方法时,== 可以完美运行。问题是笔记片段正在实现待办片段的 OnContextItemSelected 而不是笔记本身
    猜你喜欢
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 2018-03-20
    相关资源
    最近更新 更多