Android 数据存储之 SQLite嵌入式数据库
2010-03-26 15:43:00 来源:WEB开发网.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操作总结结束!
更多精彩
赞助商链接