[英]Delete data from database in Sqlite android
這是我在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函數時,我想刪除特定項目上的聯系人,但是我無法刪除,請告訴我如何耗盡聯系人。
這樣使用
//刪除
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.