代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
listview和checkbox结合
作者:
/ 发布于
2014/1/6
/
324
通过重写listview的adapter,将listview和checkbox结合在一起,并且二者可以分别操作。
activity_menu.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="2.0" android:scrollbars="vertical" /> <TextView android:id="@+id/foodname" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:textSize="20dp" android:text="" /> <TextView android:id="@+id/price" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:textSize="20dp" android:text="" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:id="@+id/call" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/thphone1" android:layout_weight="1.0" android:textSize="20dp" android:text="电话订餐" /> <Button android:id="@+id/cancelButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0" android:textSize="20dp" android:text="取消" /> </LinearLayout> </LinearLayout> listview_style.xml~1KB <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="4dip" android:paddingLeft="12dip" android:paddingRight="12dip" > <CheckBox android:id="@+id/CheckBox01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:focusable="false" android:clickable="false" android:focusableInTouchMode="false" android:gravity="center_vertical" android:paddingTop="12dip" > </CheckBox> <TextView android:id="@+id/topTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView1" android:textSize="20dip" /> <TextView android:id="@+id/bottomTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/topTextView" android:text="TextView2" > </TextView> </RelativeLayout> MenuActivity.java~6KB package com.amaker.wlo; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager.NameNotFoundException; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; import com.amaker.util.HttpUtil; public class MenuActivity extends Activity { private ListView listview; private TextView totalPrice; private TextView totalFood; private Button callBtn; private Button cancleBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_menu); listview=(ListView)findViewById(android.R.id.list); totalPrice=(TextView)findViewById(R.id.price); totalFood=(TextView)findViewById(R.id.foodname); callBtn=(Button)findViewById(R.id.call); callBtn.setOnClickListener(callListener); cancleBtn=(Button)findViewById(R.id.cancelButton); cancleBtn.setOnClickListener(canclelistener); Context context=null; try { context= createPackageContext("com.amaker.wlo", Context.CONTEXT_IGNORE_SECURITY); } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } SharedPreferences preferences = context.getSharedPreferences("shop_id", MODE_WORLD_WRITEABLE); int shopid=preferences.getInt("shopid", 0); String urlStr = HttpUtil.BASE_URL + "servlet/FoodServlet?shopId="+shopid; try { // 实例化URL URL url = new URL(urlStr); // URLConnection 实例 URLConnection conn = url.openConnection(); // 获得输入流 InputStream in = conn.getInputStream(); // 获得DocumentBuilderFactory对象 DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); // 获得DocumentBuilder对象 DocumentBuilder builder = factory.newDocumentBuilder(); // 获得Document对象 Document doc = builder.parse(in); // 获得节点列表 NodeList nl = doc.getElementsByTagName("food"); // Spinner数据 ArrayList<HashMap<String, Object>> listitem = new ArrayList<HashMap<String, Object>>(); // 获得XML数据 for (int i = 0; i < nl.getLength(); i++) { // 商家編號 // 商家名字foodPrice String foodName = doc.getElementsByTagName("foodName") .item(i).getFirstChild().getNodeValue(); String foodPrice = doc.getElementsByTagName("foodPrice") .item(i).getFirstChild().getNodeValue(); HashMap<String, Object> map = new HashMap<String, Object>(); map.put("foodName", foodName); map.put("foodPrice", foodPrice+"元"); listitem.add(map); } MenuListAdapter listAdapter = new MenuListAdapter(this,listitem, R.layout.listview_style, new String[] { "foodName", "foodPrice" }, new int[] { R.id.topTextView, R.id.bottomTextView } ); listview.setAdapter(listAdapter); listview.setOnItemClickListener(getMenu); }catch (Exception e) { e.printStackTrace(); } } OnItemClickListener getMenu=new OnItemClickListener() { int sumprice=0; //定义一个存储所有价格的变量 String sumfood=""; //定义一个存储所有食物的变量 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { RelativeLayout lr = (RelativeLayout) view; TextView tvtop = (TextView) lr.getChildAt(1); String foodName=tvtop.getText().toString()+" "; TextView tv = (TextView) lr.getChildAt(2); String s = tv.getText().toString(); int foodprice=Integer.parseInt(s.substring(0, s.indexOf("元"))); // 取得ViewHolder对象,这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤 ViewHolder holder = (ViewHolder) view.getTag(); // 改变CheckBox的状态 holder.cb.toggle(); // 将CheckBox的选中状况记录下来 MenuListAdapter.getIsSelected().put(position, holder.cb.isChecked()); // 调整选定条目 if (holder.cb.isChecked() == true) { sumprice=sumprice+foodprice; sumfood=sumfood+foodName; } else { sumprice=sumprice-foodprice; sumfood=sumfood.replace(foodName, ""); } // 用TextView显示 totalPrice.setText("你預計消費: "+sumprice); totalFood.setText("你已經點了: "+sumfood); } }; OnClickListener callListener=new OnClickListener() { @Override public void onClick(View v) { Context context=null; try { context= createPackageContext("com.amaker.wlo", Context.CONTEXT_IGNORE_SECURITY); } catch (NameNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } SharedPreferences pref = context.getSharedPreferences("phoneinfo", MODE_WORLD_WRITEABLE); String PhoneNum=pref.getString("PhoneNum", ""); Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+PhoneNum)); startActivity(intent); } }; OnClickListener canclelistener=new OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MenuActivity.this,MainMenuActivity.class); startActivity(intent); } }; } MenuListAdapter.java~2KB package com.amaker.wlo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.SimpleAdapter; import android.widget.TextView; public class MenuListAdapter extends SimpleAdapter { // 用来控制CheckBox的选中状况 private static HashMap<Integer, Boolean> isSelected; // 用来导入布局 private LayoutInflater inflater = null; private List<? extends Map<String, ?>> Mdata; public MenuListAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); this.Mdata=data; isSelected=new HashMap<Integer, Boolean>(); inflater = LayoutInflater.from(context); initDate(); } private void initDate(){ for(int i=0; i<Mdata.size();i++) { getIsSelected().put(i,false); } } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { // 获得ViewHolder对象 holder = new ViewHolder(); // 导入布局并赋值给convertview convertView = inflater.inflate(R.layout.listview_style, null); holder.tv1 = (TextView) convertView.findViewById(R.id.topTextView); holder.tv2 = (TextView) convertView.findViewById(R.id.bottomTextView); holder.cb = (CheckBox) convertView.findViewById(R.id.CheckBox01); // 为view设置标签 convertView.setTag(holder); } else { // 取出holder holder = (ViewHolder) convertView.getTag(); } // 设置list中TextView的显示 //holder.tv1.setText(); holder.tv1.setText(((Map)getItem(position)).get("foodName").toString()); holder.tv2.setText(((Map)getItem(position)).get("foodPrice").toString()); // 根据isSelected来设置checkbox的选中状况 holder.cb.setChecked(getIsSelected().get(position)); return convertView; } public static HashMap<Integer, Boolean> getIsSelected() { return isSelected; } public static void setIsSelected(HashMap<Integer, Boolean> isSelected) { MenuListAdapter.isSelected = isSelected; } } class ViewHolder { TextView tv1; TextView tv2; CheckBox cb; }
试试其它关键字
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3