我制作了一个简单的 android Databse 演示程序用于学习目的我已经引用了如下链接: 在此处输入链接描述
我已经一步一步完成了,但是我的程序中没有创建数据库,我的代码如下:
联系人列表适配器.java
package com.example.dblistview;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class ContactListAdapter extends BaseAdapter {
Context context;
ArrayList<ContactListItems> contactList;
public ContactListAdapter(Context context, ArrayList<ContactListItems> list) {
this.context = context;
contactList = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return contactList.size();
}
@Override
public Object getItem(int pos) {
// TODO Auto-generated method stub
return contactList.get(pos);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ContactListItems contactListItems = contactList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.raw, null);
}
TextView tvSlNo = (TextView) convertView.findViewById(R.id.tv_slno);
tvSlNo.setText(contactListItems.getSlno());
TextView tvName = (TextView) convertView.findViewById(R.id.tv_name);
tvName.setText(contactListItems.getName());
TextView tvPhone = (TextView) convertView.findViewById(R.id.tv_phone);
tvPhone.setText(contactListItems.getPhone());
return convertView;
}
}
联系人列表项.java
package com.example.dblistview;
public class ContactListItems {
String slno;
String name;
String phone;
public String getSlno() {
return slno;
}
public void setSlno(String slno) {
this.slno = slno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
main.java
package com.example.dblistview;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
sqlHandler sqlHandler;
ListView lv;
Button submit;
EditText nm,fn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.lv_custom_list);
nm=(EditText)findViewById(R.id.et_name);
fn=(EditText)findViewById(R.id.et_phone);
submit=(Button)findViewById(R.id.btn_submit);
sqlHandler =new sqlHandler();
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name=nm.getText().toString();
String fon=fn.getText().toString();
String query = "INSERT INTO PHONE_CONTACTS(name,phone) values ('"
+ name + "','" + fon + "')";
sqlHandler.executeQuery(query);
}
});
}
private void showList() {
ArrayList<ContactListItems> contactList = new ArrayList<ContactListItems>();
contactList.clear();
String query = "SELECT * FROM PHONE_CONTACTS ";
Cursor c1 = sqlHandler.selectQuery(query);
if (c1 != null && c1.getCount() != 0) {
if (c1.moveToFirst()) {
do {
ContactListItems contactListItems = new ContactListItems();
contactListItems.setSlno(c1.getString(c1
.getColumnIndex("slno")));
contactListItems.setName(c1.getString(c1
.getColumnIndex("name")));
contactListItems.setPhone(c1.getString(c1
.getColumnIndex("phone")));
contactList.add(contactListItems);
} while (c1.moveToNext());
}
}
c1.close();
ContactListAdapter contactListAdapter = new ContactListAdapter(
MainActivity.this, contactList);
lv.setAdapter(contactListAdapter);
}
}
sqlDbhelper.java
package com.example.dblistview;
import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class SqlDbhelper extends SQLiteOpenHelper {
public static final String DATABASE_TABLE = "PHONE_CONTACTS";
public static final String COLUMN1 = "slno";
public static final String COLUMN2 = "name";
public static final String COLUMN3 = "phone";
private static final String SCRIPT_CREATE_DATABASE = "create table "
+ DATABASE_TABLE + " (" + COLUMN1
+ " integer primary key autoincrement, " + COLUMN2
+ " text not null, " + COLUMN3 + " text not null);";
public SqlDbhelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SCRIPT_CREATE_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists" + DATABASE_TABLE);
onCreate(db);
}
}
sqlhandler.java
package com.example.dblistview;
import com.example.dblistview.SqlDbhelper;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class sqlHandler {public static final String DATABASE_NAME = "MY_DATABASE";
public static final int DATABASE_VERSION = 1;
Context context;
SQLiteDatabase sqlDatabase;
SqlDbhelper dbHelper;
public void SqlHandler(Context context) {
dbHelper = new SqlDbhelper(context, DATABASE_NAME, null,
DATABASE_VERSION);
sqlDatabase = dbHelper.getWritableDatabase();
}
public void executeQuery(String query) {
try {
if (sqlDatabase.isOpen()) {
sqlDatabase.close();
}
sqlDatabase = dbHelper.getWritableDatabase();
sqlDatabase.execSQL(query);
} catch (Exception e) {
System.out.println("DATABASE ERROR " + e);
}
}
public Cursor selectQuery(String query) {
Cursor c1 = null;
try {
if (sqlDatabase.isOpen()) {
sqlDatabase.close();
}
sqlDatabase = dbHelper.getWritableDatabase();
c1 = sqlDatabase.rawQuery(query, null);
} catch (Exception e) {
System.out.println("DATABASE ERROR " + e);
}
return c1;
}
}