复制代码 代码如下:
/**
* 知识点1:ListVIEw item:两种长按d出菜单方式
* 知识点2:ListVIEw SimpleAdapter的使用
* 知识点 3:在java代码中创建一个ListVIEw
*/
public class ListOnLongClickActivity extends Activity {
private linearLayout myListVIEwlayout;
private ListVIEw mListVIEw;
SimpleAdapter adapter;
public int MID;
// 创建一个List对象,用来存放列表项的每一行map信息
List<Map<String,Object>> List = new ArrayList<Map<String,Object>>();
@OverrIDe
public voID onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.main);
myListVIEwlayout = (linearLayout) findVIEwByID(R.ID.myListVIEwlayout);
mListVIEw = new ListVIEw(this);
// 创建布局参数
linearLayout.LayoutParams ListvIEwParams = new linearLayout.LayoutParams(
linearLayout.LayoutParams.FILL_PARENT,
linearLayout.LayoutParams.FILL_PARENT);
// 当滑动列表上,默认显示的黑色
mListVIEw.setCachecolorHint(color.WHITE);
// 将列表添加到流式布局myListVIEwlayout中
myListVIEwlayout.addVIEw(mListVIEw,ListvIEwParams);
FillListData();
// 列表现的单机事件
mListVIEw.setonItemClickListener(new OnItemClickListener() {
@OverrIDe
public voID onItemClick(AdapterVIEw<?> arg0,VIEw arg1,
int position,long ID) {
/*
* 点击列表项时触发onItemClick方法,四个参数含义分别为
* arg0:发生单击事件的AdapterVIEw
* arg1:AdapterVIEw中被点击的VIEw
* position:当前点击的行在adapter的下标
* ID:当前点击的行的ID
*/
Toast.makeText(ListOnLongClickActivity.this,
"您选择的是" + List.get(position).get("name").toString(),
Toast.LENGTH_SHORT).show();
}
});
/**
* Item 长按方式d出菜单多选方式1
* Item 长按方式d出菜单多选方式2
* ItemOnLongClick1()与ItemOnLongClick2()不共存,按实际需要选择
*/
// ItemOnLongClick1();
ItemOnLongClick2();
}
// 填充ListVIEw资源
private voID FillListData() {
adapter = new SimpleAdapter(ListOnLongClickActivity.this,
getListData(),R.layout.ListvIEwrow,new String[] { "name",
"price" },new int[] { R.ID.tv_name,R.ID.tv_price });
mListVIEw.setAdapter(adapter);
}
private List<Map<String,Object>> getListData() {
Map<String,Object> _map = new HashMap<String,Object>();
_map.put("name","小米");
_map.put("price","2350元");
List.add(_map);
_map = new HashMap<String,"HTC G18");
_map.put("price","3340元");
List.add(_map);
_map = new HashMap<String,"iphone 5");
_map.put("price","5450元");
List.add(_map);
_map = new HashMap<String,"iPhone 4S");
_map.put("price","4650元");
List.add(_map);
_map = new HashMap<String,"MOTO ME525");
_map.put("price","1345元");
List.add(_map);
return List;
}
private voID ItemOnLongClick1() {
//注:setonCreateContextMenuListener是与下面onContextItemSelected配套使用的
mListVIEw
.setonCreateContextMenuListener(new OnCreateContextMenuListener() {
public voID onCreateContextMenu(ContextMenu menu,VIEw v,
ContextMenuInfo menuInfo) {
menu.add(0,"购买");
menu.add(0,1,"收藏");
menu.add(0,2,"对比");
}
});
}
// 长按菜单响应函数
public boolean onContextItemSelected(MenuItem item) {
AdapterVIEw.AdapterContextMenuInfo info = (AdapterVIEw.AdapterContextMenuInfo) item
.getMenuInfo();
MID = (int) info.ID;// 这里的info.ID对应的就是数据库中_ID的值
switch (item.getItemID()) {
case 0:
// 添加 *** 作
Toast.makeText(ListOnLongClickActivity.this,
"添加",
Toast.LENGTH_SHORT).show();
break;
case 1:
// 删除 *** 作
break;
case 2:
// 删除ALL *** 作
break;
default:
break;
}
return super.onContextItemSelected(item);
}
private voID ItemOnLongClick2() {
mListVIEw.setonItemLongClickListener(new OnItemLongClickListener() {
@OverrIDe
public boolean onItemLongClick(AdapterVIEw<?> arg0,
final int arg2,long arg3) {
List.remove(arg2);
new AlertDialog.Builder(ListOnLongClickActivity.this)
.setTitle("对Item进行 *** 作")
.setItems(R.array.arrcontent,
new DialogInterface.OnClickListener() {
public voID onClick(DialogInterface dialog,
int which) {
String[] PK = getResources()
.getStringArray(
R.array.arrcontent);
Toast.makeText(
ListOnLongClickActivity.this,
PK[which],Toast.LENGTH_LONG)
.show();
if (PK[which].equals("删除")) {
// 按照这种方式做删除 *** 作,这个if内的代码有BUG,实际代码中按需 *** 作
List.remove(arg2);
adapter = (SimpleAdapter) mListVIEw
.getAdapter();
if (!adapter.isEmpty()) {
adapter.notifyDataSetChanged(); // 实现数据的实时刷新
}
}
}
})
.setNegativebutton("取消",
int which) {
// Todo auto-generated method stub
}
}).show();
return true;
}
});
}
}
-----------
ListvIEwrow.xml
代码片段, <?xml version="1.0" enCoding="utf-8"?>
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"
androID:layout_wIDth="match_parent"
androID:layout_height="match_parent"
androID:background="@drawable/ListvIEwbg"
androID:orIEntation="vertical" >
<TextVIEw
androID:ID="@+ID/tv_name"
androID:layout_wIDth="fill_parent"
androID:layout_height="wrap_content"
androID:textcolor="@androID:color/black" />
<TextVIEw
androID:ID="@+ID/tv_price"
androID:layout_wIDth="fill_parent"
androID:layout_height="wrap_content"
androID:textcolor="@androID:color/black" />
</linearLayout>
以上是内存溢出为你收集整理的Android ListView长按d出菜单二种实现方式示例全部内容,希望文章能够帮你解决Android ListView长按d出菜单二种实现方式示例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)