3:BaseAdapter
这种Adapter写起来最复杂,但是可以实现的功能却是最多的,比如说在某一个listview中加入button的监听事件等等,这也是必备的掌握技能

package com.example.arrayada;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.widget.ListView;import com.example.adapter.MyBaseAdapter;public class BaseActivity extends Activity{private ListView listview = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.base_main);listview = (ListView) findViewById(R.id.balist);listview.setAdapter(new MyBaseAdapter(this, getData()));}private List<HashMap<String, Object>> getData(){List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();HashMap<String, Object> map = new HashMap<String,Object>();map.put("img", R.drawable.pkq);map.put("te1", "皮卡丘");map.put("te2", "能力五星");map.put("te3", "查询详细");list.add(map);map = new HashMap<String,Object>();map.put("img", R.drawable.jng);map.put("te1", "皮卡丘");map.put("te2", "皮卡丘");map.put("te3", "皮卡丘");list.add(map);map = new HashMap<String,Object>();map.put("img", R.drawable.xhl);map.put("te1", "皮卡丘");map.put("te2", "皮卡丘");map.put("te3", "皮卡丘");list.add(map);map = new HashMap<String,Object>();map.put("img", R.drawable.zz);map.put("te1", "皮卡丘");map.put("te2", "皮卡丘");map.put("te3", "皮卡丘");list.add(map);return list;}}


base_main.xml
<LinearLayout 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"    android:orientation="vertical"    tools:context=".MainActivity" >    <ListView        android:id="@+id/balist"        android:layout_width="match_parent"        android:layout_height="wrap_content"         android:layout_weight="1">    </ListView>        <Button        android:id="@+id/but3"         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="baseadapter 的listview" /></LinearLayout>


base_list.xml
<LinearLayout 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"    android:orientation="horizontal"    tools:context=".MainActivity" >    <ImageView        android:id="@+id/img2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:src="@drawable/ic_launcher" />    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="2"        android:orientation="vertical" >        <TextView            android:id="@+id/text1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="23dp"            android:text="TextView" />        <TextView            android:id="@+id/text2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="TextView" />    </LinearLayout>        <Button        android:id="@+id/base_but"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="Button" />    </LinearLayout>


package com.example.adapter;import java.util.HashMap;import java.util.List;import com.example.arrayada.R;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;public class MyBaseAdapter extends BaseAdapter{private Context c =null;private List<HashMap<String, Object>> list;private LayoutInflater myInflater = null;public MyBaseAdapter(Context context,List<HashMap<String, Object>> baselist) {c = context;list = baselist;System.out.println(list.size());}@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int arg0) {return arg0;}@Overridepublic long getItemId(int arg0) {return arg0;}@Overridepublic View getView(int arg0, View arg1, ViewGroup arg2) {BaseHolder baseHolder = new BaseHolder();if(arg1 == null){myInflater = LayoutInflater.from(c);arg1 = myInflater.inflate(R.layout.base_list, null);baseHolder.img = (ImageView) arg1.findViewById(R.id.img2);baseHolder.textview1 = (TextView) arg1.findViewById(R.id.text1);baseHolder.textview2 = (TextView) arg1.findViewById(R.id.text2);baseHolder.button1 = (Button) arg1.findViewById(R.id.base_but);arg1.setTag(baseHolder);}else{baseHolder = (BaseHolder) arg1.getTag();}baseHolder.img.setImageResource((Integer) list.get(arg0).get("img"));baseHolder.textview1.setText((String) list.get(arg0).get("te1"));baseHolder.textview2.setText((String) list.get(arg0).get("te2"));baseHolder.button1.setText((String) list.get(arg0).get("te3"));return arg1;}private class BaseHolder{ImageView img;TextView textview1;TextView textview2;Button button1;}}

更多相关文章

  1. 移动无线测试技能树
  2. Android万能适配器Adapter
  3. Android架构纵横谈之――软件自愈能力 (2)
  4. 还不会View绘制?本文带你get新技能!!
  5. 这是什么水平
  6. Android技能树 - Rxjava源码(1) 之 初步结构
  7. android高级开发工程师基本素质
  8. "Android中的动画"-Android面试必问"精华技能点"汇总
  9. 北京友好互动科技发展有限公司招聘ios、c++、php等职位

随机推荐

  1. android Intent机制详解
  2. Android(安卓)- 文件读写操作 总结
  3. android之用户定位(一)
  4. Android(安卓)的实现TextView中文字链接
  5. Android中的五大存储
  6. 引用 Android上dip、dp、px、sp等单位说
  7. Android(安卓)中屏幕点击事件的实现
  8. Android(安卓)系统服务管家servicemanage
  9. Android(安卓)QQ、微信聊天消息界面设计
  10. Android浮动窗口实现原理及代码实例