Android SimpleCursorAdapter 绑定数据的陷阱抢沙发

2013-12-04    分类:Android开发原创文章编程相关      

研究了一个通宵的代码,研究完后发现自己特么是一只逗比,先把图片和代码贴出来!
值得一逗的是:
1、SQLite在安卓API中(推荐)使用_id作为主键,务必小写,一定要下划线。这是安卓对程序员的强迫还是推荐?不使用_id的话程序运行直接报错,我个人感觉这是挺坑爹的作法。也就是说select name from Person 这种做法是万万不可行的,只能select * from Person或者Select _id,xx,xx,xx from Person,无论如何都得把_id给包含进去返回给Cursor指针。
2、 SimpleCursorAdapter中,Cursor这个指针一定不能关闭。万万不得,昨天看API发现Cursor可以关闭闲的蛋疼居然在 upperspinner方法的结尾把指针给关闭了,结果数据无论怎么绑定也绑定不了,读还是可以读的。后来灵机一动把close方法屏蔽掉就可以绑定数据了。
3、Android中有非常多坑爹学习的陷阱值得我们去发掘。
不知是我API没看懂还是什么原因,总之很多蛋碎的结果让我总是很意外,每次的意外都得用一个通宵来解决,很多东西百度是百度不出来的,就像SimpleCursorAdapter 中的其中一个参数android.R.id.text1到现在我还没弄明白,这个既不是textview的句柄,也不是我自己定义的,妈蛋是从哪来的?百度无果,我个人只好主观的认为是Spinner的句柄了。望大神奔走相告~

image

 

image

 

 

