android使用SimpleAdapter和ListView实现列表功能例子
解决方法:
1、activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/root"
tools:context="com.example.ff.MainActivity" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</ListView>
</RelativeLayout>
2、在layout目录下新建row.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"
android:textSize="22sp" />
<TextView
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:text="详细信息" />
</RelativeLayout>
3、MainActivity.java
package com.example.ff;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
// 指定适配器
SimpleAdapter adapter = new SimpleAdapter(this, getData(),
R.layout.row, new String[] { "title", "desc" },
new int[] { R.id.title, R.id.desc });
// 绑定
listView.setAdapter(adapter);
// 事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ListView lv = (ListView) parent;
// 得到数据
Map<String, Object> item = (Map<String, Object>) lv
.getItemAtPosition(position);
Toast.makeText(MainActivity.this, "你选中了" + item.get("title"),
Toast.LENGTH_LONG).show();
}
});
}
// 数据源的方法
List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> row = new HashMap<String, Object>();
row.put("title", "照相机");
row.put("desc", "1000万像素");
row.put("img", R.drawable.camera);
list.add(row);
row = new HashMap<String, Object>();
row.put("title", "望远镜");
row.put("desc", "军方使用");
row.put("img", R.drawable.telescope);
list.add(row);
row = new HashMap<String, Object>();
row.put("title", "手电筒");
row.put("desc", "唯一的家用电器");
row.put("img", R.drawable.flashlight);
list.add(row);
return list;
}
}
本文地址:http://www.yayihouse.com/yayishuwu/chapter/1151