日期:2014-05-16  浏览次数:20645 次

在模拟器上显示数据库中提取的信息的列表
时间有限,就不写注释了,看得懂的就看,看不懂的到晚上查资料

创建一个db的androiproject项目

同时创建一个MainActivity的Acitivity.java的类

新建com.gohighsoft.db包



MainActivity.java:

package com.gohighsoft.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
private final static String TAG = "aa";
private PersonService personService;
private ListView listView;

// private PersonService personService;

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  personService = new PersonService(this);// 前十条数据
  List<Person> persons = personService.getScrollData(0, 100);
  listView = (ListView) findViewById(R.id.personList);
  List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
  HashMap<String, String> title = new HashMap<String, String>();
  title.put("personid", "编号");
  title.put("name", "姓名");
  title.put("age", "年龄");
  data.add(title);
  for (Person person : persons) {
   HashMap<String, String> map = new HashMap<String, String>();
   map.put("personid", String.valueOf(person.getId()));
   map.put("name", person.getName());
   map.put("age", String.valueOf(person.getAge()));
   data.add(map);
  }
  SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, data,
    R.layout.personitem,
    new String[] { "personid", "name", "age" }, new int[] {
      R.id.personid, R.id.name, R.id.age });
  listView.setAdapter(adapter);
  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> parent, View view,
     int position, long id) {
    ListView listView = (ListView) parent;
    HashMap<String, String> itemData = (HashMap<String, String>) listView
      .getItemAtPosition(position);
    String personid = itemData.get("personid");
    String name = itemData.get("name");
    String age = itemData.get("age");
    Log.i(TAG, "className" + view.getClass().getName());
    // 打印view的类名
    Log.i("TAG", "personid=" + personid + ",name=" + name + ",age="
      + age);
    Log.i(TAG, "result=" + (position == id));
   }
  });
}
}

在Layout文件夹下创建personitem.xml的界面配置代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:layout_width="60px" android:layout_height="60px"
  android:id="@+id/personid" />
<TextView android:layout_width="160px" android:layout_height="wrap_content"
  android:layout_toRightOf="@id/personid" android:layout_alignTop="@id/personid"
  android:gravity="center_horizontal" android:id="@+id/name" />
 
<TextView android:layout_width="wrap_content"
  android:layout_hei