从手机中获取联系人并将其显示在列表视图中,然后标记联系人并将其添加到列表视图中,从那里标记的列表视图正在添加,未标记的列表视图正在删除。
我用于此的代码是..
@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();
}
联系人正在完美地添加到数据库中,但没有从数据库中删除......