删除SQLite中列表中的项目

时间:2013-05-26 23:35:43

标签: android

我是Android新手。你能帮我找到如何删除SQLite中的数据列表的项目吗?我在onListItemClick的{​​{1}}中有此代码。请删除此项目的代码是什么?我有:

setPositiveButton

对于我拥有的数据库:

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

}

1 个答案:

答案 0 :(得分: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。