在ListView中自定义Adapter

package android.myadapter;


import java.util.ArrayList;
import java.util.HashMap;


import android.app.ListActivity;
import android.os.Bundle;


public class MyAdapterActivity extends ListActivity {


private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new MyAdapter(this, getData());
setListAdapter(adapter);
}
private ArrayList<HashMap<String, Object>> getData() {
ArrayList<HashMap<String, Object>> dataArrayList = new ArrayList<HashMap<String,Object>>();
int i;
String gen,heigh,nam;
for(i=0;i<16;i++){
HashMap<String, Object> map = new HashMap<String, Object>();
if(i%2 == 0){
gen = "female";
heigh = "168cm";
nam = "XiaoZhang";
} else {
gen = "male";
heigh = "170cm";
nam = "XiaoWang";
}
map.put("name", nam);
map.put("height", heigh);
map.put("gender", gen);
dataArrayList.add(map);
}
return dataArrayList;
}
}










//自定义适配器
package android.myadapter;


import java.util.ArrayList;
import java.util.HashMap;


import android.content.Context;
import android.test.examples.R;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


public class MyAdapter extends BaseAdapter {


private Composites composites;
private LayoutInflater layoutInflater;
private Context context;
private ArrayList<HashMap<String, Object>> dataArrayList ;

public MyAdapter(Context context,
ArrayList<HashMap<String, Object>> dataArrayList) {
super();
this.context = context;
this.dataArrayList = dataArrayList;
this.layoutInflater = LayoutInflater.from(context);
}


@Override
public int getCount() {
return dataArrayList.size();
}


@Override
public Object getItem(int arg0) {
return dataArrayList.get(arg0);
}


@Override
public long getItemId(int arg0) {
return arg0;
}


//根据数据获取相应的视图
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
//在convertView为空时,即系统尚未构造出条目,此时需要找到相应的条目!
if( convertView == null ) {
composites = new Composites();
convertView = layoutInflater.inflate(R.layout.adapter_contents,null);
composites.name = (TextView) convertView.findViewById(R.id.namenew);
composites.height = (TextView) convertView.findViewById(R.id.gendernew);
composites.gender = (TextView) convertView.findViewById(R.id.gendernew);
convertView.setTag(composites);
}else {//在convertView不为空时,Recycler根据旧的item根据数据进行置换,从而直接使用旧的item即可!
convertView.getTag();
}
//加载数据
composites.name.setText(dataArrayList.get(position).get("name").toString());
composites.height.setText(dataArrayList.get(position).get("height").toString());
composites.gender.setText(dataArrayList.get(position).get("gender").toString());
return convertView;
}
}


//将组件保存于一个类中,便于接下来的使用,无需逐个创建(new ***)
package android.myadapter;


import android.widget.TextView;


public class Composites {
public TextView name;
public TextView height;
public TextView gender;
}




//布局文件


<?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="vertical" >


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >


        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.41"
            android:text="@string/TextView01"
            android:textSize="23sp" />


        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.47"
            android:text="@string/TextView02"
            android:textSize="23sp" />


        <TextView
            android:id="@+id/textView3"
            android:layout_width="86dp"
            android:layout_height="wrap_content"
            android:text="@string/TextView03"
            android:textSize="23sp" />
    </LinearLayout>


    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>


</LinearLayout>


//要显示的样式!
<?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="wrap_content"
    android:orientation="horizontal" >
        <TextView
            android:id="@+id/namenew"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.41"
            android:text="@string/TextView01"
            android:textSize="23sp" />


        <TextView
            android:id="@+id/heightnew"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.47"
            android:text="@string/TextView02"
            android:textSize="23sp" />
        <TextView
            android:id="@+id/gendernew"
            android:layout_width="86dp"
            android:layout_height="wrap_content"
            android:text="@string/TextView03"
            android:textSize="23sp" />
</LinearLayout>













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值