• Android上下文菜单


    package com.bawei.d4;
    
    
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.ContextMenu;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ContextMenu.ContextMenuInfo;
    import android.widget.Adapter;
    import android.widget.AdapterView;
    import android.widget.AdapterView.AdapterContextMenuInfo;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.BaseAdapter;
    import android.widget.ListView;
    
    public class MainActivity extends Activity {
     public static String[] name=new String[]{
            "爱国者(aigo) MP5数码播放器U303 8GB 触摸屏幕MP3 白色",
            "雪曼2014新款女包OL职业单肩包 女手提包斜挎包包8305 米白色",
            "长袖连衣裙雪纺裙修身气质女欧美长款帝弗润驰森女拖地大牌仙长裙",
            "韩国文具 可爱 创意 时尚 办公用品 大 多功能 收纳盒 笔筒包邮",
            "ThinkPad E531 6885-D4C 赛扬双核 500G 联想 IBM商务 笔记本电脑",
            "现货包邮顺丰 Nokia/诺基亚 107 双卡双待 老人备用手机 超长待机",
            "3条装 北极绒吊带背心女打底 新款内搭黑色吊带 莫代尔蕾丝背心",
            "[标配送8G卡]Sony/索尼 HDR-CX240E 数码摄像机/DV高清专业摄像机",
            "天然白蓝绿玉髓水晶耳环男女925银耳钉黑玛瑙耳饰防过敏白银饰品",
            "2014韩版欧根纱连衣裙新款夏女刺绣公主蓬蓬裙粉红色假两件套270",
            "杭依阁2014夏装新款韩版修身淑女连衣裙女装蕾丝雪纺时尚连衣裙潮",
            "HP/惠普 Envy 15 Envy 15-j105TX 四代i7高分屏8G内存/1T/4G独显",
            "卡西欧CASIO时尚石英手表男士皮带手表MTP-1095Q-7B",
            "正版hellokitty公仔KT毛绒玩具凯蒂猫布娃娃玩偶七夕生日礼物女生",
            "正品卡通宝宝相框二连情侣挂式相架kt米奇维尼熊史努比儿童照片墙"  
          
     };
    
     
     public static String[] price=new String[]{
          "450", "1450", "150", "45",
            "3999", "99", "129", "4999", "899", "270", "298", "6999", "1299",
            "29", "19" 
     };
    public static int[] image=new int[]{
          R.drawable.mp4, R.drawable.bag,
            R.drawable.bqz, R.drawable.bt, R.drawable.ibm,
            R.drawable.nokia3310, R.drawable.dd, R.drawable.dv, R.drawable.eh,
            R.drawable.hqz, R.drawable.hqz, R.drawable.hs, R.drawable.watch,
            R.drawable.wj, R.drawable.xk
    };
    
    
    private int     position;
    
    
    private List<Goods> list;
    
    
    private Goods g;
    
    
    private BaseAdapter adapter;
    
    
    private ListView listview;    
        
        
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listview = (ListView) findViewById(R.id.listview);
            adapter = new Activitylist(MainActivity.this,getCon());
            listview.setAdapter(adapter);
            
            registerForContextMenu(listview);
            
            
        }
    
     @Override
        public void onCreateContextMenu(ContextMenu menu, View v,
                ContextMenuInfo menuInfo) {
            // TODO Auto-generated method stub
            super.onCreateContextMenu(menu, v, menuInfo);
            menu.add(0,1, Menu.NONE, "增加条目 ");
            menu.add(0,2, Menu.NONE,"删除条目");
            menu.add(0, 3,Menu.NONE, "修改条目");
            menu.add(0, 4, Menu.NONE, "查询条目");
            menu.setHeaderIcon(R.drawable.ic_launcher);
            menu.setHeaderTitle("上下文菜单");
        } 
       @Override
        public boolean onContextItemSelected(MenuItem item) {
            // TODO Auto-generated method stub
           
           AdapterContextMenuInfo MenuInfo=(AdapterContextMenuInfo) item.getMenuInfo();
            position = MenuInfo.position;
            switch (item.getItemId()) {
            case 1:
               g.setImage(R.drawable.ic_launcher);
               g.setName("安卓");
               g.setPrice("200");
               list.add(g);
               adapter.notifyDataSetChanged();
                break;
            case 2:
                list.remove(position);
                 adapter.notifyDataSetChanged();
                break;
            case 3:
                
                
                break;
            case 4:
                  listview.setOnItemClickListener(new OnItemClickListener() {
    
                      @Override
                      public void onItemClick(AdapterView<?> arg0, View view, int arg2,
                              long arg3) {
                          // TODO Auto-generated method stub
                          Intent intent = new Intent(MainActivity.this, ShowActivity.class);
                          intent.putExtra("image",image[arg2] );
                          intent.putExtra("name",name[arg2] );
                          intent.putExtra("price",price[arg2] );
                          startActivity(intent);
                      }
                  });
        
               break;
            }
            
            
            
            return super.onContextItemSelected(item);
            
        } 
        
        
        
      private List<Goods> getCon(){
          list = new ArrayList<Goods>();
          for (int i=0;i<image.length;i++) {
            g = new Goods();
            g.setImage(image[i]);
            g.setName(name[i]);
            g.setPrice(price[i]);
            list.add(g);
            
        }
        return list;
      }
    }
    复制代码
  • 相关阅读:
    Linux性能监控
    程序员技术练级攻略
    使用 GDB 调试多进程程序
    nginx下面部署fast-cgi和C++【原】
    ROS Learning-024 (提高篇-002) rviz的安装和使用
    ROS Learning-023 (提高篇-001) 准备工作 --- 安装一些必要的软件包
    STM32 C++编程 005 I2c(Soft)类
    Python 网络爬虫 005 (编程) 如何编写一个可以 下载(或叫:爬取)一个网页 的网络爬虫
    设置 PyCharm 软件中 Terminal 窗口 中启动的 python 交互命令的版本
    在PyCharm 软件中设置你的项目 使用的Python版本
  • 原文地址:https://www.cnblogs.com/3674-it/p/5567244.html
Copyright © 2020-2023  润新知