SimpleAdapter 简单适配器
activty_main.xml代码如下
<LinearLayout 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"
android:orientation="vertical"
>
<ListView
android:id="@+id/ListView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
————————————————————————
list_app.xml<自己写的布局文件>
<?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="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="名字"
android:textSize="18dp"
android:gravity="center_vertical"
></TextView>
</LinearLayout>
______________________________
MainActivity.java
private ListView l1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l1=(ListView)findViewById(R.id.ListView1);
//创建数据
List<HashMap<String,Object>> list=new ArrayList<HashMap<String,Object>>();
HashMap<String,Object>map1=new HashMap<String,Object>();
map1.put("name","六哥");
map1.put("image", R.drawable.ic_launcher);
HashMap<String,Object>map2=new HashMap<String,Object>();
map2.put("name","清华");
map2.put("image", R.drawable.list_1);
HashMap<String,Object>map3=new HashMap<String,Object>();
map3.put("name","骚阳");
map3.put("image", R.drawable.list_2);
list.add(map1);
list.add(map2);
list.add(map3);
SimpleAdapter adapter=new SimpleAdapter(MainActivity.this,
list,
R.layout.list_app,
new String[]{"name","image"},
new int[]{R.id.name,R.id.image}
);
// l1=(ListView)findViewById(R.id.ListView1);
l1.setAdapter(adapter);
//设置监听器
l1.setOnItemClickListener(new mItemClick());
}
class mItemClick implements OnItemClickListener
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int positon,
long arg3) {
// TODO Auto-generated method stub
HashMap<String,Object> map=(HashMap<String,Object>)parent.getItemAtPosition(positon);
Toast.makeText(MainActivity.this,"你的选择是"+map.get("name"),Toast.LENGTH_LONG).show();
}
}
注:这篇文章是本人从论坛里看到的,然后自己动手写了一下 感觉容易理解 分享给大家
//本人也是刚学安卓,肯定有很多不足的地方,那些写的不好,大家多多纠正,我及时改正,希望正在观看本文章的人 工作顺利,学业有成。