SimpleAdapter适配的ListView(不常用)

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_adpter);
mListView= (ListView) findViewById(R.id.list_itemmm);
// ArrayAdapter<String> adapter= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,array);
// mListView.setAdapter(adapter);
gettdata();
SimpleAdapter adapter=new SimpleAdapter(this,mData,R.layout.simple_adpter,new String[]{"name","age","sex","hobby"},new int[]{R.id.text_name,R.id.text_age,R.id.text_sex,R.id.text_hobby});
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//不允许使用onclicklistener
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//position指的是点击的第position个数据
HashMap<String,String> itemdata=mData.get(position);
Log.d("data",""+itemdata.get("name")+itemdata.get("age")+itemdata.get("sex")+itemdata.get("hobby")) ;
}
});

}

private void gettdata() {
mData=new ArrayList<>();
HashMap<String,String> zhangsan=createHashMap("张三","18","","爱好篮球");
mData.add(zhangsan);
HashMap<String,String> lisi=createHashMap("李四","17","","爱好吃");
mData.add(lisi);
HashMap<String,String> wangwu=createHashMap("王五","20","","爱好游戏");
mData.add(wangwu);
HashMap<String,String> zhaoliu=createHashMap("赵六","13","","爱好睡");
mData.add(zhaoliu);
}
private HashMap<String,String> createHashMap(String name,String age,String sex,String hobby){
HashMap<String,String> hashmap=new HashMap<>();
hashmap.put("name",name);
hashmap.put("age",age);
hashmap.put("sex",sex);
hashmap.put("hobby",hobby);
return hashmap;
}

}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_itemmm"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/text_name"
android:text="名字"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/text_age"
android:text="年龄"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_sex"
android:text="性别"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="@+id/text_hobby"
android:text="爱好"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值