从OncontextItemSelected

时间:2016-06-27 12:09:19

标签: java android

这是我项目的代码,其中我有对象'mProductList'的列表视图。我在列表视图的每个项目上设置了一个onclickListener,它将打开一个包含三个选项的菜单。现在进行编辑我想更改所选项目中的变量并将其显示在屏幕上。我是新来的,所以请不要低估这一点。任何帮助将不胜感激stackoverflow。如果您需要我的更多代码,请现在就告诉我。

     ProductListAdapter adapter = new ProductListAdapter(getApplicationContext(), mProductList);
        lvProduct.setAdapter(adapter);

        lvProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Do something
                Toast.makeText(getApplicationContext(), " Clicked product id = " + view.getTag(), Toast.LENGTH_SHORT).show();

                registerForContextMenu(lvProduct);
                openContextMenu(lvProduct);

            }
        });

    }
    final int CONTEXT_MENU_ADD =1;
    final int CONTEXT_MENU_EDIT =2;
    final int CONTEXT_MENU_ARCHIVE =3;
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
        //Context menu
        menu.setHeaderTitle("My Context Menu");
        menu.add(0, CONTEXT_MENU_ADD, 0, "Add");
        menu.add(0, CONTEXT_MENU_EDIT, 0, "Edit");
        menu.add(0, CONTEXT_MENU_ARCHIVE, 0, "Delete");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch(item.getItemId())
        {
            case CONTEXT_MENU_ADD:
            {

            }
            break;
            case CONTEXT_MENU_EDIT:
            {
                // Edit Action

            }
            break;
            case CONTEXT_MENU_ARCHIVE:
            {

            }
            break;
        }

        return super.onContextItemSelected(item);
    }
}

这是我的Product类,其对象在列表视图中生成并显示:

public class Product {
    private int id;
    private String name;
    private int amount;
    //private String description;

    //Constructor

    public Product(int id, String name, int amount) {
        this.id = id;
        this.name = name;
        this.amount = amount;

    }


    //Setter, getter

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAmount() {
        return amount;
    }

    public void setPrice(int amount) {
        this.amount = amount;
    } 

0 个答案:

没有答案