SimpleAdapter 实例
调用的Layout文件样式
<?xml version="1.0" encoding="utf-8"?><ImageView
android:id="@+id/image_0"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:layout_weight="2"
android:adjustViewBounds="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:layout_weight="1"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="姓名: "
android:textSize="17dp"/>
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#f00"
android:textSize="10pt" />
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="年龄: "
android:textSize="17dp"/>
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#f00"
android:textSize="10pt">
</TextView>
</TableRow>
</TableLayout>
</LinearLayout>
Layout文件.xml
<?xml version="1.0" encoding="utf-8"?> <ListView
android:id="@+id/listview_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
Main.java
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends Activity {
SimpleAdapter simpleAdapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adapter);
ArrayList<Map<String, Object>> arrayList = new ArrayList<Map<String, Object>>();
String[] names = new String[]{"张三", "李四", "网速", "赵六"};
int[] ages=new int[]{10,20,30,40};
int[] imageIDs=new int[]{R.drawable.lijiang,R.drawable.qiao,R.drawable.shuangta,R.drawable.shui};
listView = findViewById(R.id.listview_id);
for(int i=0;i<4;i++){
Map<String,Object> map=new HashMap<>();
map.put("image",imageIDs[i]);
map.put("name",names[i]);
map.put("age",ages[i]);
arrayList.add(map);
}
String[] from=new String[]{"image","name","age"};
int[] to=new int[]{R.id.image_0,R.id.tv1,R.id.tv2};
simpleAdapter=new SimpleAdapter(this,arrayList,R.layout.textview_layout,from,to);
listView.setAdapter(simpleAdapter);
}
}
效果图