08_Android中的SimpleAdapter的使用
1 目的界面
?
?
?
?
?
?
?
?
?
?
?
???????????????? ?
2、編寫Android清單文件
| <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" ??? package="com.itheima28.simpleadapterdemo" ??? android:versionCode="1" ??? android:versionName="1.0" > ? ??? <uses-sdk ??????? android:minSdkVersion="8" ??????? android:targetSdkVersion="19" /> ? ??? <application ??????? android:allowBackup="true" ??????? android:icon="@drawable/ic_launcher" ??????? android:label="@string/app_name" ??????? android:theme="@style/AppTheme" > ??????? <activity ??????????? android:name="com.itheima28.simpleadapterdemo.MainActivity" ??????????? android:label="@string/app_name" > ??????????? <intent-filter> ??????????????? <action android:name="android.intent.action.MAIN" /> ? ??????????????? <category android:name="android.intent.category.LAUNCHER" /> ??????????? </intent-filter> ??????? </activity> ??? </application> ? </manifest> |
3 activity_main.xml的文件內容
| <RelativeLayout 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" ??? tools:context=".MainActivity" > ??? <ListView ??????? android:id="@+id/listview" ??????? android:layout_width="match_parent" ??????? android:layout_height="match_parent"/> ??? </RelativeLayout> |
4 listview_item.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="wrap_content" ??? android:gravity="center_vertical" ??? android:orientation="horizontal" ??? android:padding="10dip" > ??? ??? <ImageView ??????? android:id="@+id/iv_icon" ??????? android:layout_width="wrap_content" ??????? android:layout_height="wrap_content" ??????? android:src="@drawable/f007" /> ??? ??? <TextView ??????? android:id="@+id/tv_name" ??????? android:layout_width="wrap_content" ??????? android:layout_height="wrap_content" ??????? android:layout_marginLeft="10dip" ??????? android:text="張三" ??????? android:textColor="#FF0000" ??????? android:textSize="23sp"/> ??? </LinearLayout> |
5 MainActivity的內容如下:
| package com.itheima28.simpleadapterdemo; ? import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; ? import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.widget.ListView; import android.widget.SimpleAdapter; ? public class MainActivity extends ActionBarActivity { ? ??? @Override ??? protected void onCreate(Bundle savedInstanceState) { ?????? super.onCreate(savedInstanceState); ?????? setContentView(R.layout.activity_main); ?????? ?????? ListView mListView = (ListView) findViewById(R.id.listview); ?????? ?????? List<Map<String, Object>> data = new ArrayList<Map<String,Object>>(); ?????? ?????? Map<String, Object> map = new HashMap<String,Object>(); ?????? map.put("name", "張三1"); ?????? map.put("icon", R.drawable.f007); ??? ??? data.add(map);??? ??? ??? ??? map = new HashMap<String,Object>(); ??? ??? map.put("name", "張三2"); ??? ??? map.put("icon", R.drawable.f007); ??? ??? data.add(map); ??? ??? ??? ??? map = new HashMap<String,Object>(); ??? ??? map.put("name", "張三3"); ??? ??? map.put("icon", R.drawable.f007); ??? ??? data.add(map); ??? ??? ??? ??? map = new HashMap<String,Object>(); ??? ??? map.put("name", "張三4"); ??? ??? map.put("icon", R.drawable.f007); ??? ??? data.add(map); ??? ??? ??? ??? map = new HashMap<String,Object>(); ??? ??? map.put("name", "張三5"); ??? ??? map.put("icon", R.drawable.f007); ??? ??? data.add(map); ??? ??? ??? ??? SimpleAdapter adapter = new SimpleAdapter( ??? ??? ?????? this,?? //上下文 ??? ??? ?????? data,?? //listView綁定的數據 ??? ??? ?????? R.layout.listview_item, //listview的子條目的布局的id ??? ??? ?????? new String[]{"name","icon"}, //data數據中的map集合里的key ??? ??? ?????? new int[]{R.id.tv_name,R.id.iv_icon}); //resource中的id ??? ??? ??? ??? mListView.setAdapter(adapter); ??? } } |
?
?
?
?
總結
以上是生活随笔為你收集整理的08_Android中的SimpleAdapter的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 07_Android操作sqllite数
- 下一篇: 小孩湿疹会不会传染给别人(湿疹会不会传染