2013-03-12 98 views
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提前

回答

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

希望這有助於你。

+0

我的問題是在這裏,我的代碼被刪除記錄的基礎上的記錄id這是在string2 [位置],然後當我第一次運行代碼,然後複選框已經取消選中,並根據您的代碼時複選框是未選中然後它刪除記錄.......但記錄不存在數據庫bcoz它在檢查事件創建.....所以模擬器給出了一個錯誤,你沒有記錄哪個ID是A是第一個fatch in string2 [position] ......那是我的問題 – dev 2013-03-12 20:22:13

+0

在'delete_image()'編輯返回這個'return ourDatabase.delete(DATABASE_TABLE,KEY_ID +「=」+ id,null)> 0;'。 – AwadKab 2013-03-12 20:28:27

+0

由於您在表中創建了'id'整數,因此您將'id'作爲字符串傳遞,因此您應該將其轉換爲整數。 – AwadKab 2013-03-12 20:33:59