功能:
1)数据库的增删改查
2)可以显示在listView_item上
文件介绍:
// SampleActivity.java 增删改查操作,对应activity_mian布局文件
// activity_mian.xml 布局文件
代码:
// SampleActivity.java
package com.yline.sqlite.Sample;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import com.yline.sqlite.R;
import com.yline.sqlite.database.DataBaseOperateNoTool;
import com.yline.sqlite.database.PersonDomain;
import com.yline.sqlite.database.SQLiteOpenHelperYline;
public class SampleACtivity extends Activity implements OnClickListener{
private Button btn_add;
private Button btn_delete;
private Button btn_find;
private Button btn_update;
private EditText et_name;
private EditText et_number;
private String name;
private String number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CreateDB();
initView();
}
private void initView(){
btn_add = (Button) findViewById(R.id.btn_add);
btn_delete = (Button) findViewById(R.id.btn_delete);
btn_find = (Button) findViewById(R.id.btn_find);
btn_update = (Button) findViewById(R.id.btn_update);
et_name = (EditText) findViewById(R.id.et_name);
et_number = (EditText) findViewById(R.id.et_number);
btn_add.setOnClickListener(this);
btn_delete.setOnClickListener(this);
btn_find.setOnClickListener(this);
btn_update.setOnClickListener(this);
}
@Override
public void onClick(View v) {
name = et_name.getText().toString().trim();
number = et_number.getText().toString().trim();
switch (v.getId()) {
case R.id.btn_add:
add(name,number);
break;
case R.id.btn_delete:
delete(name);
break;
case R.id.btn_update:
update(1,name,number);
break;
case R.id.btn_find:
find(3);
break;
}
show();
}
/**
* 创建一个数据库
*/
private void CreateDB(){
SQLiteOpenHelperYline helper = new SQLiteOpenHelperYline(this);
helper.getWritableDatabase();
}
/**
* 在表中,添加 一条信息
* @param name 姓名
* @param number 号码
*/
private void add(String name,String number){
DataBaseOperateNoTool ylineTable = new DataBaseOperateNoTool(this);
ylineTable.addDBItem(name,number);
}
private void show(){
ListView lv_show = (ListView) findViewById(R.id.lv_show);
DataBaseOperateNoTool ylineTable = new DataBaseOperateNoTool(this);
List<PersonDomain> persons = ylineTable.findAll();
List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();
for(PersonDomain m : persons){
HashMap<String, Object> item = new HashMap<String, Object>();
item.put("id", m.getId()+" ");
item.put("name", m.getName()+" ");
item.put("number", m.getNumber()+" ");
data.add(item);
}
String[] from = new String[]{"id","name","number"};
int[] to = new int[]{R.id.tv_id,R.id.tv_name,R.id.tv_number};
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.listview_item, from, to);
lv_show.setAdapter(adapter);
}
/**
* 删除 姓名为name的 信息
* @param name
*/
private void delete(String name){
DataBaseOperateNoTool ylineTable = new DataBaseOperateNoTool(this);
ylineTable.deleteDBItem(name);
}
/**
* 更新数据
* @param id 待更新的编号
* @param name 新的姓名
* @param number 新的号码
*/
private void update(int id,String name,String number){
DataBaseOperateNoTool ylineTable = new DataBaseOperateNoTool(this);
ylineTable.updateDBItem(id,name,number);
}
/**
* 查询信息
* @param id 待查询信息的编号
*/
private void find(int id){
DataBaseOperateNoTool ylineTable = new DataBaseOperateNoTool(this);
ylineTable.findDBItem(id);
}
/**
* 遍历列表
*/
private void findAll(){
DataBaseOperateNoTool ylineTable = new DataBaseOperateNoTool(this);
List<PersonDomain> persons = ylineTable.findAll();
for(PersonDomain m : persons){
System.out.println(m);
}
}
}
// activity_mian.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="@+id/et_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:hint="id"
android:singleLine="true" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/et_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/et_name"
android:hint="number"
android:singleLine="true" />
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/et_number"
android:text="add" />
<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_add"
android:layout_alignBottom="@+id/btn_add"
android:layout_toRightOf="@+id/btn_add"
android:text="delete" />
<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_delete"
android:layout_alignBottom="@+id/btn_delete"
android:layout_toRightOf="@+id/btn_delete"
android:text="updata" />
<Button
android:id="@+id/btn_find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_update"
android:layout_alignBottom="@+id/btn_update"
android:layout_toRightOf="@+id/btn_update"
android:text="find" />
<ListView
android:id="@+id/lv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btn_add" >
</ListView>
</RelativeLayout>
运行图:
可运行代码下载链接:
http://pan.baidu.com/s/1i3kSJqP