• 自定义一下BaseAdapter



    我们很多时候需要去自定义一下BaseAdapter,很常用的一个东西,希望对需要的人有帮助 
    喜欢得人可以交流,Android专业开发群1:150086842
    Android专业开发群2:219277004
     不要对贴图做评价,谢谢啦
     
    标签: Android SDK
     

    代码片段(4)

    [图片] 菜2.jpg

    [图片] 菜.jpg

    [代码] 主类

    01 package com.android.wei.zidingyib;
    02  
    03 import android.app.Activity;
    04 import android.app.AlertDialog;
    05 import android.os.Bundle;
    06 import android.view.View;
    07 import android.view.View.OnClickListener;
    08 import android.widget.ImageButton;
    09 import android.widget.ListView;
    10 import java.util.*;
    11  
    12 public class MyActivity extends Activity {
    13   
    14     private ListView listView;
    15     private ImageButton imageButton;
    16     private ListViewAdapter listViewAdapter;
    17     private List<Map<String,Object>> listItems;
    18     private Integer[] imgeIDs = {
    19         R.drawable.niao,R.drawable.niao,R.drawable.rui,R.drawable.rui,
    20         R.drawable.ning,R.drawable.ning
    21     };
    22     private String[] goodsNames={
    23         "土豆丝","土豆块","土豆泥","红烧土豆",
    24         "麻辣土豆","清蒸土豆"
    25     };
    26     private String[] goodsDetails={
    27         "土豆丝:便宜又好吃",    "土豆丝:便宜又好吃",    "土豆丝:便宜又好吃",   
    28         "土豆丝:便宜又好吃",    "土豆丝:便宜又好吃",    "土豆丝:便宜又好吃",
    29     };
    30     private int[] money={
    31         2,3,3,10,8,13  
    32     };
    33     
    34     public void onCreate(Bundle savedInstanceState) {
    35         super.onCreate(savedInstanceState);
    36         setContentView(R.layout.main);
    37         listView=(ListView) this.findViewById(R.id.listview);
    38         imageButton = (ImageButton) this.findViewById(R.id.imagebutton);
    39         imageButton.setOnClickListener(new  ClickEvent());
    40         listItems = getListItems();
    41         listViewAdapter = new ListViewAdapter(this,listItems);
    42         listView.setAdapter(listViewAdapter);
    43     }
    44     private List<Map<String,Object>> getListItems(){
    45         List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>();
    46         for(int i=0;i<goodsNames.length;i++){
    47             Map<String,Object> map = new HashMap<String,Object>();
    48             map.put("image", imgeIDs[i]);
    49             map.put("title", goodsNames[i]);
    50             map.put("info", money[i]+"元");
    51             map.put("detail", goodsDetails[i]);
    52             listItems.add(map);
    53         }
    54         return listItems;
    55     }
    56     class ClickEvent implements OnClickListener{
    57         public void onClick(View v){
    58             String goodsList="";
    59             int sun=0;
    60             for(int i=0;i<listItems.size();i++){
    61                 goodsList += listViewAdapter.hasChecked(i)?goodsNames[i]+" ":"";
    62                 sun +=listViewAdapter.hasChecked(i)?money[i]:0;
    63                  
    64             }
    65             new AlertDialog.Builder(MyActivity.this)
    66             .setTitle("点菜清单")
    67             .setMessage("你好,你选择的菜:\n"+goodsList+"\n"+"总消费额为:"+sun+"元")
    68             .setPositiveButton("确定",null)
    69             .show();
    70              
    71         }
    72     }
    73 }

    [代码] 自定义的类

    001 package com.android.wei.zidingyib;
    002  
    003 import java.util.List;
    004 import java.util.Map;
    005  
    006 import android.app.AlertDialog;
    007 import android.content.Context;
    008 import android.view.LayoutInflater;
    009 import android.view.View;
    010 import android.view.ViewGroup;
    011 import android.widget.BaseAdapter;
    012 import android.widget.Button;
    013 import android.widget.CheckBox;
    014 import android.widget.CompoundButton;
    015 import android.widget.ImageView;
    016 import android.widget.TextView;
    017  
    018 public class ListViewAdapter extends BaseAdapter{
    019     private Context context;
    020      
    021     private List<Map<String,Object>> listItems;
    022      
    023     private LayoutInflater listContainer;
    024      
    025     private boolean[] hasChecked;
    026     public final class ListItemView{
    027         public ImageView image;
    028         public TextView title;
    029         public TextView info;
    030         public CheckBox check;
    031         public Button detail;
    032     }
    033      
    034     public ListViewAdapter(Context context,List<Map<String,Object>> listItems){
    035         this.context = context;
    036         listContainer = LayoutInflater.from(context);
    037         this.listItems = listItems;
    038         hasChecked = new boolean[getCount()];
    039     }
    040     @Override
    041     public int getCount() {
    042         // TODO Auto-generated method stub
    043         return listItems.size();
    044     }
    045  
    046     @Override
    047     public Object getItem(int position) {
    048         // TODO Auto-generated method stub
    049         return null;
    050     }
    051  
    052     @Override
    053     public long getItemId(int position) {
    054         // TODO Auto-generated method stub
    055         return 0;
    056     }
    057     private void checkedChange(int checkedID){
    058         hasChecked[checkedID] = !hasChecked[checkedID];
    059     }
    060     public boolean hasChecked(int checkedID){
    061         return hasChecked[checkedID];
    062     }
    063     private void showDetailInfo(int clickID){
    064         new AlertDialog.Builder(context)
    065            .setTitle("本菜详情:"+listItems.get(clickID).get("title"))
    066            .setMessage(listItems.get(clickID).get("detail").toString())
    067            .setPositiveButton("确定",null)
    068            .show();
    069     }
    070  
    071     @Override
    072     public View getView(int position, View convertView, ViewGroup parent) {
    073         final int selectID= position;
    074         ListItemView listItemView = null;
    075         if(convertView ==null){
    076             listItemView = new ListItemView();
    077             convertView = listContainer.inflate(R.layout.list_item, null);
    078             //获得控件对象
    079             listItemView.image =(ImageView) convertView.findViewById(R.id.imageitem);
    080             listItemView.title =(TextView) convertView.findViewById(R.id.textview);
    081             listItemView.info =(TextView) convertView.findViewById(R.id.textview1);
    082             listItemView.detail=(Button) convertView.findViewById(R.id.button);
    083             listItemView.check = (CheckBox) convertView.findViewById(R.id.checkItem);
    084            //设置空间集到convertView
    085             convertView.setTag(listItemView);
    086         }else{
    087             listItemView = (ListItemView) convertView.getTag();
    088         }
    089         //设置文字图片
    090         listItemView.image.setBackgroundResource((Integer)listItems.get(position).get("image"));
    091         listItemView.title.setText((String)listItems.get(position).get("title"));
    092         listItemView.info.setText((String) listItems.get(position).get("info"));
    093         listItemView.detail.setText("本菜介绍");
    094         listItemView.detail.setOnClickListener(new View.OnClickListener() {
    095              
    096             @Override
    097             public void onClick(View v) {
    098                 // TODO Auto-generated method stub
    099                 showDetailInfo(selectID);
    100             }
    101         });
    102         listItemView.check.setOnCheckedChangeListener(newCheckBox.OnCheckedChangeListener(){
    103             public void onCheckedChanged(CompoundButton buttonView,booleanisChecked){
    104                 checkedChange(selectID);
    105             }
    106         });
    107         return convertView;
    108     }
    109  
    110 }
  • 相关阅读:
    完美图解教程 Linux环境VNC服务安装、配置与使用
    c语言中<stdbool.h>的使用
    UNIX文件结构(转自UNIX/AIX操作系统基础教程)
    umask
    在AIX环境为Oracle表空间增加裸设备(逻辑卷)
    面的面积大小合线的长度的判断。
    不用新浪博客了。以后的日常感悟也在这里写了, 新浪太烂了。 (感叹也没用,什么都写不出来。 )
    轴心工具的做法。
    选择因素随机的判断。
    上面发的那个完整版变量还有问题,难道我不是用英文输入法, 电脑不说谎 ,看来我是大意了。下面是最终版。
  • 原文地址:https://www.cnblogs.com/appzhang/p/2672034.html
Copyright © 2020-2023  润新知