我有一个自定义列表视图,它实现了列表适配器,它有两个按钮,用于删除和编辑列表中的每个条目。删除按钮工作正常,但编辑按钮接受更改,但不会在列表中反映它,直到通过返回并再次返回活动重新创建活动。我尝试了notifyDataSetChanged()
,invalidate
,refreshDrawable
,但没有任何效果。任何帮助将不胜感激。这是我的Adapter_Settings代码。
public class Adapter_Settings extends BaseAdapter implements ListAdapter {
private ArrayList<String> ContactList = new ArrayList<String>();
private Context context;
private SaveValues sv = new SaveValues();
private ListView listView;
CharSequence options[] = new CharSequence[]{"New Contact", "Contact List"};
String emerConName, emerConNum;
public Adapter_Settings(ArrayList<String> ContactList, Context context,ListView list) {
this.ContactList = ContactList;
this.context = context;
this.listView=list;
}
@Override
public int getCount() {
return ContactList.size();
}
@Override
public Object getItem(int position) {
return ContactList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.settings_list, null);
}
final TextView Contact_list = (TextView) view.findViewById(R.id.Contact_settings);
Contact_list.setText(ContactList.get(position));
ImageButton DeleteBtn = (ImageButton) view.findViewById(R.id.delete_button);
ImageButton EditBtn = (ImageButton) view.findViewById(R.id.edit_button);
DeleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println(Contact_list.getText().toString());
System.out.println(position);
AlertDialog.Builder builder=new AlertDialog.Builder(context);
builder.setMessage("Are you sure you want to delete this contact?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String delcon;
delcon = Contact_list.getText().toString();
if (delcon.equals(sv.GetValue(ConstantValues.ContactName1, context) + "\n" + (sv.GetValue(ConstantValues.ContactNumber1, context)))) {
sv.SaveStringValues(ConstantValues.ContactName1, ConstantValues.skipper, context);
sv.SaveStringValues(ConstantValues.ContactNumber1, ConstantValues.skipper, context);
// System.out.println(sv.GetValue(ConstantValues.ContactName1, context));
} else if (delcon.equals(sv.GetValue(ConstantValues.ContactName2, context) + "\n" + (sv.GetValue(ConstantValues.ContactNumber2, context)))) {
sv.SaveStringValues(ConstantValues.ContactName2, ConstantValues.skipper, context);
sv.SaveStringValues(ConstantValues.ContactNumber2, ConstantValues.skipper, context);
// System.out.println(sv.GetValue(ConstantValues.ContactName2, context));
} else if (delcon.equals(sv.GetValue(ConstantValues.ContactName3, context) + "\n" + (sv.GetValue(ConstantValues.ContactNumber3, context)))) {
sv.SaveStringValues(ConstantValues.ContactName3, ConstantValues.skipper, context);
sv.SaveStringValues(ConstantValues.ContactNumber3, ConstantValues.skipper, context);
// System.out.println(sv.GetValue(ConstantValues.ContactName3, context));
} else if (delcon.equals(sv.GetValue(ConstantValues.ContactName4, context) + "\n" + (sv.GetValue(ConstantValues.ContactNumber4, context)))) {
sv.SaveStringValues(ConstantValues.ContactName4, ConstantValues.skipper, context);
sv.SaveStringValues(ConstantValues.ContactNumber4, ConstantValues.skipper, context);
// System.out.println(sv.GetValue(ConstantValues.ContactName4, context));
}
ContactList.remove(position);
notifyDataSetChanged();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
notifyDataSetChanged();
}
});
EditBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String editcon = Contact_list.getText().toString();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.Add_Contact);
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
builder1.setTitle("New Contact");
builder1.setView(inflater.inflate(R.layout.defaultcontact_popup, null)).setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
Dialog d = (Dialog) dialog;
EditText newnameview, newphoneview;
// System.out.println("=======CONTACT ADDED=============");
newnameview = (EditText) d.findViewById(R.id.ConName);
newphoneview = (EditText) d.findViewById(R.id.ConNumber);
emerConName = newnameview.getText().toString();
emerConNum = newphoneview.getText().toString();
if (editcon.equals(sv.GetValue(ConstantValues.ContactName1, context) + "\n" + (sv.GetValue(ConstantValues.ContactNumber1, context)))) {
sv.SaveStringValues(ConstantValues.ContactName1, emerConName, context);
sv.SaveStringValues(ConstantValues.ContactNumber1, emerConNum, context);
//Contact_list.setText(emerConName + "\n" + emerConNum);
} else if (editcon.equals(sv.GetValue(ConstantValues.ContactName2, context) + "\n" + (sv.GetValue(ConstantValues.ContactNumber2, context)))) {
sv.SaveStringValues(ConstantValues.ContactName2, emerConName, context);
sv.SaveStringValues(ConstantValues.ContactNumber2, emerConNum, context);
//Contact_list.setText(emerConName + "\n" + emerConNum);
} else if (editcon.equals(sv.GetValue(ConstantValues.ContactName3, context) + "\n" + (sv.GetValue(ConstantValues.ContactNumber3, context)))) {
sv.SaveStringValues(ConstantValues.ContactName3, emerConName, context);
sv.SaveStringValues(ConstantValues.ContactNumber3, emerConNum, context);
//Contact_list.setText(emerConName + "\n" + emerConNum);
} else if (editcon.equals(sv.GetValue(ConstantValues.ContactName4, context) + "\n" + (sv.GetValue(ConstantValues.ContactNumber4, context)))) {
sv.SaveStringValues(ConstantValues.ContactName4, emerConName, context);
sv.SaveStringValues(ConstantValues.ContactNumber4, emerConNum, context);
//Contact_list.setText(emerConName+"\n"+emerConNum );
}
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// System.out.println("=========CONTACT REMOVED==========");
}
});
builder1.show();
break;
case 1:
Intent intent = new Intent(Intent.ACTION_PICK); //Crating a new intent to PICK contact from list
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); //Setting the type to fetch all attributes of selected contact
//ActivityCompat.startActivityForResult(intent, ConstantValues.PHONE_REQUEST_CODE);// starting the intent with a unique value of PHONE_REQUEST_CODE
break;
}
}
});
builder.show();
notifyDataSetChanged();
}
});
return view;
}
}