ListView中嵌入Button,并响应Button点击事件
来源:互联网 发布:淘宝怎么卖出第一单 编辑:程序博客网 时间:2024/05/15 00:33
OrderListView.java
public class OrderListView extends Activity { private ArrayList<Map<String, Object>> arrayList = new ArrayList<Map<String, Object>>(); private Map<String, Object> map; MyAdapter adapter = null; public static List<Order> order = new ArrayList(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.order_list); initDate(); adapter = new MyAdapter(this); ListView list = (ListView) findViewById(R.id.order_list); list.setAdapter(adapter); } private void initDate() { arrayList.clear(); for (int i = 0; i < 10; i++) { map = new HashMap<String, Object>(); map.put("序号", "" + (i + 1)); map.put("菜号", "000"); map.put("菜名", "ZZZ"); map.put("份数", "1"); map.put("单价", "¥100"); map.put("总价", "¥100"); arrayList.add(map); } } public Handler handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 1: arrayList.remove(msg.arg1); adapter.notifyDataSetChanged(); break; } super.handleMessage(msg); } }; public class MyAdapter extends BaseAdapter { private LayoutInflater inflater; public MyAdapter(Context c) { this.inflater = LayoutInflater.from(c); } @Override public int getCount() { return arrayList.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } /** * 设置数据源与行View关联 设置行中个组件的事件响应 返回设置好的View */ @Override public View getView(int arg0, View arg1, ViewGroup arg2) { // 取得要显示的行View View myView = inflater.inflate(R.layout.order_list_item, null); LinearLayout layout = (LinearLayout) myView .findViewById(R.id.LinearLayoutOLI); if (arg0 % 2 == 0) { layout.setBackgroundDrawable(getResources().getDrawable( R.drawable.bg1)); } else { layout.setBackgroundDrawable(getResources().getDrawable( R.drawable.bg2)); } TextView textView1 = (TextView) myView.findViewById(R.id.textView1); TextView textView2 = (TextView) myView.findViewById(R.id.textView2); TextView textView3 = (TextView) myView.findViewById(R.id.textView3); TextView textView4 = (TextView) myView.findViewById(R.id.textView4); TextView textView5 = (TextView) myView.findViewById(R.id.textView5); Button button = (Button) myView.findViewById(R.id.button6); // 让行View的每个组件与数据源相关联 textView1.setText((String) arrayList.get(arg0).get("序号")); textView2.setText((String) arrayList.get(arg0).get("菜名")); textView3.setText((String) arrayList.get(arg0).get("份数")); textView4.setText((String) arrayList.get(arg0).get("单价")); textView5.setText((String) arrayList.get(arg0).get("总价")); button.setFocusable(false); final int arg = arg0; // 添加事件响应 button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub new AlertDialog.Builder(OrderListView.this).setTitle( "您确定删除吗?").setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Message message = new Message(); message.what = 1; message.arg1 = arg; handler.sendMessage(message); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }).show(); } }); return myView; } }}
order_list.xml
<?xml version="1.0" encoding="UTF-8"?><LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" androidrientation="vertical" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <TableRow> <ImageView android:layout_height="35px" android:layout_width="fill_parent" android:src="@drawable/cd" /> </TableRow> <TableRow> <ListView android:layout_width="fill_parent" android:layout_height="390px" android:id="@+id/order_list" /> </TableRow> </TableLayout></LinearLayout>
order_list_item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:id="@+id/LinearLayoutOLI" androidrientation="vertical" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:background="@drawable/bg1"> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <TableRow> <TextView android:id="@+id/textView1" android:layout_width="72px" android:layout_height="wrap_content" android:textSize="25sp" /> <TextView android:id="@+id/textView2" android:layout_width="270px" android:layout_height="wrap_content" android:textSize="25sp" /> <TextView android:id="@+id/textView3" android:layout_width="85px" android:layout_height="wrap_content" android:textSize="25sp" /> <TextView android:id="@+id/textView4" android:layout_width="105px" android:layout_height="wrap_content" android:textSize="25sp" /> <TextView android:id="@+id/textView5" android:layout_width="106px" android:layout_height="wrap_content" android:textSize="25sp" /> <Button android:id="@+id/button6" android:text="删除" android:layout_width="164px" android:focusable="false" android:layout_height="wrap_content" android:textSize="25sp" /> </TableRow> </TableLayout> </LinearLayout>
- ListView中嵌入Button,并响应Button点击事件
- ListView中嵌入Button,并响应Button点击事件
- ListView中嵌入布局中有Button,如何响应Button点击事件
- Android listView 中响应Button点击事件
- Android listView 中响应Button点击事件
- Android listView 中响应Button点击事件
- Android listView 中响应Button点击事件
- activity响应listview中button点击事件
- ListView中嵌入button,点击监听无响应的解决方案
- listview中button响应事件
- ListView中Button的点击事件
- listView中button点击事件冲突
- ListView中Button的点击事件冲突
- ListView中嵌入布局的Button或多个点击事件
- Android listView 每行多个Button 响应点击事件
- Button响应鼠标点击事件
- ListView中添加Button后,Button的点击事件和ListView的点击事件冲突
- ListView中添加Button后,Button的点击事件和ListView的点击事件冲突
- [每天一个Linux小技巧] 巧用strace查找程序打开的文件
- 蓝牙物理链路类型:SCO和ACL链路
- 妈妈说……
- CentOS 6.3安装配置LAMP服务器 (Apache+PHP5+MySQL)
- 输入矩形的长和宽,求出该矩形的周长和面积
- ListView中嵌入Button,并响应Button点击事件
- JAVASE接口
- Android json解析有关内容学习笔记
- 温暖与生活
- XMPP聊天程序iPhone版开发(一):初识XMPPFramework
- 快速排序
- QButtonGroup与QRadioButton的结合使用(Qt4中QButtonGroup类的使用)
- c c++ 中 结构体内成员 位域的 一点经验【2013.10.11小记】
- 软件度量都该度个啥?(4)——项目进度、成本度量