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>