0
我尝试做一个应用程序,其中我创建一个列表视图与两个textview和一个复选框字段,我想当我点击复选框检查然后两个textview值都添加到数据库和时我点击相同的复选框来取消选中复选框,然后再添加从数据库中删除的值。Android逻辑需要工作
我能够编码在检查事件上添加数据库上的数据值,但不能删除这些值取消复选框。
package data.base;
import com.example.phone_no.DBhelper;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
public class Second extends ArrayAdapter<String> {
String string1[];
Context context;
String string[];
TextView textView;
TextView textView1,textView2;
CheckBox checkBox;
EditText editText;
String string2[];
public Second(Context context, String[] objects, String[] Object1,String[] object2)
{
super(context,R.layout.main,R.id.text1, objects);
System.out.println("you in under super");
this.context=context;
this.string=objects;
this.string1=Object1;
this.string2 = object2;
// this.imageView = Object3;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
System.out.println("you in under getview");
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.main,parent,false);
textView=(TextView)view.findViewById(R.id.text1);
System.out.println("you in under inflator");
textView1=(TextView)view.findViewById(R.id.text2);
// imageView = (ImageView)view.findViewById(R.id.text3);
textView.setText(string[position]);
textView1.setText(string1[position]);
System.out.println("you are above of return");
checkBox = (CheckBox)view.findViewById(R.id.check);
textView2 = (TextView)view.findViewById(R.id.text3);
textView2.setText(string2[position]);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(checkBox.isChecked())
{
System.out.println("under check");
DBhelper DB = new DBhelper(context);
DB.open();
DB.delete_image(string2[position]);
DB.close();
}
else
{
System.out.println("underhhhhhhhhhhhhhhhhhhhhhhh check");
DBhelper DB = new DBhelper(context);
DB.open();
DB.adddata(string2[position], string[position], string1[position]);
DB.getAlldata();
DB.close();
}
}
});
return view; //To change body of overridden methods use File | Settings | File Templates.
}
}
这是我DBhelper类维护数据库的操作:
package com.example.phone_no;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBhelper {
public static final String KEY_ID = "id";
public static final String KEY_NAME = "names";
public static final String KEY_PHONE = "phoneno";
private final Context ourContext;
private static final String DATABASE_TABLE = "Contactinfo";
private static final int DATABASE_VERSION = 27;
private static final String DATABASE_NAME = "contactdata.db";
private DbHelper ourHelper;
private SQLiteDatabase ourDatabase;
// end for location
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
/*db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
+ " INTEGER PRIMARY KEY, " + KEY_NAME + " TEXT NULL , "
+ KEY_PHONE + " INTEGER NULL);");*/
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
+ " INTEGER , " + KEY_NAME + " TEXT NULL , "
+ KEY_PHONE + " INTEGER NULL);");
// string value
String y = "CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
+ " INTEGER PRIMARY KEY , " + KEY_NAME + " TEXT NULL , "
+ KEY_PHONE + " INTEGER NULL);";
System.out.println("query" + y);
Log.d("query", y);
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public DBhelper(Context c) {
ourContext = c;
}
public DBhelper open() throws SQLException
{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close()
{
ourHelper.close();
}
public long adddata(String id,String name,String number)
{
// TODO Auto-generated method stub
// add the custom Image Gallery Image Path to Data Base
ContentValues cv = new ContentValues();
cv.put(KEY_ID, id);
cv.put(KEY_NAME, name);
cv.put(KEY_PHONE, number);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public void getAlldata()
{
Cursor details = null;
if (ourDatabase.isOpen() == false)
ourDatabase = ourHelper.getWritableDatabase();
if (ourDatabase.isOpen())
{
details = ourDatabase.query(DATABASE_TABLE, null, null, null, null, null, null);
for(details.moveToFirst();!details.isAfterLast();details.moveToNext())
{
String a=details.getString(0);
String b=details.getString(1);
String c=details.getString(2);
System.out.println("id--"+a+"name"+b+"phoneno"+c);
}
}
}
public long delete_image(String id)
{
if (ourDatabase.isOpen() == false)
ourDatabase = ourHelper.getWritableDatabase();
if (ourDatabase.isOpen())
{
return ourDatabase.delete(DATABASE_TABLE, KEY_ID + "=" + id, null);
}
return 0;
}
}
这是我第二类文件名Second.java中,我不能下,如果条件创建逻辑。
package data.base;
import com.example.phone_no.DBhelper;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
public class Second extends ArrayAdapter<String> {
String string1[];
Context context;
String string[];
TextView textView;
TextView textView1,textView2;
CheckBox checkBox;
EditText editText;
String string2[];
public Second(Context context, String[] objects, String[] Object1,String[] object2)
{
super(context,R.layout.main,R.id.text1, objects);
System.out.println("you in under super");
this.context=context;
this.string=objects;
this.string1=Object1;
this.string2 = object2;
// this.imageView = Object3;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
System.out.println("you in under getview");
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.main,parent,false);
textView=(TextView)view.findViewById(R.id.text1);
System.out.println("you in under inflator");
textView1=(TextView)view.findViewById(R.id.text2);
// imageView = (ImageView)view.findViewById(R.id.text3);
textView.setText(string[position]);
textView1.setText(string1[position]);
System.out.println("you are above of return");
checkBox = (CheckBox)view.findViewById(R.id.check);
textView2 = (TextView)view.findViewById(R.id.text3);
textView2.setText(string2[position]);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(checkBox.isChecked())
{
System.out.println("under check");
DBhelper DB = new DBhelper(context);
DB.open();
DB.delete_image(string2[position]);
DB.close();
}
else
{
System.out.println("underhhhhhhhhhhhhhhhhhhhhhhh check");
DBhelper DB = new DBhelper(context);
DB.open();
DB.adddata(string2[position], string[position], string1[position]);
DB.getAlldata();
DB.close();
}
}
});
return view; //To change body of overridden methods use File | Settings | File Templates.
}
}
在如果条件岂不下,我使用的删除方法,这就是为什么它总是在数据基础上,检查和复选框取消选中的事件添加记录首节去。
请大家帮忙。 thanx提前
我的问题是在这里,我的代码被删除记录的基础上的记录id这是在string2 [位置],然后当我第一次运行代码,然后复选框已经取消选中,并根据您的代码时复选框是未选中然后它删除记录.......但记录不存在数据库bcoz它在检查事件创建.....所以模拟器给出了一个错误,你没有记录哪个ID是A是第一个fatch in string2 [position] ......那是我的问题 – dev 2013-03-12 20:22:13
在'delete_image()'编辑返回这个'return ourDatabase.delete(DATABASE_TABLE,KEY_ID +“=”+ id,null)> 0;'。 – AwadKab 2013-03-12 20:28:27
由于您在表中创建了'id'整数,因此您将'id'作为字符串传递,因此您应该将其转换为整数。 – AwadKab 2013-03-12 20:33:59