Android(三)数据存储之三SQLite嵌入式数据库 3
在mail.xml所在目录里添加一个contactitem.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content">
<!-- contact.id --> <TextView android:layout_width="30dip" android:layout_height="wrap_content" android:textSize="20sp" android:id="@+id/tv_id" />
<!-- contact.name --> <TextView android:layout_width="150dip" android:layout_height="wrap_content" android:textSize="20sp" android:layout_toRightOf="@id/tv_id" android:layout_alignTop="@id/tv_id" android:id="@+id/tv_name" />
<!-- contact.phone --> <TextView android:layout_width="150dip" android:layout_height="wrap_content" android:textSize="20sp" android:layout_toRightOf="@id/tv_name" android:layout_alignTop="@id/tv_name" android:id="@+id/tv_phone" />
</RelativeLayout> |
编辑AndroidSQLite类:
package com.changcheng.sqlite;
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import com.changcheng.sqlite.R; import com.changcheng.sqlite.entity.Contact; import com.changcheng.sqlite.service.ContactsService; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener;
public class AndroidSQLite extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 获取分页数据 ContactsService contactsService = new ContactsService(this); List<Contact> contacts = contactsService.getScrollData(0, 3); // 获取ListView ListView lv = (ListView) this.findViewById(R.id.listView); // 生成List<? extends Map<String, ?>>数据 List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); for (Contact contact : contacts) { HashMap<String, Object> item = new HashMap<String, Object>(); item.put("_id", contact.get_id()); item.put("name", contact.getName()); item.put("phone", contact.getPhone()); data.add(item); } // 生成Adapter SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.contactitem, new String[] { "_id", "name", "phone" }, new int[] { R.id.tv_id, R.id.tv_name, R.id.tv_phone }); // 设置ListView适配器 lv.setAdapter(adapter);
// 为ListView添加事件 lv.setOnItemClickListener(new OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { HashMap<String, Object> item = (HashMap<String, Object>) parent .getItemAtPosition((int) id); Toast.makeText(AndroidSQLite.this, item.get("name").toString(), 1).show(); }
}); } } |
上面编写的分页显示数据比较麻烦,Android为我们提供了一个SimpleCursorAdapter类。使用它可以方便的显示分页数据。将AndroidSQLite类修改为:
package com.changcheng.sqlite;
import com.changcheng.sqlite.R; import com.changcheng.sqlite.service.ContactsService; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleCursorAdapter;
public class AndroidSQLite extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
// 获取分页数据 ContactsService contactsService = new ContactsService(this); Cursor cursor = contactsService.getScrollDataCursor(0, 3); // 获取ListView ListView lv = (ListView) this.findViewById(R.id.listView); // 创建Adapter SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contactitem, cursor, new String[] { "_id", "name", "phone" }, new int[] { R.id.tv_id, R.id.tv_name, R.id.tv_phone }); // 设置ListView适配器 lv.setAdapter(adapter);
// 为ListView添加事件 lv.setOnItemClickListener(new OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Cursor cursor = (Cursor) parent .getItemAtPosition((int) position); Toast.makeText(AndroidSQLite.this, cursor.getString(1), 1) .show(); } }); } } |
OK,在Android中的SQLite操作总结结束!