java适配器各三种_适配器三种
適配器有:
作用:用于將數(shù)據(jù)綁定到組件上
過程:寫的內(nèi)容----->適配器
控件------------------->通過適配器----->listView的布局
1,ArrayAdapter列表適配器
2,SimpleCursorAdapter簡(jiǎn)單標(biāo)游適配器
3,BaseAdapter基礎(chǔ)適配器
(2,3都可以用復(fù)雜的Lietview)
1,ArrayAdapter列表適配器(只能寫TextView體系的控件)
java代碼寫:
package com.example.layou_text;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class CopyOfdi1Activity extends Activity {
ListView lv_man;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lv_man);//加載(是布局不是id!)
lv_man=(ListView)findViewById(R.id.lv_man);//獲取操作
// 注意寫法不能寫R.layout.獲取布局
//準(zhǔn)備數(shù)據(jù)
String [] data={"a","b","c","d","e","f","g","h"};
//構(gòu)造arrayaadapter適配(上下文,加載另一個(gè)布局id,傳入數(shù)據(jù)(數(shù)據(jù)會(huì)被傳入布局中))
ArrayAdapter adapter = new ArrayAdapter(CopyOfdi1Activity.this,
R.layout.item_array_adapter,data);
// 加載textview體系的單個(gè)布局
//給lv_man設(shè)置適配器
lv_man.setAdapter(adapter);
}}
xml:(定義一個(gè)listview布局)id+lv_man.xml
android:id="@+id/lv_man"
android:layout_width="match_parent"
android:layout_height="match_parent" >
再寫一個(gè)xml(定義listView里面的內(nèi)容textview)
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp"
android:gravity="start"
>
ArrayAdapter效果圖
image.png
2,SimpleAdapter簡(jiǎn)單的適配器
java寫:
package com.example.layou_text;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class CopyOfdi2Activity extends Activity {
ListView lv_man;
List data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lv_man);//加載(是布局不是id!)
lv_man=(ListView) findViewById(R.id.lv_man);//獲取操作
ArrayList> arrayList = new ArrayList>();
//new了一個(gè)Arraylist數(shù)組包含map集合
HashMap hashMap = new HashMap();
//new了一個(gè)map集合編寫key value
hashMap=new HashMap();
//簡(jiǎn)寫用上面hashMap的對(duì)象再新建一個(gè)map集合,賦值,添加到arrayList集合中
hashMap.put("icom", R.drawable.f2);
hashMap.put("name","美食--2");
hashMap.put("content","內(nèi)容");
arrayList.add(hashMap);
hashMap=new HashMap();
hashMap.put("icom", R.drawable.f3);
hashMap.put("name","美食--3");
hashMap.put("content","內(nèi)容");
arrayList.add(hashMap);
hashMap=new HashMap();
hashMap.put("icom", R.drawable.f4);
hashMap.put("name","美食--4");
hashMap.put("content","內(nèi)容");
arrayList.add(hashMap);
hashMap=new HashMap();
hashMap.put("icom", R.drawable.f5);
hashMap.put("name","美食--5");
hashMap.put("content","內(nèi)容");
arrayList.add(hashMap);
hashMap=new HashMap();
hashMap.put("icom", R.drawable.f6);
hashMap.put("name","美食--6");
hashMap.put("content","內(nèi)容");
arrayList.add(hashMap);
hashMap=new HashMap();
hashMap.put("icom", R.drawable.f7);
hashMap.put("name","美食--7");
hashMap.put("content","內(nèi)容");
arrayList.add(hashMap);
hashMap=new HashMap();
hashMap.put("icom", R.drawable.f8);
hashMap.put("name","美食--8");
hashMap.put("content","內(nèi)容");
arrayList.add(hashMap);
hashMap=new HashMap();
hashMap.put("icom", R.drawable.f9);
hashMap.put("name","美食--9");
hashMap.put("content","內(nèi)容");
arrayList.add(hashMap);
hashMap=new HashMap();
hashMap.put("icom", R.drawable.f10);
hashMap.put("name","美食--10");
hashMap.put("content","內(nèi)容");
arrayList.add(hashMap);
//arrayList集合里面有許多個(gè)map集合,map集合中有許多的數(shù)據(jù)
// Map對(duì)象中的key的數(shù)組,用于得到對(duì)應(yīng)的value
String from[]={"icom","name","content"};//定義from代表map里面的key值
// item布局文件中的子View的id的數(shù)組
int [] to ={R.id.imageView1,R.id.tv_item_name,R.id.tv_item_content,};
//定義to代表布局控件
// 準(zhǔn)備SimpleA
SimpleAdapter simpleAdapter = new SimpleAdapter(this, arrayList,
R.layout.activity_chatn, from, to);
//加載包含to的控件視圖(不是視圖id),from是通過key獲取了value值,
// to是把value賦給了布局中的控件
lv_man.setAdapter(simpleAdapter);
// 設(shè)置適配器
}
}
lv_man.xml布局寫
android:id="@+id/lv_man"
android:layout_width="match_parent"
android:layout_height="match_parent" >
再寫一個(gè)xml(定義listView里面的內(nèi)容控件)
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
android:id="@+id/imageView1"
android:layout_width="80dp"
android:layout_height="80dp"
android:gravity="center_vertical"
android:src="@drawable/f1" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
>
android:id="@+id/tv_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
android:id="@+id/tv_item_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
/>
效果圖
image.png
3,BaseAdapter基礎(chǔ)適配器(需要繼承是抽象類)
java寫
package com.example.layou_text;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends Activity {
ListView lv_man;//列表視圖
List data;//不用map用了shoinfo類來儲(chǔ)存數(shù)據(jù)集合
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lv_man);//加載(是布局不是id!)
lv_man=(ListView) findViewById(R.id.lv_man);//獲取操作
// 準(zhǔn)備集合數(shù)據(jù)
data =new ArrayList();
data.add(new shoinfo(R.drawable.f1,"美食名--1","美食內(nèi)容--1"));
data.add(new shoinfo(R.drawable.f2,"美食名--2","美食內(nèi)容--2"));
data.add(new shoinfo(R.drawable.f3,"美食名--3","美食內(nèi)容--3"));
data.add(new shoinfo(R.drawable.f4,"美食名--4","美食內(nèi)容--4"));
data.add(new shoinfo(R.drawable.f5,"美食名--5","美食內(nèi)容--5"));
data.add(new shoinfo(R.drawable.f6,"美食名--6","美食內(nèi)容--6"));
data.add(new shoinfo(R.drawable.f7,"美食名--7","美食內(nèi)容--7"));
data.add(new shoinfo(R.drawable.f8,"美食名--8","美食內(nèi)容--8"));
data.add(new shoinfo(R.drawable.f9,"美食名--9","美食內(nèi)容--9"));
data.add(new shoinfo(R.drawable.f10,"美食名--10","美食內(nèi)容--10"));
// 準(zhǔn)備Baseadapter對(duì)象
Myadapter myadapter = new Myadapter();
//設(shè)置Adapter顯示列表
lv_man.setAdapter(myadapter);
}
class Myadapter extends BaseAdapter{//定義一個(gè)局部?jī)?nèi)部類繼承BaseAdapter
//返回集合的總數(shù)
@Override
public int getCount() {
return data.size();
}
//返回指定下標(biāo)對(duì)應(yīng)的數(shù)據(jù)對(duì)象
@Override
public Object getItem(int position) {
return data.get(position);
}
//返回每個(gè)條目的ID
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
//返回指定下標(biāo)所對(duì)應(yīng)的Item的View對(duì)象(試圖和總數(shù))
// position下標(biāo)
// convertView可以復(fù)用的緩存Item視圖對(duì)象, 前n+1個(gè)為null
// parent listView對(duì)象
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 1.創(chuàng)建或得到對(duì)應(yīng)當(dāng)前行的一個(gè)Viewholder對(duì)象
ViewHolder hole=null;
if(convertView==null){
// 加載布局Item的布局,得到View對(duì)象
convertView = View.inflate(MainActivity.this, R.layout.activity_chatn, null);
hole=new ViewHolder();
// 獲取到convertView的子view的控件
hole.findViewById = (ImageView) convertView.findViewById(R.id.imageView1);
hole.view = (TextView) convertView.findViewById(R.id.tv_item_name);
hole.view1 = (TextView) convertView.findViewById(R.id.tv_item_content);
// 將holder對(duì)象保存到converterView上
convertView.setTag(hole);
}else{
hole=(ViewHolder)convertView.getTag();
}
// 根據(jù)position設(shè)置對(duì)應(yīng)的數(shù)據(jù)
// 得到當(dāng)行的數(shù)據(jù)對(duì)象
shoinfo shoinfo = data.get(position);
// 得到子View對(duì)象
// 給ViewHolder對(duì)象的視圖設(shè)置數(shù)據(jù)
hole.findViewById.setImageResource(shoinfo.getIncon());
hole.view.setText(shoinfo.getName());
hole.view1.setText(shoinfo.getContent());
return convertView;
}
//數(shù)據(jù)類
class ViewHolder{
public ImageView findViewById;
public TextView view;
public TextView view1;
}
}
}
java寫一個(gè)存放數(shù)據(jù)類
package com.example.layou_text;
public class shoinfo {
public int incon;
public String name;
public String content;
@Override
public String toString() {
return "shoinfo [incon=" + incon + ", name=" + name + ", content="
+ content + "]";
}
public int getIncon() {
return incon;
}
public void setIncon(int incon) {
this.incon = incon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public shoinfo(int incon, String name, String content) {
super();
this.incon = incon;
this.name = name;
this.content = content;
}
}
定義一個(gè)listview(id+lv_man)
android:id="@+id/lv_man"
android:layout_width="match_parent"
android:layout_height="match_parent" >
再寫一個(gè)xml(定義listView里面的內(nèi)容控件)
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
android:id="@+id/imageView1"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/f1" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
>
android:id="@+id/tv_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
android:id="@+id/tv_item_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
/>
效果圖
image.png
總結(jié)
以上是生活随笔為你收集整理的java适配器各三种_适配器三种的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java异常处理图片_Java处理图片时
- 下一篇: 达摩java_JAVA面向对象