我有一个带有自定义数组适配器的listview:
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;
}
}
我的主要活动中还有一个方法,它通过将项添加到与自定义数组适配器关联的数组列表来更新列表视图:
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();
}
可以从我的代码中的两个不同位置调用更新方法; 1)如果用户从另一个列表视图中选择一个项目,2)如果我在某些情况下直接调用该方法。除了在用户按下后退按钮(启动对onDestroy()
的调用),然后返回到应用程序(导致对onCreate()
的新调用)的情况下,这一切都可以找到和花花公子。如果发生这种情况,列表视图将仅从我的代码中的一个位置更新(如果用户选择其他列表视图中的项目)。对于调用update方法的其他情况,将执行代码,但listview不会更新。我已经使用调试工具,在update方法中查看变量,我看不出listview没有更新的任何原因。
另请注意,如果用户按下主页按钮而不是后退按钮,然后返回应用程序,一切正常。在这种情况下,onDestroy()
和onCreate()
永远不会被调用,只有onStop()
和onStart()
。
如有必要,我可以发布我的onCreate()
和onDestroy()
,但我现在可以说这两种方法中没有任何内容我认为导致错误(onCreate()
只是加载偏好并设置布局,onDestroy()
关闭数据库。
答案 0 :(得分:0)
有几种方法可以实现这一目标。
我希望这对你有所帮助
答案 1 :(得分:0)
我最终恢复到之前的SVN提交并重写我以前的进度。这个问题第二次没有发生......我仍然不知道为什么它一开始就发生了。