Android中ListView,Gallery,GridView 的适配器Adapter的使用

时间:2021-12-31 19:37:29
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 package
com.android.wei.zidingyi.adapter;
 import
android.app.Activity;
 import
android.os.Bundle;
import
android.view.Window;
import
android.view.WindowManager;
import
android.widget.Gallery;
import
android.widget.GridView;
 import
java.util.*;
  public
class
MyActivity extends
Activity {
               private
Gallery gallery;
        private
GridView gridView;
        private
ImageAdapter listViewAdapter;
        public
static
  ImageAdapterView imageAdapterView;
        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);            /**全屏显示**/            requestWindowFeature(Window.FEATURE_NO_TITLE);            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,              WindowManager.LayoutParams.FLAG_FULLSCREEN);                         setContentView(R.layout.main);            gridView=(GridView)this.findViewById(R.id.gridView1);            imageAdapterView = new
ImageAdapterView(
this);
            gridView.setAdapter(imageAdapterView);            gallery = (Gallery) this.findViewById(R.id.gallery1);            listItems = getListItems();            listViewAdapter = new
ImageAdapter(
this,listItems);
            gallery.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]+"元");                                 listItems.add(map);            }            return
listItems;
        } }

4. [代码]Gallery     

?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 package
com.android.wei.zidingyi.adapter;
 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.ImageView;
import
android.widget.TextView;
import
android.widget.Toast;
 import
java.util.*;
      public
class
ImageAdapter 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
Button add;
    }         public
ImageAdapter(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
;
    }                    @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.add =(Button) convertView.findViewById(R.id.button1);                        //设置空间集到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.add.setOnClickListener(new
View.OnClickListener() {
                         @Override            public
void
onClick(View v) {
                Map<String,Object> map = new
HashMap<String,Object>();
                map.put("image", listItems.get(selectId).get("image"));                             ImageAdapterView.menuList.add(map);               MyActivity.imageAdapterView.notifyDataSetChanged();            }        });                          return
convertView;
    } }

5. [代码]GridView     

?
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 package
com.android.wei.zidingyi.adapter;
 import
android.content.Context;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.BaseAdapter;
import
android.widget.ImageButton;
import
android.widget.ImageView;
import
android.widget.Toast;
 import
java.util.*;
 public
class
ImageAdapterView extends
BaseAdapter{
    private
Context context;
         private
LayoutInflater layoutInflater;
         public
static
List<Map<String,Object>> menuList = new
ArrayList<Map<String,Object>>();
         public
static
class ItemListView{
        public
ImageButton image;
    }    public
ImageAdapterView(Context context){
        this.context = context;        layoutInflater = LayoutInflater.from(context);             }    public
int
getCount() {
        // TODO Auto-generated method stub        return
menuList.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
;
    }     @Override    public
View getView(
int
position, View convertView, ViewGroup parent) {
        ItemListView itemListView = null;        if(convertView==null){            itemListView = new
ItemListView();
            convertView = layoutInflater.inflate(R.layout.item_list1, null);                         itemListView.image=(ImageButton) convertView.findViewById(R.id.menuImage);            convertView.setTag(itemListView);        }else
{
            itemListView=(ItemListView)convertView.getTag();        }       itemListView.image.setBackgroundResource((Integer)menuList.get(position).get("image"));       itemListView.image.setOnClickListener(new
View.OnClickListener() {
                 @Override        public
void
onClick(View v) {
            // TODO Auto-generated method stub            Toast.makeText(context,"触发成功", Toast.LENGTH_SHORT).show();        }    });        return
convertView;
    } }
举报