//MainActivity.java
package com.example.jkd.listview;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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 ActionBarActivity {
private ListView listView = null;
private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
private SimpleAdapter simpleAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
listView = (ListView) super.findViewById(R.id.listView);
settingAdapter();
listView.setAdapter(simpleAdapter);
}
private void settingAdapter() {
initList();
// map中的key
String from[] = new String[] { "people", "name", "introduce" };
// 模板中的组件id
int to[] = new int[] { R.id.people, R.id.name, R.id.introduce };
simpleAdapter = new SimpleAdapter(this, list, R.layout.listview_item,
from, to);
}
private void initList() {
// 显示的图片资源
int[] res = new int[] { R.drawable.maaa, R.drawable.mao,
R.drawable.maoa, R.drawable.maoaa, R.drawable.maoaaa,
};
// 定义一个二维数组来显示姓名和简介
String string[][] = new String[][] { { "猫1", "猫1简介" },
{ "猫2", "猫2简介" }, { "猫3", "猫3简介" }, { "猫4", "猫4简介" },
{ "猫5", "猫5简介" }, };
//初始化list数据
for (int i = 0; i < 9; i++) {
HashMap map = new HashMap();
map.put("people", res[i]);
map.put("name", string[i][0]);
map.put("introduce", string[i][1]);
list.add(map);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
//avtivity_main.xml
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="wrap_content" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_marginTop="17dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
//listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/people"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="TextView" />
<TextView
android:id="@+id/introduce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>