• android的listview+BaseAdapter的例子


    准备一个listview的xml,绑定到activity里面的listView的变量

    准备一个继承自BaseAdapter的的MyAdapter,构造函数里面传入一个datasource的array,一个context,一般为绑定listview的activity(用来把item的xml实例化为layout)

    准备一个数据模型,可以用hashmap,也可以用list

    MainActivity.java

    package com.example.testapp;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.GridView;
    import android.widget.ListView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        private List<ObjectEntity> dataSource;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            
    //        initListView();
            initGridView();
        }
        
        private void initListView() {
            
            setContentView(R.layout.activity_list_main);
            ListView listView = (ListView)findViewById(R.id.listView1);
            
            dataSource = new ArrayList<ObjectEntity>();
            
            for (int i = 0; i < 10; i++) {
                ObjectEntity object = new ObjectEntity();
                
                object.imgUrl = "http://a.hiphotos.baidu.com/album/w%3D2048/sign=3da6584ff2deb48ffb69a6dec4273b29/960a304e251f95ca11945098c8177f3e670952bf.jpg";
                object.desc = "这是list描述信息";
                
                dataSource.add(object);
            }
                    
            MyAdapter myAdapter = new MyAdapter(dataSource,MainActivity.this);
            listView.setAdapter(myAdapter);
            
            listView.setOnItemClickListener(new OnItemClickListener() {
            
                public void onItemClick(AdapterView<?> parent, View view, int position, long id)
                {
                    ObjectEntity data = dataSource.get(position);
                    
                    Toast.makeText(getApplicationContext(),  
                            data.desc, 
                            Toast.LENGTH_SHORT).show(); 
                }
            });
        }
        
        private void initGridView() {
    
            setContentView(R.layout.ctivity_grid_main);
            GridView gridView = (GridView)findViewById(R.id.gridView1);
            
            dataSource = new ArrayList<ObjectEntity>();
            
            for (int i = 0; i < 10; i++) {
                ObjectEntity object = new ObjectEntity();
                
                object.imgUrl = "http://a.hiphotos.baidu.com/album/w%3D2048/sign=3da6584ff2deb48ffb69a6dec4273b29/960a304e251f95ca11945098c8177f3e670952bf.jpg";
                object.desc = "这是grid描述信息";
                
                dataSource.add(object);
            }
        
            MyAdapter myAdapter = new MyAdapter(dataSource,MainActivity.this);
            gridView.setAdapter(myAdapter);
            
            gridView.setOnItemClickListener(new OnItemClickListener() {
                
                public void onItemClick(AdapterView<?> parent, View view, int position, long id)
                {
                    ObjectEntity data = dataSource.get(position);
                    
                    Toast.makeText(getApplicationContext(),  
                            data.desc, 
                            Toast.LENGTH_SHORT).show(); 
                }
            });
        }
    }

    MyAdapter.java

    package com.example.testapp;
    import java.util.List;
    
    import net.tsz.afinal.FinalBitmap;
    import android.app.Activity;
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    
    public class MyAdapter extends BaseAdapter {
    
        private FinalBitmap fb;
        
        private List<ObjectEntity> dataSourceArray;
        private Context activityContext;
        
        public MyAdapter(List<ObjectEntity> dataSource,Context context) {
            // TODO Auto-generated constructor stub
            
            this.dataSourceArray = dataSource;
            this.activityContext = context;
            
            fb = FinalBitmap.create(context);//初始化FinalBitmap模块
        }
    
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return this.dataSourceArray.size();
        }
    
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            
            ObjectEntity data = dataSourceArray.get(position);
            
            if ( null == convertView )
            {
                LayoutInflater inflater = ((Activity)activityContext).getLayoutInflater();
                convertView = inflater.inflate(R.layout.item, null);
                
                ImageView imgView = (ImageView)convertView.findViewById(R.id.listitem_pic);
                
                //AFinal很好很强大 fb.configLoadingImage(R.drawable.ic_launcher); fb.display(imgView,data.imgUrl); } TextView info
    = (TextView)convertView.findViewById(R.id.listitem_title); info.setText(data.desc); return convertView; } }

    ObjectEntity.java

    package com.example.testapp;
    
    public class ObjectEntity {
    
        public String imgUrl;
        public String desc;
    }
  • 相关阅读:
    SDOI2008 沙拉公主的困惑
    HNOI2004 L语言
    SDOI2008 sue的小球
    HNOI2011 XOR和路径
    BJWC2011 元素
    CQOI2013 新nim游戏
    HNOI2007 紧急疏散
    sublime text 3将px换算为rem的插件的安装及使用
    移动端知识
    登录拦截逻辑
  • 原文地址:https://www.cnblogs.com/ziyouchutuwenwu/p/3094633.html
Copyright © 2020-2023  润新知