您的位置:首页 > 移动开发 > Android开发

自定义适配器

2014-12-05 16:39 309 查看
使用自定义适配器,完成一个通讯录并且能够实现打电话的程序:

先完成整个条目的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<ListView
android:id="@+id/lv_constact"
android:layout_width="match_parent"
android:layout_height="wrap_content"

></ListView>

</RelativeLayout>

再完成条目中的每个对象,图片+姓名+电话号码:
<?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="wrap_content"
android:orientation="horizontal"
android:padding="10dp" >
<ImageView
android:id="@+id/iv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_launcher" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_gravity="center_vertical"
android:orientation="vertical" >

<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="张飞" />

<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="188888888" />
</LinearLayout>

</LinearLayout>


因为每个条目中的对象是一个集合,所以可以把里面的对象写成一个javabean文件:
public class Person {
private int icon;
private String name;
private String phone;

public Person() {
super();
// TODO Auto-generated constructor stub
}

public Person(int icon, String name, String phone) {
super();
this.icon = icon;
this.name = name;
this.phone = phone;
}

public int getIcon() {
return icon;
}

public void setIcon(int icon) {
this.icon = icon;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

}


在src目录下的xml文件中:package com.mzx.apdate;

import java.util.ArrayList;
import java.util.List;

import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity implements InitInterface{

private ListView lv_constact;
private List<Person> persons;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initView();
initData();
initListener();

}

@Override
public void initView() {
lv_constact = (ListView) this.findViewById(R.id.lv_constact);

}

@Override
public void initData() {
// TODO Auto-generated method stub
persons=initPersonData();
Myadapter adapter=new Myadapter();
lv_constact.setAdapter(adapter);
}

private List<Person> initPersonData() {
List<Person> persons = new ArrayList<Person>();
Person person1 = new Person(R.drawable.face1, "刘备", "12345884543");
persons.add(person1);

Person person2 = new Person(R.drawable.face2, "关羽", "13888454543");
persons.add(person2);

Person person3 = new Person(R.drawable.face3, "张飞", "13888823543");
persons.add(person3);

Person person4 = new Person(R.drawable.face4, "吕布", "13881245543");
persons.add(person4);

Person person5 = new Person(R.drawable.face5, "董卓", "13887455433");
persons.add(person5);

Person person6 = new Person(R.drawable.face6, "孙权", "13823345543");
persons.add(person6);

Person person7 = new Person(R.drawable.face7, "赵云", "13854845543");
persons.add(person7);

Person person8 = new Person(R.drawable.face8, "赵兴", "13834845543");
persons.add(person8);

return persons;
}

@Override
public void initListener() {
// TODO Auto-generated method stub
lv_constact.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Person person=persons.get(position);
Intent intent=new Intent();
intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+person.getPhone()));
startActivity(intent);

}
});

}

}
在xml文件中继承接口类InitInterface,里面的方法有:
initView();
initData();
initListener();

实现未实现的方法:

在initView()方法中找到布局文件中控件,并将获得的对象变为全局变量,方便下面的方法的使用。

自定义一个List集合,将Person对象放入集合中,并生成方法:private List<Person> initPersonData()

在方法中添加每个对象中的图片,姓名,电话号码,返回persons对象。

要实现打电话功能,在initListener()中设置点击的监听,并通过Intent意图实现打电话功能,注意要在清单文件中添加打电话的权限。

自定义一个类,继承BaseAdapter,这个适配器中很多方法系统已经写好,只需要写四个方法。

private class MyAdapter extends BaseAdapter{

@Override
public int getCount() {
// TODO Auto-generated method stub
return persons.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return persons.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater layoutInflater = MainActivity.this.getLayoutInflater();
View view = layoutInflater.inflate(R.layout.listview, null);
ImageView iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
TextView tv_phone = (TextView) view.findViewById(R.id.tv_phone);
TextView tv_name = (TextView) view.findViewById(R.id.tv_name);

Person person=persons.get(position);

iv_icon.setImageResource(person.getIcon());
tv_phone.setText(person.getPhone());
tv_name.setText(person.getName());

return view;
}getCount方法返回的是对象的个数,persons.size();
getItem方法返回的是具体对象,里面带有的参数是对象的下标

getItemId方法返回的对象的下标

getView返回的是视图,可以优化。

四个方法中最重要的是getCount   和   getView方法。

在getView方法创建一个LayoutInflater,相当于一个打气筒,可以将布局文件变为View

然后找到每个对象中的控件,并为控件设置,返回View

现在来写initData()方法:

@Override
public void initData() {
// TODO Auto-generated method stub
persons=initPersonData();
Myadapter adapter=new Myadapter();
lv_constact.setAdapter(adapter);
}

在方法中将刚才所写的内部类Myadapter放进去就可以了

这个程序的功能已经完成了,现在改变一下自定义适配器的方法,将内部类的自定义适配器改为外部类的。新建一个java类

public class Myadapter extends BaseAdapter {
private List<Person> persons;
private Context context;
public Myadapter(Context context,List<Person> persons) {
// TODO Auto-generated constructor stub
this.context=context;
this.persons=persons;

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return persons.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return persons.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

LayoutInflater layoutInflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.listview, null);
ImageView iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
TextView tv_phone = (TextView) view.findViewById(R.id.tv_phone);
TextView tv_name = (TextView) view.findViewById(R.id.tv_name);

Person person=persons.get(position);

iv_icon.setImageResource(person.getIcon());
tv_phone.setText(person.getPhone());
tv_name.setText(person.getName());

return view;
}里面的方法和内容跟之前内部类的是一样的,注意要创建Person集合对象才能使用。
不同的是,LayoutInflater对象中需要上下文的参数,这个类中没有,所以要传入一个,这个时候就创建一个Context,它的作用和上下文是一样的,不过它可以在各种方法中调用,便于操作。

把之前的内部类注释掉,然后在public void initData()改为:

@Override
public void initData() {
// TODO Auto-generated method stub
persons=initPersonData();
Myadapter adapter=new Myadapter(MainActivity.this, persons);
lv_constact.setAdapter(adapter);
}


就可以完成将内部类自定义适配器改为外部类啦。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android