2013-05-26 99 views
0

我是Android新手。你能幫我找到如何刪除SQLite中的數據列表的項目?我在onListItemClick的代碼setPositiveButton中有此代碼。請刪除這個項目的代碼是什麼?我有:刪除SQLite列表中的項目

package tab.example.menutab; 

import java.util.ArrayList; 
import java.util.List; 

import android.app.AlertDialog; 
import android.app.ListActivity; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

public class ListedesClient extends ListActivity { 
    TextView selectionc; 
    public int idToModify; 
    ClientDataAdapter dm; 

    List<String[]> list = new ArrayList<String[]>(); 
    List<String[]> names2 =null ; 
    String[] stg1; 
    protected void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.listeclients); 
      dm = new ClientDataAdapter(this); 
      names2 = dm.selectAll(); 

     stg1=new String[names2.size()]; 

     int x=0; 
     String stg; 

     for (String[] name : names2) { 
      stg = name[1]+" - "+name[2]+ " - "+name[3]; 

      stg1[x]=stg; 
      x++; 
     } 


     ArrayAdapter<String> adapter = new ArrayAdapter<String>( 
       this,android.R.layout.simple_list_item_1, 
       stg1); 
     this.setListAdapter(adapter); 
     selectionc=(TextView)findViewById(R.id.selectionc); 

    }  

    public void onListItemClick(ListView parent, View v, final int position, long id) { 

     selectionc.setText("Vous êtes sur : " + stg1[position]); 
     AlertDialog.Builder builder = new AlertDialog.Builder(this) 
     .setTitle("Delete Item") 
     .setMessage("are you sure to delete?") 
     .setPositiveButton("Yes", 
       new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface arg0, int arg1) { 
         // TODO Auto-generated method stub 
        ClientDataAdapter nota = (ClientDataAdapter) adapterView 
           .getItemAtPosition(position); 
         ClientDataAdapter.delete(stg1[position]); 

         // Refrescamos la lista refrech i gess but havent the methode 
         refrescarLista(); 


        } 
      }) 

     .setNegativeButton("No", 
       new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, 
          int which) { 
         // TODO Auto-generated method stub 
         return; 
        } 
      }); 
    builder.show(); 
    } 

} 

併爲數據庫,我有:

package tab.example.menutab; 

import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.database.sqlite.SQLiteStatement; 
import java.util.ArrayList; 
import java.util.List; 

public class ClientDataAdapter { 
    private static final String DATABASE_NAME = "clientbase.db"; 
    private static final int DATABASE_VERSION = 1; 
    static final String TABLE_CLIENT = "clienttable"; 
    private static Context context; 
    static SQLiteDatabase dc; 
private SQLiteStatement insertStmt; 

    private static final String INSERT = "insert into " 
     + TABLE_CLIENT + " (nom_complet,adresse_client,numero_telephone) values (?,?,?)"; 

    public ClientDataAdapter(Context context) { 
     ClientDataAdapter.context = context; 
     OpenHelper openHelper = new OpenHelper(ClientDataAdapter.context); 
     ClientDataAdapter.dc = openHelper.getWritableDatabase(); 
     this.insertStmt = ClientDataAdapter.dc.compileStatement(INSERT); 
     } 

    public long insert(String nom_complet,String adresse_client,String numero_telephone) { 
     this.insertStmt.bindString(1, nom_complet); 
     this.insertStmt.bindString(2, adresse_client); 
     this.insertStmt.bindString(3, numero_telephone); 
     return this.insertStmt.executeInsert(); 
    } 

    public void deleteAll() { 
     dc.delete(TABLE_CLIENT, null, null); 
    } 

    public List<String[]> selectAll() 
    { 

     List<String[]> list = new ArrayList<String[]>(); 
     Cursor cursor = dc.query(TABLE_CLIENT, new String[] { "id","nom_complet","adresse_client","numero_telephone"}, 
       null, null, null, null, "nom_complet asc"); 

     int x=0; 
     if (cursor.moveToFirst()) { 
      do { 
       String[] b1=new String[]{cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3)}; 

       list.add(b1); 

       x=x+1; 
      } while (cursor.moveToNext()); 
     } 
     if (cursor != null && !cursor.isClosed()) { 
      cursor.close(); 
     } 
     cursor.close(); 

     return list; 
    } 


    public void delete(int rowId) { 
     dc.delete(TABLE_CLIENT, null, null); 
    } 

    private static class OpenHelper extends SQLiteOpenHelper { 

     OpenHelper(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase dc) { 
      dc.execSQL("CREATE TABLE " + TABLE_CLIENT + " (id INTEGER PRIMARY KEY, nom_complet TEXT NOT NULL, adresse_client TEXT NOT NULL, numero_telephone TEXT NOT NULL)"); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase dc, int oldVersion, int newVersion) { 
      dc.execSQL("DROP TABLE IF EXISTS " + TABLE_CLIENT); 
      onCreate(dc); 
     } 
    } 
} 

回答

0

這個網站應該幫助你在Android的數據庫處理的大多數問題。 http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

如果我理解正確;

public void delete(int rowId) { 
    dc.delete(TABLE_CLIENT, id + " = ?", new String[] { String.valueOf(cursor.getString(0)) }); 
} 

我不知道爲什麼需要rowId。

+0

列表的形式和方法的用法是注意對我來說相同的數據庫如何在我的情況下它是一個cople的行插入我不知道如何在Android新的 –

+0

請看我的編輯,我相信它接近你想要的。 – Waddas