listview适配器arrayAdapter
public class Myadapter extends BaseAdapter {
/*
构造器可以有多个
*/
private int images[];
private String names[],signs[];
public Myadapter(int images[],String names[],String signs[]){
this.names=names;
this.images=images;
this.signs=signs;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return names.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return names[arg0];
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
arg1=LayoutInflater.from(context).inflate(R.layout.list, null) ;
TextView name,sign;
ImageView image;
name=(TextView)arg1.findViewById(R.id.name);
sign=(TextView)arg1.findViewById(R.id.sign);
image=(ImageView)arg1.findViewById(R.id.image);
name.setText(names[arg0]);
sign.setText(signs[arg0]);
image.setImageResource(images[arg0]);
return arg1;
}
}
构造完成只需2在其他地方调用即可
xml文件代码如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/p_1"
/>
<TextView
android:id="@+id/name"
android:layout_height="30dp"
android:layout_width="match_parent"
android:text="如花"
android:layout_toRightOf="@+id/image"
android:gravity="center_vertical"
/>
<TextView
android:id="@+id/sign"
android:layout_height="30dp"
android:layout_width="wrap_content"
android:text="......."
android:textSize="10dp"
android:gravity="center_vertical"
android:textColor="#3299cc"
android:layout_below="@+id/name"
android:layout_toRightOf="@+id/image"
/>
</RelativeLayout>