当前位置:编程学习 > JAVA >>

Adapter详解


动态添加数据适配器adapter

Adapter是适配器,许多UI组件需要搭配Adapter来显示界面,例如Spinner与ListView,另外当我们需要动态的添加数据给UI组件时就需要Adapter了。

我们来介绍几个常见的Adapter:

 


ArrayAdapter<T>(comman)
SimpleAdapter extends Baseadapter(comman)
******************************************************************************

这两个适配器非常常见,也非常有用,通常传输的数据可以是ArrayList<>封装的数据。

示例如下:ArrayAdapter


[java]
private String[] string={"china","america","易做图"}; 
List list=new ArrayList<String>(); 
for(int i=0;i<string.length;i++) 

list.add(string[i]); 

ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.易做图_spinner_item,list); 

private String[] string={"china","america","易做图"};
List list=new ArrayList<String>();
for(int i=0;i<string.length;i++)
{
list.add(string[i]);
}
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.易做图_spinner_item,list);首先我们定义封装数据,然后再定义Adapter,最后调用setAdapter()方法。

ArrayAdapter有三个参数,第一个参数为Activity.this,第三个参数为动态添加给adapter的数据
对于第二个数据:
R.layout.易做图_spinner_item:意思是将数据传给一个简单地TextView组件


示例:SimpleAdapter


[java]
public class SimpleAdapter extends Activity{ 
private String[] names =new String[]{"nick","lebron","kobe","kevin"}; 
private int[] imgsIds[] =new Int[] {R.drawable.1,R.drawable.2,R.drawable.3,R.drawable.4}; 
@Override 
public void onCreate(Bundle savedInsatnceState) 

super.onCreate(savedInsatnceState); 
setTitle("易做图adapter"); 
setContentView(R.layout.main); 
List<Map><String,Object> list=new ArrayList<Map><String,Object>();//创建动态数组ArrayList  
for(int i=0;i<names.length;i++){ 
Map<String,Object> map=new HashMap=<String,Object>; 
map.put("name",names[i]); 
map.put("headerImg",imgsIds[i]); 
list.add(map); 

SimpleAdapter adapter=new SimpleAdaper(this,list,R.layout.main,new String[]{"name","headerImg"},new int[]{R.id.name,R.id.headerImg}); 
ListView list=(ListView)findViewById(R.id.mylist); 
list.setAdaper(adapter); 

public class SimpleAdapter extends Activity{
private String[] names =new String[]{"nick","lebron","kobe","kevin"};
private int[] imgsIds[] =new Int[] {R.drawable.1,R.drawable.2,R.drawable.3,R.drawable.4};
@Override
public void onCreate(Bundle savedInsatnceState)
{
super.onCreate(savedInsatnceState);
setTitle("易做图adapter");
setContentView(R.layout.main);
List<Map><String,Object> list=new ArrayList<Map><String,Object>();//创建动态数组ArrayList
for(int i=0;i<names.length;i++){
Map<String,Object> map=new HashMap=<String,Object>;
map.put("name",names[i]);
map.put("headerImg",imgsIds[i]);
list.add(map);
}
SimpleAdapter adapter=new SimpleAdaper(this,list,R.layout.main,new String[]{"name","headerImg"},new int[]{R.id.name,R.id.headerImg});
ListView list=(ListView)findViewById(R.id.mylist);
list.setAdaper(adapter);
}
}

 


接下来我们介绍另一个常用的Adapter:BaseAdapter
BaseAdapter implements SpinnerAdapter implements ListViewAdapter(可用于ListView和Spinner)

示例:


[java]
public MyListView extensds Activity{ 
@Override 
public void onCreate(Bundle savedInstanceState) 

super.onCreate(savedInsatnceState); 
mDate=getDate(); 
MyAdapter myadapter=new MyAdapter(this); 
ListView list=(ListView)findById(R.id.myList); 
setAdapter(myadapter); 

 private List<Map<String, Object>> getData() { 
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); 
            Map<String, Object> map = new HashMap<String, Object>(); 
            map.put("title", "G1"); 
            map.put("info", "google 1"); 
            map.put("img", R.drawable.i1); 
            list.add(map); 
     //添加第二个Map数据  
            map = new HashMap<String, Object>(); 
            map.put("title", "G2"); 
            map.put("info", "google 2"); 
            map.put("img", R.drawable.i2); 
            list.add(map); 
     //添加第三个Map数据  
            map = new HashMap<String, Object>(); 
            map.put("title", "G3"); 
            map.put("info", "google 3"); 
            map.put("img", R.drawable.i3); 
            list.add(map); 
        return list; //返回List数据  
    } 
@verride 
protected void onListItemClicked(ListView 1,View v,int position,long id){ 
Log.v("MyListView4_click",(String)mData.get(position).get("title")); 
    } 
public void showInfo(){ 
    new AlertDiolog.Builder(this) 
    .setTitle("MyListView") 
    .setMessage(Introduce) 
    .setPositiveButton("Ensure",new DialogInte易做图ce.OnClickListener() 
    {@Override 
    public void onClick(DialogInte易做图ce dialog,int which){} }).show(); 
    } 

private final class ViewHolder{ 
public ImageView img; 
public TextView title; 
public TextView info; 
public Button viewBu;    

public class MyAdapter extens BaseAdapter{ 
private LayoutInlfater myInflater; 
public MyAdapter(Context context){ 
this.myInflater=LayoutInflater.from(context); 

@Override 
public int getCount(){ 
 &n

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,