码迷,mamicode.com
首页 > 移动开发 > 详细

Android上下文菜单

时间:2016-06-07 16:04:48      阅读:439      评论:0      收藏:0      [点我收藏+]

标签:

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;
  }
}
技术分享

Android上下文菜单

标签:

原文地址:http://www.cnblogs.com/3674-it/p/5567244.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!