0

My fragment Note is accessing the delete method of Fragment To-do List and not performing its own line of code. How can I rectify it? The rest of the methods work correctly.

Where is it going wrong?

Code of delete in fragment 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);

}

Code of 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);

        }
halfer
  • 19,824
  • 17
  • 99
  • 186
  • what does `Fragment` Note & todolist extend? @AkshraGupta , also do they share the same activity? if yes, see this : https://stackoverflow.com/questions/5297842/how-to-handle-oncontextitemselected-in-a-multi-fragment-activity – Ashish Ranjan Oct 28 '17 at 20:18
  • 1
    @AshishRanjan thank you for providing the link. Though it didn't answer my question, it led me to another question where I found the answer. – Akshra Gupta Oct 28 '17 at 23:49
  • 1
    Instead of using MenuItem, I had to use android.view.MenuItem when using fragments. – Akshra Gupta Oct 28 '17 at 23:50

2 Answers2

1

Use .equals() method to compare Strings. And due to this your if condition is getting false. So replace

item.getTitle()=="Delete"

with

"Delete".equals(item.getTitle());
Kaushal28
  • 5,377
  • 5
  • 41
  • 72
0

To compare String you need equals() or matches() method.

So, in your case, you need to correct following if condition.

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

}

Or in another scenario, you can use

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

}
Pankaj Lilan
  • 4,245
  • 1
  • 29
  • 48
  • 1
    `item.getTitle().equals("Delete")` will give `NullPointerException` if getTitle returns null. And `matches()` is not for comparing exact Strings. It's for regular expressions. if `getTitle` returns something like `.+`, then your condition will be true. See this: `"Delete".matches(".+")` returns true. – Kaushal28 Oct 28 '17 at 11:37
  • @Kaushal28 So, for that you should keep one condition of checking if `item.getTitle() != null` and then implement the above condition inside it. Also, make sure to write this under try/catch block – Pankaj Lilan Oct 28 '17 at 11:40
  • but no need of try catch and extra condition, if you write like this: `"Delete".equals(item.getTitle());` – Kaushal28 Oct 28 '17 at 11:40
  • @Kaushal28 As I said in your case you can use `.equals()`. Hope it helps – Pankaj Lilan Oct 28 '17 at 11:42
  • the == works perfectly when I am implementing the method in todolist fragment. The problem is that notes fragment is implementing the OnContextItemSelected of todo fragment rather than of notes itself – Akshra Gupta Oct 28 '17 at 14:47