返回顶部

收藏

自定义android BaseAdapter

更多

我们很多时候需要去自定义一下BaseAdapter,很常用的一个东西,希望对需要的人有帮助喜欢得人可以交流

package com.android.wei.zidingyib;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import java.util.*;

public class MyActivity extends Activity {

    private ListView listView;
    private ImageButton imageButton;
    private ListViewAdapter listViewAdapter;
    private List<Map<String,Object>> listItems;
    private Integer[] imgeIDs = {
        R.drawable.niao,R.drawable.niao,R.drawable.rui,R.drawable.rui,
        R.drawable.ning,R.drawable.ning
    };
    private String[] goodsNames={
        "土豆丝","土豆块","土豆泥","红烧土豆",
        "麻辣土豆","清蒸土豆"
    };
    private String[] goodsDetails={
        "土豆丝:便宜又好吃",    "土豆丝:便宜又好吃",    "土豆丝:便宜又好吃",    
        "土豆丝:便宜又好吃",    "土豆丝:便宜又好吃",    "土豆丝:便宜又好吃",
    };
    private int[] money={
        2,3,3,10,8,13   
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        listView=(ListView) this.findViewById(R.id.listview);
        imageButton = (ImageButton) this.findViewById(R.id.imagebutton);
        imageButton.setOnClickListener(new  ClickEvent());
        listItems = getListItems();
        listViewAdapter = new ListViewAdapter(this,listItems);
        listView.setAdapter(listViewAdapter);
    }
    private List<Map<String,Object>> getListItems(){
        List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>();
        for(int i=0;i<goodsNames.length;i++){
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("image", imgeIDs[i]);
            map.put("title", goodsNames[i]);
            map.put("info", money[i]+"元");
            map.put("detail", goodsDetails[i]);
            listItems.add(map);
        }
        return listItems;
    }
    class ClickEvent implements OnClickListener{
        public void onClick(View v){
            String goodsList="";
            int sun=0;
            for(int i=0;i<listItems.size();i++){
                goodsList += listViewAdapter.hasChecked(i)?goodsNames[i]+" ":"";
                sun +=listViewAdapter.hasChecked(i)?money[i]:0;

            }
            new AlertDialog.Builder(MyActivity.this)
            .setTitle("点菜清单")
            .setMessage("你好,你选择的菜:\n"+goodsList+"\n"+"总消费额为:"+sun+"元")
            .setPositiveButton("确定",null)
            .show();

        }
    }
}

自定义的类

package com.android.wei.zidingyib;

import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter{
    private Context context;

    private List<Map<String,Object>> listItems;

    private LayoutInflater listContainer;

    private boolean[] hasChecked;
    public final class ListItemView{
        public ImageView image;
        public TextView title;
        public TextView info;
        public CheckBox check;
        public Button detail;
    }

    public ListViewAdapter(Context context,List<Map<String,Object>> listItems){
        this.context = context;
        listContainer = LayoutInflater.from(context);
        this.listItems = listItems;
        hasChecked = new boolean[getCount()];
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listItems.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
    private void checkedChange(int checkedID){
        hasChecked[checkedID] = !hasChecked[checkedID];
    }
    public boolean hasChecked(int checkedID){
        return hasChecked[checkedID];
    }
    private void showDetailInfo(int clickID){
        new AlertDialog.Builder(context)
           .setTitle("本菜详情:"+listItems.get(clickID).get("title"))
           .setMessage(listItems.get(clickID).get("detail").toString())
           .setPositiveButton("确定",null)
           .show();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int selectID= position;
        ListItemView listItemView = null;
        if(convertView ==null){
            listItemView = new ListItemView();
            convertView = listContainer.inflate(R.layout.list_item, null);
            //获得控件对象
            listItemView.image =(ImageView) convertView.findViewById(R.id.imageitem);
            listItemView.title =(TextView) convertView.findViewById(R.id.textview);
            listItemView.info =(TextView) convertView.findViewById(R.id.textview1);
            listItemView.detail=(Button) convertView.findViewById(R.id.button);
            listItemView.check = (CheckBox) convertView.findViewById(R.id.checkItem);
           //设置空间集到convertView
            convertView.setTag(listItemView);
        }else{
            listItemView = (ListItemView) convertView.getTag();
        }
        //设置文字图片
        listItemView.image.setBackgroundResource((Integer)listItems.get(position).get("image"));
        listItemView.title.setText((String)listItems.get(position).get("title"));
        listItemView.info.setText((String) listItems.get(position).get("info"));
        listItemView.detail.setText("本菜介绍");
        listItemView.detail.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showDetailInfo(selectID);
            }
        });
        listItemView.check.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
                checkedChange(selectID);
            }
        });
        return convertView;
    }

}

标签:Android,java

收藏

0人收藏

支持

0

反对

0

发表评论