Adapter的简单用法
adapter相关及使用
话不多说了,直接上关键代码:
my_listview.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">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
list_item.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/head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bullet"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/content"
android:text="ArrayAdapter暂时设置无内容"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Myadapter.java:
public class Myadapter extends Activity{
private ListView listview;
private String[] title={"第一行","第二行","第三行","第四行"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_listview);
listview= (ListView) findViewById(R.id.lv);
listview.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item,R.id.title,title));
}
}
以上是ArrayAdapter的最为简单操作。
MySimpleAdapter.java:
public class MySimpleAdapter extends Activity{
private List<Map<String,Object>> Data;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_listview);
Data=mData();
ListView listView=(ListView) findViewById(R.id.lv);
listView.setAdapter(new SimpleAdapter(this,Data,R.layout.list_item,new String[]{"head","title","content"},new int[]{R.id.head,R.id.title,R.id.content}));
}
private List<Map<String,Object>> mData(){
List<Map<String,Object>> list=new ArrayList<Map<String, Object>>();
Map<String,Object> map1=new HashMap<String,Object>();
map1.put("head",R.drawable.packge1);
map1.put("title","简单一");
map1.put("content","这是第一个");
list.add(map1);
Map<String,Object> map2=new HashMap<String,Object>();
map2.put("head",R.drawable.packge1);
map2.put("title","简单二");
map2.put("content","这是第二个");
list.add(map2);
Map<String,Object> map3=new HashMap<String,Object>();
map3.put("head",R.drawable.packge1);
map3.put("title","简单三");
map3.put("content","这是第三个");
list.add(map3);
return list;
}
}
以上是SimpleAdapter的简单操作
关于adapter的使用,贴出关键代码和注意项
listview= (ListView) findViewById(R.id.lv);
listview.setAdapter(new myadapter());
}
private class myadapter extends BaseAdapter{
/**
* 控制Listview里面总共有多少个条目
*/
@Override
public int getCount() {
return persons.size();//条目个数==集合的size
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public Object getItem(int i) {
return null;
}
/**
*展现数据集合,某一个位置所对应的数据,int i:当前数据的位置
*/
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
return null;
}
}
这里adapter用了继承BaseAdapter方法。
最后说一下BaseAdapter的使用,这也是最为常用的方法。
有时候,列表不光会用来做显示用,我们同样可以在在上面添加按钮。添加按钮首先要写一个有按钮的xml文件,然后自然会想到用上面的方法定义一个适配器,然后将数据映射到布局文件上。但是事实并非这样,因为按钮是无法映射的,即使你成功的用布局文件显示出了按钮也无法添加按钮的响应,这时就要研究一下ListView是如何现实的了,而且必须要重写一个类继承BaseAdapter。
XML基本与前面相同,就修改了一下List_item.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/head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bullet"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/content"
android:text="ArrayAdapter暂时设置无内容"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/view_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
BaseAdapter.java全部代码
public class MyBaseAdapter extends ListActivity {
private List<Map<String, Object>> mData;
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mData=getData();
Myadapter myadapter=new Myadapter(this);
setListAdapter(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("head",R.drawable.bullet);
map.put("title","1x");
map.put("content","我是第一个");
list.add(map);
map=new HashMap<String, Object>();
map.put("head",R.drawable.bullet);
map.put("title","2x");
map.put("content","我是第二个");
list.add(map);
map=new HashMap<String, Object>();
map.put("head",R.drawable.bullet);
map.put("title","3x");
map.put("content","我是第三个");
list.add(map);
return list;
}
protected void onListItemClick(ListView l, View v, int position, long id){
Log.v("MyListView4-click", (String)mData.get(position).get("title"));
}
public void showniew(){
new AlertDialog.Builder(this)
.setTitle("BuseAdapter")
.setMessage("这是什么信息")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
/* switch (which){
case R.id.view_btn.;
break;
}*/
}
}).show();
}
public class Myadapter extends BaseAdapter{
private LayoutInflater inflater;
public Myadapter(Context context){
this.inflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder=null;
if (view==null){
holder=new ViewHolder();
view=inflater.inflate(R.layout.list_item,null);
holder.head=(ImageView) view.findViewById(R.id.head);
holder.title=(TextView) view.findViewById(R.id.title);
holder.content=(TextView) view.findViewById(R.id.content);
holder.viewBtn=(Button) view.findViewById(R.id.view_btn);
view.setTag(holder);
}else {
holder=(ViewHolder)view.getTag();
}
holder.head.setBackgroundResource((Integer)mData.get(i).get("head"));
holder.title.setText((String)mData.get(i).get("title"));
holder.content.setText((String)mData.get(i).get("content"));
holder.viewBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showniew();
}
});
return view;
}
}
public final class ViewHolder{
public ImageView head;
public TextView title;
public TextView content;
public Button viewBtn;
}
}
listView在开始绘制的时候,系统首先调用getCount()函数,根据他的返回值得到listView的长度,然后根据这个长度,调用getView()逐一绘制每一行。如果你的getCount()返回值是0的话,列表将不显示同样return 1,就只显示一行。在实际的运行过程中会发现listView的每一行没有焦点了,这是因为Button抢夺了listView的焦点,只要布局文件中将Button设置为没有焦点就OK了;实例化一个自定义的适配器,要手动映射数据,所以要重写getview()方法。系统在绘制列表的每一行的时候将调用此方法。getView()有三个参数,position表示将显示的是第几行,covertView是从布局文件中inflate来的布局,用LayoutInflate(打气筒)实例化xml的组件。
此外Adapter中为什么用Arraylist和HashMap的原因是:
ArrayList和HashMap是异步的,Vector和HashTable是同步的,所以Vector和HashTable是线程安全的,而ArrayList和HashMap并不是线程安全的。因为同步需要花费机器时间,所以Vector和HashTable的执行效率要低于ArrayList和HashMap。
**原因是看了很多,牵扯过多就不详解了,链接**