android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#99ccff"
android:orientation="vertical" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
android:id="@+id/bianhao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.09"
android:text="编号"
android:textColor="#6666ff"/>
android:id="@+id/_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.09"
android:text="_ID"
android:textColor="#6666ff"/>
区分列表的编号和id号,当删除列表时编号回自动排列不会断续,而id号会断续。
正文代码
listView = (ListView) findViewById(R.id.listView1);
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, 0, "删除");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterView.AdapterContextMenuInfo menuInfo=(AdapterContextMenuInfo)
item.getMenuInfo();//获取ID
TextView tname=(TextView) menuInfo.targetView.findViewById(R.id.shangpin);
TextView tid=(TextView) menuInfo.targetView.findViewById(R.id._ID);
_id = tid.getText().toString();
shangname=tname.getText().toString();
if (item.getItemId() == 1) {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setIcon(R.drawable.alipay_info);
dialog.setTitle("删除");
dialog.setMessage("是否删除商品"+shangname+"?");
dialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
System.out.println(_id + "..........id.....");
try {
boolean delete=stock_mingxi.this.stock_service.delete(_id);
if(delete(ture)){
Toast.makeText(stock_mingxi.this,"商品删除成功", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(stock_mingxi.this,"商品删除是失败", Toast.LENGTH_LONG).show();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
dialog.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog.create().show();
}
return super.onContextItemSelected(item);
} 新手第一次发多多谅解