Android Logic需要工作

时间:2013-03-12 19:39:50

标签: android database android-emulator logic

我尝试创建一个应用程序,在其中我创建一个列表视图,其中包含两个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条件下,它不会进入我使用删除方法的第一部分,这就是为什么它总是在检查时取消选中数据库中的记录并取消选中复选框的事件。

请帮忙。提前谢谢

1 个答案:

答案 0 :(得分:0)

尝试编辑此

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;
    }

希望这有助于你。