【问题标题】:Android Logic needs workAndroid Logic 需要工作
【发布时间】:2013-03-12 19:39:49
【问题描述】:

我正在尝试创建一个应用程序,在该应用程序中创建一个包含两个 textview 和一个复选框字段的列表视图,我希望当我单击复选框进行检查时,两个 textview 值都添加到数据库中,当我单击相同时复选框以取消选中复选框,然后之前都添加从数据库中删除的值。

我能够编写代码以在选中事件时在数据库中添加数据值,但在取消选中复选框时无法删除这些值。

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,在 if 条件下我无法在其中创建逻辑。

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.

    }


}

在 if 条件下,它不会进入我使用删除方法的第一部分,这就是为什么它总是根据复选框的选中和取消选中事件在数据库中添加记录。

请帮忙。提前谢谢

【问题讨论】:

    标签: android database android-emulator logic


    【解决方案1】:

    尝试编辑这个

    checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub
    
    
                if(!arg1)   
               {
    
                 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();
    
               }
    
            }
        });
    

    然后编辑这个

    public boolean delete_image(int id) 
    
    {
        if (ourDatabase.isOpen() == false)
            ourDatabase = ourHelper.getWritableDatabase();
          if (ourDatabase.isOpen()) 
            {
                return ourDatabase.delete(DATABASE_TABLE, KEY_ID + "=" + id, null) > 0;
            }
            return false;
        }
    

    希望这对您有所帮助。

    【讨论】:

    • 我的问题是我的代码是根据来自 string2[position] 的 Record id 删除记录,然后当我第一次运行代码时,复选框已经被取消选中并根据当复选框未选中时,您的代码将删除记录.......但是数据库中不存在记录,因为它是在检查事件中创建的......所以模拟器给出了一个错误,您没有记录哪个 id是 A,它是 string2[position] 中的第一个 fatch ......这就是我的问题
    • delete_image() 编辑返回到这个return ourDatabase.delete(DATABASE_TABLE, KEY_ID + "=" + id, null) &gt; 0;
    • 您将id 作为字符串传递,您应该将其转换为整数,因为您在表中创建了id 整数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 2017-11-03
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    相关资源
    最近更新 更多