BaseAdapter的简化实例
来源:互联网 发布:好用打印软件注册码 编辑:程序博客网 时间:2024/05/16 19:35
直接上代码吧
public class MainActivity extends ActionBarActivity {private ListView lv;private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);lv = (ListView) findViewById(R.id.lv_list);initData();lv.setAdapter(new MyAdapter());}private void initData() {Map<String, Object> map = new HashMap<String, Object>();map.put("img", R.drawable.su01);map.put("title", "0");map.put("button", "0");list.add(map);map = new HashMap<String, Object>();map.put("img", R.drawable.su02);map.put("title", "1");map.put("button", "1");list.add(map);map = new HashMap<String, Object>();map.put("img", R.drawable.su03);map.put("title", "2");map.put("button", "2");list.add(map);}class MyAdapter extends BaseAdapter {@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int position) {return null;}@Overridepublic long getItemId(int position) {return 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {convertView = View.inflate(MainActivity.this,R.layout.list_item_layout, null);ImageView mImageView = (ImageView) convertView.findViewById(R.id.imageview);TextView mTextView = (TextView) convertView.findViewById(R.id.textview);Button mButton = (Button) convertView.findViewById(R.id.button);mImageView.setImageResource((Integer) list.get(position).get("img"));mTextView.setText(list.get(position).get("title").toString());mButton.setText(list.get(position).get("button").toString());return convertView;}}}再加上两个布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/lv_list" android:layout_width="match_parent" android:layout_height="wrap_content" android:listSelector="@android:color/transparent" android:divider="@android:color/transparent" android:layout_marginTop="40dp" /></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageView android:id="@+id/imageview" android:layout_width="50dip" android:layout_height="50dip" /> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="match_parent" android:paddingTop="8dip" android:textSize="20sp" /><Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>效果如下
0 0
- BaseAdapter的简化实例
- BaseAdapter的简化
- BaseAdapter 的使用实例
- 自定义封装了ViewHolder的BaseAdapter,简化了Adapter的编写
- 使用typedef简化声明的实例分析
- ListView结合BaseAdapter实例
- BaseAdapter 创建ListView实例
- BaseAdapter适配器实例介绍
- 对BaseAdapter中ViewHolder编写简化
- 对BaseAdapter中ViewHolder编写简化
- [Android]对BaseAdapter中ViewHolder编写简化
- Python简化类例三:实现类实例的事件定义
- Android实例demo10之listview的使用(加载xml、textview、BaseAdapter的使用)
- BaseAdapter的应用
- 万能的适配器 BaseAdapter
- BaseAdapter的使用
- BaseAdapter的ArrayIndexOutOfBoundsException
- 实现自定义的BaseAdapter
- Ubuntu下安装Apache+php+mysql
- Guava Cache缓存容量管理
- Maven最佳实践:版本管理
- 3086 区域发展
- 线程池控制并发数量
- BaseAdapter的简化实例
- 控制材质偏移和缩放
- java面向对象高级分层实例_接口类
- 人脸检测、对齐、识别相关资料收集
- SQL语句左关联和where的区别
- 消息服务设计
- PHP 底层的运行机制与原理解析
- ROS openni_tracker:骨架追踪
- OkHttp简单封装,支持进度UI线程回调(支持缓存)