我们很多时候需要去自定义一下BaseAdapter,很常用的一个东西,希望对需要的人有帮助
喜欢得人可以交流,Android专业开发群1:150086842
Android专业开发群2:219277004
不要对贴图做评价,谢谢啦
[代码] 主类
01 |
package com.android.wei.zidingyib; |
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; |
12 |
public class MyActivity extends Activity { |
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 |
22 |
private String[] goodsNames={ |
23 |
"土豆丝" , "土豆块" , "土豆泥" , "红烧土豆" , |
26 |
private String[] goodsDetails={ |
27 |
"土豆丝:便宜又好吃" , "土豆丝:便宜又好吃" , "土豆丝:便宜又好吃" , |
28 |
"土豆丝:便宜又好吃" , "土豆丝:便宜又好吃" , "土豆丝:便宜又好吃" , |
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); |
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]); |
56 |
class ClickEvent implements OnClickListener{ |
57 |
public void onClick(View v){ |
60 |
for ( int i= 0 ;i<listItems.size();i++){ |
61 |
goodsList += listViewAdapter.hasChecked(i)?goodsNames[i]+ " " : "" ; |
62 |
sun +=listViewAdapter.hasChecked(i)?money[i]: 0 ; |
65 |
new AlertDialog.Builder(MyActivity. this ) |
67 |
.setMessage( "你好,你选择的菜:\n" +goodsList+ "\n" + "总消费额为:" +sun+ "元" ) |
68 |
.setPositiveButton( "确定" , null ) |
[代码] 自定义的类
001 |
package com.android.wei.zidingyib; |
003 |
import java.util.List; |
004 |
import java.util.Map; |
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; |
018 |
public class ListViewAdapter extends BaseAdapter{ |
019 |
private Context context; |
021 |
private List<Map<String,Object>> listItems; |
023 |
private LayoutInflater listContainer; |
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; |
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()]; |
041 |
public int getCount() { |
043 |
return listItems.size(); |
047 |
public Object getItem( int position) { |
053 |
public long getItemId( int position) { |
057 |
private void checkedChange( int checkedID){ |
058 |
hasChecked[checkedID] = !hasChecked[checkedID]; |
060 |
public boolean hasChecked( int checkedID){ |
061 |
return hasChecked[checkedID]; |
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 ) |
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 ); |
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); |
085 |
convertView.setTag(listItemView); |
087 |
listItemView = (ListItemView) convertView.getTag(); |
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() { |
097 |
public void onClick(View v) { |
099 |
showDetailInfo(selectID); |
102 |
listItemView.check.setOnCheckedChangeListener( new CheckBox.OnCheckedChangeListener(){ |
103 |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){ |
104 |
checkedChange(selectID); |