分类: Android平台
2013-08-26 09:02:38
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package com.example.arrayadapter;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
//声明视图ListView控件
private ListView lv;
//声明适配器(控制器)ArrayAdapter
private ArrayAdapter
//声明并定义数据源
private String datas[]={
"Wireless & networks","Call settings","Sound","Display","Location & security",
"Applications","Accounts & sync","Privacy","CD card & phone storage","Search","Language & keyboard",
"Voice input & output","Accessibility","Date & Time","About phone"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//实现适配器,包括三个参数
aAdapter=new ArrayAdapter
lv=(ListView)findViewById(R.id.listView1);
//通过Adapter的setAdapter方法绑定数据于视图
lv.setAdapter(aAdapter);
}
}
|
(2)activity_main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
xmlns:tools=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
tools:context=".MainActivity" >
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
|
(3)引入的资源文件cell.xml
1
2
3
4
5
6
7
|
android:layout_width="fill_parent"
android:layout_height="50dip"
android:gravity="center_vertical"
android:id="@+id/tv"
android:textColor="#ffffff">
|
(4)实现效果如下:
2.SimpleAdapter类的使用
相对于ArrayAdapter而言SimpleAdapter的实现的功能更全面一些,不仅可也实现TextView的绑定,还可以在引用的资源文件(cell.xml)中使用布局,从而绑定ImageView等其他一些控件,接下来我们就来完善上面这个布局:
(1)MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package com.example.arrayadapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
//声明视图ListView控件
private ListView lv;
//声明适配器(控制器)SimpleAdapter
private SimpleAdapter sAdapter;
//声明List和Map存放数据
private List
private Map
//声明并定义数据源
private String datas[]={
"Wireless & networks","Call settings","Sound","Display","Location & security",
"Applications","Accounts & sync","Privacy","CD card & phone storage","Search",
"Language & keyboard","Voice input & output","Accessibility","Date & Time","About phone"};
private int image[]={
R.drawable.ic_settings_wireless,R.drawable.ic_settings_call,R.drawable.ic_settings_sound,
R.drawable.ic_settings_display,R.drawable.ic_settings_location,R.drawable.ic_settings_applications,
R.drawable.ic_settings_sync,R.drawable.ic_bt_config,R.drawable.ic_settings_sim,
R.drawable.ic_power_system,R.drawable.ic_settings_language,R.drawable.ic_settings_voice_calls,
R.drawable.ic_settings_accessibility,R.drawable.ic_settings_date_time,R.drawable.ic_settings_about};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.listView1);
lists=new ArrayList
//通过for循环添加数据
for(int i=0;i
maps=new HashMap
maps.put("data", datas[i]);
maps.put("image", image[i]);
lists.add(maps);
}
//实现SimpleAdapter适配器,有5个参数
sAdapter=new SimpleAdapter(this, lists, R.layout.cell, new String[]{"data","image"}, new int[]{R.id.tv,R.id.iv});
//通过Adapter的setAdapter方法绑定数据于视图
lv.setAdapter(sAdapter);
}
}
|
(2)activity_main.java同上
(3)资源文件cell.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
xmlns:tools=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity" >
android:layout_width="50dip"
android:layout_height="50dip"
android:id="@+id/iv"/>
android:layout_width="fill_parent"
android:layout_height="50dip"
android:gravity="center_vertical"
android:id="@+id/tv"
android:textColor="#ffffff">
|
(4)测试结果图:
3.自定义的Adapter——BaseAdapter的使用:
如果系统提供的适配器不能满足开发人员的要求,我们也可以通过BaseAdapter来自定义满足需要的适配器,这里为了说明情况我们就用BaseAdapter自定义的适配器(下面我们叫MyAdapter)来完成和上面相同的功能。
(1)MyAdapter.java(新建的类,实现自定义的适配器)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package myAdapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.arrayadapter.R;
public class MyAdapter extends BaseAdapter{
private Context context;
private String data[];
private int image[];
public MyAdapter(Context context,String[] data,int[] image){
this.context=context;
this.data=data;
this.image=image;
}
//重写一下四个方法,并修改返回值
@Override
public int getCount() {
return this.data.length;
}
@Override
public Object getItem(int position) {
return data[position];
}
@Override
public long getItemId(int position) {
return position;
}
//这个重写的方法是最重要的,用于加载视图
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater lif=(LayoutInflater)
context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout=(LinearLayout)
lif.inflate(R.layout.cell, null);
TextView tv=(TextView)layout.findViewById(R.id.tv);
ImageView iv=(ImageView)layout.findViewById(R.id.iv);
tv.setText(data[position]);
iv.setBackgroundResource(image[position]);
return layout;
}
}
|
(2)MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package com.example.arrayadapter;
import myAdapter.MyAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity {
//声明视图ListView控件
private ListView lv;
//声明适配器(控制器)MyAdapter
private MyAdapter mAdapter;
//声明并定义数据源
private String datas[]={
"Wireless & networks","Call settings","Sound","Display","Location & security",
"Applications","Accounts & sync","Privacy","CD card & phone storage","Search",
"Language & keyboard","Voice input & output","Accessibility","Date & Time","About phone"};
private int image[]={
R.drawable.ic_settings_wireless,R.drawable.ic_settings_call,R.drawable.ic_settings_sound,
R.drawable.ic_settings_display,R.drawable.ic_settings_location,R.drawable.ic_settings_applications,
R.drawable.ic_settings_sync,R.drawable.ic_bt_config,R.drawable.ic_settings_sim,
R.drawable.ic_power_system,R.drawable.ic_settings_language,R.drawable.ic_settings_voice_calls,
R.drawable.ic_settings_accessibility,R.drawable.ic_settings_date_time,R.drawable.ic_settings_about};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.listView1);
//实现MyAdapter适配器
mAdapter=new MyAdapter(this,datas,image);
//通过Adapter的setAdapter方法绑定数据于视图
lv.setAdapter(mAdapter);
}
}
|
(3)运行效果:
(4)最后补充一个问题——ListView的缓冲
让我们来看一下MyAdapter.java代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).
inflate(R.layout.cell,null);
holder = new ViewHolder();
holder.tv = (TextView) convertView.findViewById(R.id.tv);
holder.iv = (ImageView) convertView.findViewById(R.id.iv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText(data[position]);
holder.iv.setBackgroundResource(image[position]);
return convertView;
}
private static class ViewHolder {
TextView tv;
ImageView iv;
}
|