Android-Adapter

Adapter是UI与数据的连接者,其关系如下图所示:


常用的Adapter有:

1.BaseAdapter  抽象类  它需要实现较多的方法 灵活
2.ArrayAdapter 支持泛型操作 最
简单 只能展示一行字
3.SimpleAdapter 最好的扩充性 可以自定义出各种效果
4.
SimpleCursorAdapter 适用于简单的纯文字型ListView 它需要Cursor的字段和UI的id对应起来


下面逐一介绍这几个Adapter
-------------------------------------------------

ArrayAdapter

用法一:

package com.example.testadapter;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;

public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);  这句必须注释掉,否则会出错
        
        String strs[]={"item1","imte2","item3"};
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,strs);
        setListAdapter(adapter);
        
    }
}


用法二:

package com.example.testadapter;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
	private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //创建一个Adapter
        String strs[]={"item1","imte2","item4"};
        ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_expandable_list_item_1,strs);
        //创建一个ListView
        listView=new ListView(this);
        listView.setAdapter(adapter);
        //讲listView设置为显示View
        this.setContentView(listView);
    }
}

上面的代码都是使用
Adapter(Context context, int resourcefulness, List<T> objects)
装载数据,
Context context 设备上下文,一般是Activity
int resourcefulness 单行的布局文件
List<T> objects
数据源
注:
android.R.layout.simple_list_item_1 是系统定义好的一个布局文件,只显示一行文字(每一个条项的布局)

----------------------------------------------------------
SimpleAdapter
SimpleAdapter的可拓展型很好,可以放上各种控件,如Button,ImageView等


方法一:
package com.example.testadapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends ListActivity {
	private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        String[] headTitle={"title","image"};//对应数据源中的编号
        int[] headId={R.id.text,R.id.image}; //对应simple.xml的id
        
        SimpleAdapter adapter=new SimpleAdapter(this,getData(),R.layout.simple,headTitle,headId);
        this.setListAdapter(adapter);
    }
    
    //产生数据源
    private List<Map<String,Object>> getData(){
    	
    	List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();
    	Map<String,Object> map=new HashMap<String,Object>();
    	
    	map.put("title", "iphone");
    	map.put("image", R.drawable.ic_launcher);
    	data.add(map);
    	
    	map=new HashMap<String,Object>();
    	map.put("title", "android");
    	map.put("image", R.drawable.ic_launcher);
    	data.add(map);
    	
    	map=new HashMap<String,Object>();
    	map.put("title", "win phone");
    	map.put("image", R.drawable.ic_launcher);
    	data.add(map);

    	return data;
    }
    
    
}

simle.xml
<?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="vertical" >
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

方法二:
package com.example.testadapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends ListActivity {
	private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        String[] headTitle={"title","image","info"};//对应数据源中的编号
        int[] headId={R.id.text,R.id.image,R.id.info}; //对应simple.xml的id
        
        SimpleAdapter adapter=new SimpleAdapter(this,getData(),R.layout.simple,headTitle,headId);
        this.setListAdapter(adapter);
    }
    
    //产生数据源
    private List<Map<String,Object>> getData(){
    	
    	List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();
    	Map<String,Object> map=new HashMap<String,Object>();
    	
    	map.put("title", "iphone");
    	map.put("image", R.drawable.ic_launcher);
    	map.put("info", "便宜!");
    	data.add(map);
    	
    	map=new HashMap<String,Object>();
    	map.put("title", "android");
    	map.put("image", R.drawable.ic_launcher);
    	map.put("info", "实惠!");
    	data.add(map);
    	
    	map=new HashMap<String,Object>();
    	map.put("title", "win phone");
    	map.put("image", R.drawable.ic_launcher);
    	map.put("info", "好看!");
    	data.add(map);

    	return data;
    }
}

simple.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5px"/>
    
    <LinearLayout 
		android:orientation="vertical"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" >
		
       <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22px"/>
       
       <TextView
           android:id="@+id/info"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textSize="14px"/>
    
    </LinearLayout>
</LinearLayout>

 


---------------------
SimpleCursorAdapter 是直接与数据库互联的,此处不做介绍,可参考文献1

-----------------------
BaseAdapter
BaseAdapter是一个抽象类,比较灵活,继承它需实现4个方法
public int getCount(); 返回的是数据个数,当ListView开始绘制时,会通过此方法取得数据的大小,根据此大小逐个调用getView
public Object getItem(int arg0);
public long getItemId(int arg0);
public View getView(int position, View convertView, ViewGroup parent);

simple.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5px"/>
    
    <LinearLayout 
		android:orientation="vertical"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" >
		
       <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22px"/>
       
       <TextView
           android:id="@+id/info"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textSize="14px"/>
    	<Button 
    	    android:id="@+id/button"
    	    android:layout_width="wrap_content"
    	    android:layout_height="wrap_content"
    	    android:text="click me"/>
    </LinearLayout>
</LinearLayout>

ViewHodler.java
package com.example.testadapter;

import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public final class ViewHolder {
	public ImageView image;
	public TextView title;
	public TextView info;
	public Button button;
}

MyAdapter.java
package com.example.testadapter;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
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 MyAdapter extends BaseAdapter{
	private LayoutInflater mInflater;
	private List<Map<String, Object>> data;
	private Activity activity;
	
	
	public MyAdapter(Context context,List<Map<String, Object>> data,Activity activity) {
		this.mInflater = LayoutInflater.from(context);
		this.data=data;
		this.activity=activity;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		ViewHolder holder=null;
		if(convertView==null){
			holder=new ViewHolder();
			convertView =mInflater.inflate(R.layout.simple, null);
			holder.button=(Button)convertView.findViewById(R.id.button);
			holder.image=(ImageView)convertView.findViewById(R.id.image);
			holder.info=(TextView)convertView.findViewById(R.id.info);
			holder.title=(TextView)convertView.findViewById(R.id.text);
			convertView.setTag(holder);
			
		}else{
			holder=(ViewHolder)convertView.getTag();		
		}
		
		holder.image.setBackgroundResource((Integer)data.get(position).get("image"));
		holder.title.setText((String)data.get(position).get("title"));
		holder.info.setText((String)data.get(position).get("info"));
		holder.button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showInfo(activity);
			}
		});

		return convertView;
	}
	
	public void showInfo(Activity activity){
		new AlertDialog.Builder(activity).setMessage("hello").create().show();
	}
	
	
}

MainActivity.java
package com.example.testadapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyAdapter adapter=new MyAdapter(this, getData(), this);
        this.setListAdapter(adapter);
    }
    
    //产生数据源
    private List<Map<String,Object>> getData(){
    	
    	List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();
    	Map<String,Object> map=new HashMap<String,Object>();
    	
    	map.put("title", "iphone");
    	map.put("image", R.drawable.ic_launcher);
    	map.put("info", "便宜!");
    	data.add(map);
    	
    	map=new HashMap<String,Object>();
    	map.put("title", "android");
    	map.put("image", R.drawable.ic_launcher);
    	map.put("info", "实惠!");
    	data.add(map);
    	
    	map=new HashMap<String,Object>();
    	map.put("title", "win phone");
    	map.put("image", R.drawable.ic_launcher);
    	map.put("info", "好看!");
    	data.add(map);

    	return data;
    }
}

getView()方法中有三个参数
int position 表示将显示的是第几行
covertView是从布局文件中inflate来的布局

注:
在实际的运行过程中会发现listView的每一行没有焦点了,这是因为Button抢夺了listView的焦点,只要布局文件中将Button设置为没有焦点就OK了。



参考文献:
xxfeng: Android之Adapter用法总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值