由于刚接触android开发,故此想把学到的基础知识记录一下,以备查询,故此写的比较啰嗦:
步骤如下:
一、介绍:
此文主要是介绍怎么使用android自带的数据库SQLite,以及把后台的数据用ListView控件显示
二、新建一个android工程——DBSQLiteOperate
工程目录:
三、清单列表AndroidManifest.xml的配置为:
< ?xml version="1.0" encoding="utf-8"?>
< manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dboperate"
android:versionCode="1"
android:versionName="1.0" >
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
android:name=".DBSQLiteOperateActivity"
android:label="@string/app_name" >
android:targetPackage="com.example.dboperate"
android:label="Test for my app"/>
< /manifest>
四、main.xml配置清单:
< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:layout_height="wrap_content"
android:orientation="horizontal" >
android:id="@+id/name"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="@string/name"
android:gravity="center"/>
android:id="@+id/phone"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="@string/phone"
android:gravity="center"/>
android:id="@+id/amount"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/amount"
android:gravity="center"/>
< /LinearLayout>
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
< /LinearLayout>
五、item.xml配置清单:
< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
android:id="@+id/name"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="@string/name"
android:gravity="center"/>
android:id="@+id/phone"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="@string/phone"
android:gravity="center"/>
android:id="@+id/amount"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/amount"
android:gravity="center"/>
< /LinearLayout>
六、string.xml配置清单:
< ?xml version="1.0" encoding="utf-8"?>
< resources>
Hello World, DBSQLiteOperateActivity!
ExampleDBSQLiteOperate8
姓名
电话
存款
< /resources>
七、DBSQLiteOperateActivity.java Activity类的源码:
package com.example.dboperate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.adapter.PersonAdapter;
import com.example.domain.Person;
import com.example.service.PersonService;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class DBSQLiteOperateActivity extends Activity {
ListView listView;
PersonService personService;
OnItemClickListener listViewListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listViewListener = new OnItemClickListener(){
@Override
public void onItemClick(AdapterView