80,331
社区成员
发帖
与我相关
我的任务
分享
package com.lhj.asus.myservice;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by asus on 2016/3/19.
*/
public class ListViewActivity extends Activity {
private ListView listView1;
private List<Map<String, Object>> item_list1;
private SimpleAdapter listAdapter;
//这里有六个国家的国旗图标,不过运行出来就显示了阿根廷的。
private int[] mImage1 = new int[]{
R.drawable.agentina, R.drawable.bra,
R.drawable.canada, R.drawable.china,
R.drawable.kor, R.drawable.spanin
};
private int[] mStudentId = new int[]{
1,2,3,4,5,6
};
private String[] mStudentName = new String[]{
"童安仕", "程文海",
"叶科阳", "周玉清",
"张刚", "高尚"
};
private int[] mStudentAge = new int[]{
21,22,22,21,20,21
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_listview);
listView1 = (ListView) findViewById(R.id.list);
//建立数据
item_list1 = new ArrayList<Map<String, Object>>();
//获取数据
getData();
//adapter
String[] from = {"image1", "student_ID","student_name","student_age"};
int[] to ={R.id.image, R.id.student_ID, R.id.student_name, R.id.student_age};
listAdapter = new SimpleAdapter(this, item_list1, R.layout.list_item, from, to);
//bind数据
listView1.setAdapter(listAdapter);
}
public List<Map<String,Object>> getData(){
//mImage和mImageName长度一样,任取一个
for(int i=0; i<mStudentName.length; i++){
Map<String, Object> map = new HashMap<String, Object>();
map.put("image1", mImage1[i]);
map.put("student_ID", "学生ID: "+ mStudentId[i]);
map.put("student_name", "学生姓名 :"+ mStudentName[i]);
map.put("student_age", "学生年龄 :"+ mStudentAge[i]);
item_list1.add(map);
}
return item_list1;
}
}
<?xml version="1.0" encoding="utf-8"?>
<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"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<ImageView
android:id="@+id/image1"
android:src="@drawable/agentina"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/student_ID"
android:layout_toRightOf="@id/image1"
android:layout_width="200dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/student_name"
android:layout_toRightOf="@id/image1"
android:layout_below="@id/student_ID"
android:layout_width="200dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/student_age"
android:layout_toRightOf="@id/image1"
android:layout_below="@id/student_name"
android:layout_width="200dp"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/checkbox1"
android:layout_alignParentRight="true"
android:padding="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"/>
</RelativeLayout>