动态添加数据适配器adapter
Adapter是适配器,许多UI组件需要搭配Adapter来显示界面,例如Spinner与ListView,另外当我们需要动态的添加数据给UI组件时就需要Adapter了。
我们来介绍几个常见的Adapter:
ArrayAdapter<T>(comman)
SimpleAdapter extends Baseadapter(comman)
******************************************************************************
这两个适配器非常常见,也非常有用,通常传输的数据可以是ArrayList<>封装的数据。
示例如下:ArrayAdapter
private String[] string={"china","america","japan"};
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.simple_spinner_item,list);
首先我们定义封装数据,然后再定义Adapter,最后调用setAdapter()方法。
ArrayAdapter有三个参数,第一个参数为Activity.this,第三个参数为动态添加给adapter的数据
对于第二个数据:
R.layout.simple_spinner_item:意思是将数据传给一个简单地TextView组件
示例:SimpleAdapter
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("simpleadapter");
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)
示例:
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 DialogInterface.OnClickListener()
{@Override
public void onClick(DialogInterface 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(){
return mDate.size();}
@Override
public long getItemId(int position){
return null;}
@Override
public Object getItmeId(int position){
return 0;}
@Override
public View getview(int position,View convertView,ViewGroup parent){
ViewHolder holder=null;
if(convertView==null){
holder=new ViewHolder();
convertView=mInflater.inflate(R.layout.list,null)
//inflate函数有两个参数--解析界面布局
holder.img=(ImageView)convertView.findViewById(R.id.img);
holder.title=(TextView)convertView.findViewById(R.id.title);
holder.info=(TextView)convertView.findViewById(R.id.info);
holder.viewBu=(TextView)convertView.indViewById(R.id.viewBu);
convertView.setTag(holder);
}else{holder=(ViewHolder)convertViewgetTag();}
holder.img.setBackgroundResounce((Integer)mDate.get(position).get("img"));
holder.title.setText((String)mdata.get(position).get("title"));
holder.info.setText((String)mData.get(position).get("info"));
holder.viewBu.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
showInfo(); }
});
return convertView;
}
}
}
getView()方法有三个参数,int position表示第几行,第几个Item;convertView 表示解析过来的布局,getCount表示列表项的个数。
在使用ExpandabListView组件时,我们必须使用这个Adapter:
ExpandableAdapter:(用于ExpandableListView):
public class ExpandableListViewTest extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建一个BaseExpandableListAdapter对象
ExpandableListAdapter adapter = new BaseExpandableListAdapter()
{
int[] logos = new int[]
{
R.drawable.p,
R.drawable.z,
R.drawable.t
};
private String[] armTypes = new String[]
{ "神族兵种", "虫族兵种", "人族兵种"};
private String[][] arms = new String[][]
{
{ "狂战士", "龙骑士", "黑暗圣堂", "电兵" },
{ "小狗", "刺蛇", "飞龙", "自爆飞机" },
{ "机枪兵", "护士MM" , "幽灵" }
};
//获取指定组位置、指定子列表项处的子列表项数据
@Override
public Object getChild(int groupPosition, int childPosition)
{
return arms[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition)
{
return arms[groupPosition].length;
}
private TextView getTextView()
{
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView textView = new TextView(ExpandableListViewTest.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
return textView;
}
//该方法决定每个子选项的外观
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
//获取指定组位置处的组数据
@Override
public Object getGroup(int groupPosition)
{
return armTypes[groupPosition];
}
@Override
public int getGroupCount()
{
return armTypes.length;
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
//该方法决定每个组选项的外观
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
LinearLayout ll = new LinearLayout(ExpandableListViewTest.this);
ll.setOrientation(0);
ImageView logo = new ImageView(ExpandableListViewTest.this);
logo.setImageResource(logos[groupPosition]);
ll.addView(logo);
TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
@Override
public boolean hasStableIds()
{
return true;
}
};
ExpandableListView expandListView = (ExpandableListView)
findViewById(R.id.list);
expandListView.setAdapter(adapter);
}
}
还有一些Adapter,例如:
SimpleCursorAdapter(用于来自数据库的数据)
关键代码如下:
Cursor cur = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(cur);
//来自系统通讯录数据库的Cursor数据
ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cur, new String[] {People.NAME}, new int[] {android.R.id.text1});
setListAdapter(adapter);