开发者

Android: Handling events in menu

开发者 https://www.devze.com 2023-01-19 07:01 出处:网络
I am trying to implement smth like context menu but with icons public class MenuItem { privateint type;

I am trying to implement smth like context menu but with icons

public class MenuItem {
    private  int type; 
    private String nameItem;
    private int imageResource;
}
menu.xml 
// ImageView ant TextView

public final class  MenuItemProvider {
//create our menu items
}

   public class MenuListAdapter extends ArrayAdapter<MenuItem> {
 private Activity context;
 private List<MenuItem> menuItems;

 public MenuListAdapter(Activity context, List<MenuItem> menuItems) {
        super(context, R.menu.menudlg, menuItems);

        this.context = context;
        this.menuItems = menuItems;
      }
 @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayou开发者_开发技巧tInflater();
    View row = inflater.inflate(R.menu.menudlg, parent, false);

    TextView label = (TextView) row.findViewById(R.id.text_item);
    label.setText(menuItems.get(position).getName());

    ImageView icon = (ImageView) row.findViewById(R.id.icon_item);
    icon.setImageResource(menuItems.get(position).getImageResource());

    return row;
  }

}

Question I don't understand how to handle event in this menu(event of filled menu item), one of the ways send in builder.setAdapter dialogListener ,but i don't know how create them

    listView = getListView();        
    listView.setOnItemClickListener(new OnItemClickListener() {
     @Override
    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
       AlertDialog.Builder builder = new AlertDialog.Builder(context);
       builder.setTitle(title);
       builder.setAdapter(new AccountsTypesListAdapter(context, AccountTypesProvider.accountTypes), dialogListener);
    builder.create().show();
    }


I don't follow your approach here. Have you read Creating Menus?

All you should have to do is define your menu xml file, inflate it in onCreateOptionsMenu and handle the menu selection in onOptionsItemSelected.

I'm not sure why you have an ArrayAdapter involved.

0

精彩评论

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