调用 onDestroy() 然后调用 onCreate() 后,具有自定义数组适配器的 ListView 将无法正确更新

发布于 2024-11-18 09:24:05 字数 4412 浏览 1 评论 0原文

我有一个带有自定义数组适配器的列表视图:

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不会更新。我已经使用调试工具运行过,观察更新方法内的变量,但我看不出列表视图不更新的任何原因。

另请注意,如果用户按下主页按钮而不是后退按钮,然后返回到应用程序,则一切正常。在这种情况下,永远不会调用 onDestroy()onCreate(),只会调用 onStop()onStart()

如果有必要,我可以发布我的 onCreate()onDestroy(),但是我现在可以说,我认为这两个方法中没有任何内容导致了错误(< code>onCreate() 只是加载首选项并设置布局,而 onDestroy() 则关闭数据库。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

做个ˇ局外人 2024-11-25 09:24:05

有几种方法可以实现这一目标。

  1. 将您的 ArrayList 变量设置为本地变量(在辅助函数中声明)。
  2. 清除您的 ArrayList 变量

我希望这对您有帮助

There are several way to achieve this.

  1. Make your ArrayList variable local (Declare in side function).
  2. Clear your ArrayList variable

I hope this is helpful for you

番薯 2024-11-25 09:24:05

我最终恢复到早期的 SVN 提交并重写了我之前的进度。这个问题没有第二次发生......我仍然不知道为什么它第一次发生。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文