开发者

Detect the GridView item selected in the method onContextItemSelected

I have a GridView with an ArrayAdapter and I'd like to detect the context item selection and show a "Delete" option for deleting the object selected开发者_如何学Go. I fill the grid with imageviews correctly only need the detect the delete petition. My Code:

ArrayList<MyClass> array;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.maingrid);
    array=Manager.getMyArray();
    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new mArrayAdapter(this,array) );
    registerForContextMenu(gridview);

}

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

    menu.add(0, DELETE_ID, 0 , R.string.delete);

}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case DELETE_ID:
            return true;
    }
    return super.onContextItemSelected(item);
}

How can I guess whats the element of the array I have to remove? Thanks


In onContextItemSelected try this:

AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

you can get the selected item like this:

youradapter.getItem((int)info.id))

override the getItem() function in the adapter to return the selected item..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