从手机中获取联系人并将其显示在列表视图中,并标记联系人并将其添加到列表视图中,从标记列表视图中添加和取消标记列表视图即被删除。
我用于此的代码是..
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
x= position;
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.contactliststyle, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.txtName = (TextView) view.findViewById(R.id.txtView_Name);
viewHolder.txtNumber = (TextView)view.findViewById(R.id.txtView_Number);
viewHolder.selection = (CheckBox)view.findViewById(R.id.checkbtn_selectContact);
viewHolder.selection
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
ContactModel element = (ContactModel)viewHolder.selection
.getTag();
element.setSelected(buttonView.isChecked());
if(buttonView.isChecked())
{
String selectedName=element.getName();
String selectedNumber=element.getNumber();
System.out.println("etrtrt"+element.getName());
DatabaseHandler db = new DatabaseHandler(context);
Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact(selectedName, selectedNumber));
}
else
{
String selectedName=element.getName();
String selectedNumber=element.getNumber();
System.out.println("Number"+selectedNumber);
System.out.println("Name"+selectedName);
DatabaseHandler db = new DatabaseHandler(context);
db.deleteContact(new Contact(selectedName,selectedNumber));
}
}
});
openHelper类中用于删除联系人的代码是
// Deleting single contact
public void deleteContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
System.out.println("Deleted");
db.close();
}
联系人正在完美地添加到数据库,但没有从数据库中删除......
答案 0 :(得分:0)
试试这个
使用getWritableDatabase()。delete(TABLE_NAME,TABLE_ROW_ID +“=”+ rowId,null)