//MainAcitity.java
package com.example.android_2013_11_23_dbtest;
import java.io.File;
import javax.security.auth.PrivateCredentialPermission;
import com.example.android_2013_11_23_dbtest.db.DbHelper;
import android.os.Bundle;
import android.R.integer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private static Button b1,b2,b3,b4;
SQLiteDatabase db;
private DbHelper hepler = new DbHelper(this);
private static final String table_name = “Person”;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去除窗口标题
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.b1);
b2 = (Button)findViewById(R.id.b2);
b3 = (Button)findViewById(R.id.b3);
b4 = (Button)findViewById(R.id.b4);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
//启动数据库
db = hepler.getWritableDatabase();
//初始化数据库
initDatabase(db);
//更新下拉列表
upperspinner();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
ContentValues cv = new ContentValues();
switch (v.getId()){
case R.id.b1:
cv.put(“name”, “哈哈”);
cv.put(“age”, 30);
cv.put(“address”, “广东从化”);
long l = db.insert(table_name, “”, cv);
if( l != -1){
Toast.makeText(getApplicationContext(), “插入数据成功,ID是”+l, 0).show();
}else{
Toast.makeText(getApplicationContext(), “插入数据失败”, 0).show();
}
//upperspinner();
break;
case R.id.b2:
int i = db.delete(table_name, “name=’哈哈’”, null);
if( i != 0){
Toast.makeText(getApplicationContext(), “删除成功,删除了”+i+”条记录”, 0).show();
}else{
Toast.makeText(getApplicationContext(), “删除失败,删除了0条”, 0).show();
}
//upperspinner();
break;
case R.id.b3:
cv.put(“name”, “哈哈改装版”);
cv.put(“age”, 55);
cv.put(“address”, “湖南株洲”);
int d = db.update(“Person”, cv, “name=’哈哈’”, null);
if( d != 0 ){
Toast.makeText(getApplicationContext(), “更新了条”+d+”数据”, 0).show();
}else{
Toast.makeText(getApplicationContext(), “数据没有更新”, 0).show();
}
//upperspinner();
break;
case R.id.b4:
Cursor c = db.query(table_name, null, null, null, null, null, null);
int len = c.getCount();//返回行数
while(c.moveToNext()){
Log.v(“查询query结果”, “ID:”+c.getString(0)+”姓名:”+c.getString(1)+”年龄:”+c.getString(2)+”地址:”+c.getString(3));
}
/*
for(int k = 0;k<len;k++){
c.moveToPosition(k);
}
*/
Toast.makeText(getApplicationContext(), “共有记录”+len+”条”, 0).show();
c.close();
break;
}
}
//初始化数据库
public void initDatabase(SQLiteDatabase db){
//新建cv存储对象
ContentValues cv = new ContentValues();
cv.put(“name”, “张三”);
cv.put(“age”, 20);
cv.put(“address”, “广东广州”);
db.insert(table_name    , “”, cv);
cv.put(“name”, “李四”);
cv.put(“age”, 23);
cv.put(“address”, “广东梅州”);
db.insert(table_name    , “”, cv);
cv.put(“name”, “王五”);
cv.put(“age”, 26);
cv.put(“address”, “广东深圳”);
db.insert(table_name    , “”, cv);
cv.put(“name”, “赵六”);
cv.put(“age”, 27);
cv.put(“address”, “广东清远”);
db.insert(table_name    , “”, cv);
cv.put(“name”, “陈七”);
cv.put(“age”, 29);
cv.put(“address”, “广东湛江”);
db.insert(table_name    , “”, cv);
Log.v(“SQLite Test”, “initDatabase调用”);
}
//更新下拉列表
@SuppressLint(“NewApi”)
@SuppressWarnings(“deprecation”)
public void upperspinner(){
final TextView tv = (TextView)findViewById(R.id.TextView02);
Spinner spinner = (Spinner)findViewById(R.id.spinner);
//声明指针对象
final Cursor cursor = db.query(table_name, null, null, null, null, null, null);
Log.v(“SQLite Test”, “SimpleCursorAdapter    “+(cursor.getColumnIndex(“age”)) );
Log.v(“SQLite Test”, “SimpleCursorAdapter    “+cursor.getColumnName(cursor.getColumnIndex(“name”))  );
//声明一个简单的指针适配器
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
getApplicationContext(),
android.R.layout.simple_spinner_item,
cursor,
new String[]{(cursor.getColumnName(cursor.getColumnIndex(“name”)))},
new int[] {android.R.id.text1,android.R.id.text2});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
OnItemSelectedListener oisl = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
cursor.moveToPosition(position);
Toast.makeText(getApplicationContext(), “ID:”+cursor.getShort(0)+”  姓名:”+cursor.getString(1)+
“  年龄:”+cursor.getString(2)+”  地址:”+cursor.getString(3), 0).show();
tv.setText(“ID:”+cursor.getShort(0)+”  姓名:”+cursor.getString(1)+
“  年龄:”+cursor.getString(2)+”  地址:”+cursor.getString(3));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO 自动生成的方法存根
}
};
//设置spinner下拉选中项
spinner.setOnItemSelectedListener(oisl);
/*
while(cursor.moveToNext()){
Log.v(“查询query结果”, “ID:”+cursor.getString(0)+”姓名:”+cursor.getString(1)+”年龄:”+cursor.getString(2)+”地址:”+cursor.getString(3));
}*/
//cursor.close();
}
public void onDestroy(){
super.onDestroy();
Log.i(“SQLite前端”, “onDestroy调用”);
db.delete(table_name, “”, null);
db.close();
//db.deleteDatabase(new File(“databases/mydb.db”));
}
}
//DbHelper辅助类文件
package com.example.android_2013_11_23_dbtest.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DbHelper extends SQLiteOpenHelper {
private static int version = 2;//数据库版本号
private static String name = “mydb.db”;//数据库文件名称
public DbHelper(Context context) {
super(context, name, null, version);
Log.i(“SQLite Test”,”构造方法调用”);
// TODO 自动生成的构造函数存根
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO 自动生成的方法存根
Log.i(“SQLite Test”,”OnCreate调用”);
String sql = “create table Person (_id INTEGER PRIMARY KEY  AUTOINCREMENT NOT NULL,  name VARCHAR , age INTEGER ,address VARCHAR)”;
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO 自动生成的方法存根
Log.i(“SQLite Test”,”OnUpgrade调用”);
String sql = “alter table Person add sex varchar(8)”;
arg0.execSQL(sql);
}
}

转载请注明:乔布尸 » Android SimpleCursorAdapter 绑定数据的陷阱

继续查看有关 的文章

相关文章

我来说说

*

*

取消