带列表视图的上下文菜单

时间:2017-07-06 18:19:53

标签: android

我的代码没有显示任何Toast。当我长按它显示三个菜单但是当我选择任何菜单如“删除”它不显示Toast时,我能做什么使它工作。我试过在这个网站上的答案,但没有成功。这是我的代码。

 package com.example.avinashkumaryadav.menutwo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
ListView lview;
String[] contact={"Avinash","Golu","Sam","Tom","Ram","Yam"};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lview=(ListView)findViewById(R.id.linearId) ;
    ArrayAdapter Array=new ArrayAdapter<String>(this,R.layout.linear_view,R.id.linearView,contact);
    lview.setAdapter(Array);
    registerForContextMenu(lview);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    menu.setHeaderTitle("Select Action");
    menu.add(0,v.getId(),0,"Love");
    menu.add(0,v.getId(),0,"Edit");
    menu.add(0,v.getId(),0,"delete");
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getTitle()=="Love") {
        Toast.makeText(MainActivity.this, "Love u too dear", Toast.LENGTH_LONG).show();
    }
    else if(item.getTitle()=="Edit")
    {
        Toast.makeText(getApplicationContext(),"Editable",Toast.LENGTH_SHORT).show();
    }
    else if(item.getTitle()=="delete")
    {
        Toast.makeText(getApplicationContext(),"Can be Deleted",Toast.LENGTH_SHORT).show();
    }
  else
       {
          Toast.makeText(this,"Not clicked right",Toast.LENGTH_SHORT);
       }
       return true;

   }
 }

Cany有人帮我吗?

3 个答案:

答案 0 :(得分:2)

在Java中,您必须将字符串与.equals()进行比较,而不是==。

此外,它看起来像是在膨胀ContextMenu。您需要实现onContextItemSelected(MenuItem项)而不是onOptionsItemSelected(MenuItem)。

看看这里:https://developer.android.com/guide/topics/ui/menus.html并向下滚动以创建一个浮动上下文菜单。

答案 1 :(得分:0)

试试这个:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getTitle().equals("Love")) {
        Toast.makeText(this, "Love u too dear", Toast.LENGTH_LONG).show();
    }
    else if(item.getTitle().equals("Edit"))
    {
        Toast.makeText(ghis,"Editable",Toast.LENGTH_SHORT).show();
    }
    else if(item.getTitle().equals("delete"))
    {
        Toast.makeText(this,"Can be Deleted",Toast.LENGTH_SHORT).show();
    }
  else
       {
          Toast.makeText(this,"Not clicked right",Toast.LENGTH_SHORT);
       }
       return super.onOptionsItemSelected(item);

   }

答案 2 :(得分:0)

String比较使用equals方法,您还可以将代码简化为一个toast语句:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    String message = "Not clicked right";
    int length = Toast.LENGTH_SHORT;
    String title = item.getTitle().toString();
    if ("Love".equals(title)) {
        message = "Love u too dear";
        length = Toast.LENGTH_LONG;
    } else if ("Edit".equals(title)) {
        message = "Editable";
    } else if ("delete".equals(title)) {
        message = "Can be Deleted";
    }
    Toast.makeText(this, message, length).show();
    return true;
}