在Sqlite android中删除数据库中的数据

时间:2014-03-25 05:35:25

标签: java android android-sqlite

这是我在SqliteDatabase类中的删除功能

public void deleteContact(Employee contact) {  
            SQLiteDatabase db = this.getWritableDatabase();  
            db.delete(TABLE_EMployee, KEY_ID + " = ?",  
                    new String[] { String.valueOf(contact.get_id()) });  
            db.close();  
        }  

员工类:

public class Employee {

    private int _id;
    private String _name;
    private String _email;

    public Employee() {
        // TODO Auto-generated constructor stub
    }

    public Employee(int id, String name, String email) {
        this._id = id;
        this._name = name;
        this._email = email;

    }

    public Employee(String name, String email) {
        this._name = name;
        this._email = email;

    }

    public int get_id() {
        return _id;
    }

    public void set_id(int _id) {
        this._id = _id;
    }

    public String get_name() {
        return _name;
    }

    public void set_name(String _name) {
        this._name = _name;
    }

    public String get_email() {
        return _email;
    }

    public void set_email(String _email) {
        this._email = _email;
    }

}
  MainActivity Class:




public class MainActivity extends Activity {
    MySqliteDatabase db = new MySqliteDatabase(this);
    EditText nametext;
    EditText emailtext;
    Button btn;
    public List<Employee> list1 = new ArrayList<Employee>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nametext = (EditText) findViewById(R.id.editText1);
        emailtext = (EditText) findViewById(R.id.editText2);
        btn = (Button) findViewById(R.id.button1);
        ListView list = (ListView) findViewById(R.id.listView1);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                String name = nametext.getText().toString();
                String email = emailtext.getText().toString();

                db.addEmployee(new Employee(name, email));

                Toast.makeText(getApplicationContext(), "SucessfullyInseter",
                        1000).show();

            }

        });

        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub

                Toast.makeText(getApplicationContext(), arg2 + "clicke", 1000)
                        .show();

            }
        });

        list.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                Employee emp = new Employee();
                db.deleteContact(emp);
                return false;
            }
        });

    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        List<Employee> contacts = db.getAllContacts();
        for (Employee cn : contacts) {
            String log = "Id: " + cn.get_id() + " ,Name: " + cn.get_name()
                    + " ,Phone: " + cn.get_email();
            // Writing Contacts to log
            Log.d("Name: ", log);
        }
        ListView list = (ListView) findViewById(R.id.listView1);
        ContanctAdapter objAdapter = new ContanctAdapter(MainActivity.this,
                R.layout.alluser_row, contacts);
        list.setAdapter(objAdapter);
    }

} 

我希望删除特定项目上的联系人,当我想要longitemclick功能但我无法删除时请告诉我如何耗尽联系。

1 个答案:

答案 0 :(得分:0)

像这样使用

//删除

public void deleteid(Employee contact) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_EMployee, KEY_ID + " = ?",
            new String[] { String.valueOf(contact.getID()) });
    db.close();
}

你应该像

一样调用这个方法
 Employee emp = new Employee(); 
    emp.setId(yourid);
        db.deleteContact(emp);