Android对SQLite对ListView的操作
对一DatabaseHelper类的写法还是上一随笔的,这里加了一个Person和PersonService类;
Person用于对数据的强类型定义:
public class Person {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
Person(String name,String age)
{
this.name=name;
this.age=age;
}
Person()
{
}
}
PersonService类是对数据的增删改查操作,这里只写了两个方法一个是insert一个是select:
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class PersonService {
private DatabaseHelper dataHelper;
PersonService(Context context)
{
dataHelper=new DatabaseHelper(context);
}
public void insertPerson(Person person)
{
SQLiteDatabase sqlite=dataHelper.getWritableDatabase();
sqlite.execSQL("insert into person (name,age) values (?,?)", new String[]{person.getName(),person.getAge()});
}
public List<Person> select()
{
List<Person> persons=new ArrayList<Person>();
SQLiteDatabase sqlite=dataHelper.getWritableDatabase();
Cursor cursor=sqlite.query("person", new String[]{"name","age"},null, null, null, null, null);
while(cursor.moveToNext()){
Person person=new Person();
person.setName(cursor.getString(0));
person.setAge(cursor.getString(1));
persons.add(person);
}
cursor.close();
return persons;
}
}
在这里我建立了三个画布:
activity_main.xml(主画布,用于插入)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="43dp"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginLeft="63dp"
android:layout_marginTop="23dp"
android:text="插入" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="38dp"
android:text="查看数据" />
</RelativeLayout>
mune.xml(用于显示ListView)
<?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">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="姓名"
/>
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="年龄"
/>
</LinearLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
/>
</LinearLayout>
listfrom.xml(定义ListView显示的格式)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/testname"
/>
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/testage"
/>
</LinearLayout>
下面是写方法了:
MainActivity.cs
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.nfc.Tag;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends Activity {
PersonService personService=new PersonService(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText txt1=(EditText)findViewById(R.id.editText1);
final EditText txt2=(EditText)findViewById(R.id.editText2);
Button btn1=(Button)findViewById(R.id.button1);
Button btn2=(Button)findViewById(R.id.button2);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
next();
}
});
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
String name=txt1.getText().toString();
String age=txt2.getText().toString();
Person person=new Person(name,age);
try{
personService.insertPerson(person);
Log.i("LOG", "insert into person (name,age) values ("+name+","+age+")");
setTitle("插入成功!");
}
catch(Exception e)
{
Log.i("LOG",e.getMessage()+"|||||"+e.getLocalizedMessage());
setTitle("插入失败!");
}
}
});
/*
*/
}
public void next()
{
setContentView(R.layout.mune);
ListView list=(ListView)findViewById(R.id.listView);
List<HashMap<String,String>> data=new ArrayList<HashMap<String,String>>();
List<Person> persons=personService.select();
for(Person person:persons)
{
HashMap<String,String> map=new HashMap<String,String>();
map.put("name", person.getName());
map.put("age", person.getAge());
data.add(map);
}
SimpleAdapter adapter=new SimpleAdapter(MainActivity.this,data,R.layout.listform,new String[]{"name","age"},new int[]{R.id.testname,R.id.testage});//这这些参数中要注意R.layout.listform这个参数,定义为格式的那个画布
list.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}