ListView with custom arrayadapter won't update properly after call to onDestroy() then onCreate()
I have a listview with a custom array adapter:
public class SmsMessageAdapter extends ArrayAdapter<ContactMessage> {
private ArrayList<ContactMessage> items;
public SmsMessageAdapter(Context context, int textViewResourceId, ArrayList<ContactMessage> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.message, null);
}
ContactMessage c = items.get(position);
if(c != null) {
String name = c.getContact();
if(name.equals("Me")) v.setBackgroundResource(R.drawable.sms_history_outgoing_background_gradient);
else v.setBackgroundResource(R.drawable.sms_history_incoming_background_gradient);
TextView contactName = (TextView) v.findViewById(R.id.message_contact_name);
TextView body = (TextView) v.findViewById(R.id.message_body);
TextView date = (TextView) v.findViewById(R.id.message_date_time);
if(contactName != null) {
contactName.setText(name);
}
if(body != null) {
body.setText(c.getBody());
}
if(date != null) {
date.setText(c.getFormattedTimeString());
}
}
return v;
}
}
I also have a method in my main activity which updates the listview by adding items onto the array list associated with the custom array adapter:
private void displayContactSmsHistory(String phoneNumber) {
Contact c = new Contact();
for(Contact contact : mContactsArrayList) {
if(getOnlyNumerics(phoneNumber).equals(getOnlyNumerics(contact.getPhoneNumber()))) {
c = contact;
}
}
HashMap<String, Boolean> unreadPositions = mContactsAdapter.getUnreadMap();
unreadPositions.put(getOnlyNumerics(phoneNumber), false);
mContactsAdapter.setUnreadMap(unreadPositions);
HashMap<String, Boolean> selectedPosition = mContactsAdapter.getSelectedMap();
for(String key : selectedPosition.keySet()) {
selectedPosition.put(key, false);
}
selectedPosition.put(getOnlyNumerics(phoneNumber), true);
mContactsAdapter.setSelectedMap(selectedPosition);
String contactName;
if(c.hasName()) contactName = c.getName();
else contactName = phoneNumber;
mConversationArrayAdapter.clear();
if(!db.isOpen()) db = db.open();
Cursor smsHistory = db.getSmsForContact(phoneNumber);
startManagingCursor(smsHistory);
int bodyIndex = smsHistory.getColumnIndex(DBAdapter.KEY_BODY);
int dateIndex = smsHistory.getColumnIndex(DBAdapter.KEY_DATE);
int typeIndex = smsHistory.getColumnIndex(DBAdapter.KEY_TYPE);
if(smsHistory.moveToFirst()) {
while(!smsHistory.isAfterLast()) {
String body = smsHistory.getString(bodyIndex);
String dateString = smsHistory.getString(dateIndex);
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
Date date;
try {
date = df.parse(dateString);
} catch (ParseException e) {
Log.e(TAG, "Error parsing date string while displaying contact info:", e);
date = new Date(0);
}
if(smsHistory.getInt(typeIndex) == DBAdapter.RECEIVED) {
smsHistoryArrayList.add(new ContactMessage(date, contactName, body));
} else {
smsHistoryArrayList.add(new ContactMessage(date, null, body));
}
smsHistory.moveToNext();
}
}
stopManagingCursor(smsHistory);
if(db.isOpen()) db.close();
}
The update method can be called from two different places in my code; 1) if the user selects an item from another listview, and 2) if开发者_开发技巧 I call the method directly in certain situations. This all works find and dandy, except in the situation where the user presses the back button (which initiates a call to onDestroy()
), then returns to the app (causing a new call to onCreate()
). If that happens, the listview will only update from one place in my code (if the user selects an item in the other listview). For the other situations where the update method is called, the code is executed but the listview does not update. I've run through with the debug tool, watching variables inside the update method and I can't see any reason why the listview is not updating.
Also note, if the user presses the home button, instead of the back button, and then returns to the app, everything works fine. In this situation onDestroy()
and onCreate()
are never called, only onStop()
and onStart()
.
If necessary I can post my onCreate()
and onDestroy()
, however I can say now that there is nothing in those two methods which I believe is causing the error (onCreate()
simply loads preferences and sets the layout, and onDestroy()
closes the database).
There are several way to achieve this.
- Make your ArrayList variable local (Declare in side function).
- Clear your ArrayList variable
I hope this is helpful for you
I ended up reverting to an earlier SVN commit and rewriting my previous progress. The problem didn't occur a second time... I still have no idea why it happened in the first place.
精彩评论