何刷新ListView控件后更改与DialogFragment的

何刷新ListView控件后更改与DialogFragment的

本文介绍了如何刷新ListView控件后更改与DialogFragment的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建DialogFragment一个自定义对话框,然后在对话框中,我可以添加一些数据SQLite数据库。此外,还有在主要活动列表视图,显示了SQLite的数据。

我要刷新列表视图时,我将数据添加到从对话框中的数据库。不过,我有一些问题。

我所说的notifyDataSetChanged()在onResume(),但是当我关闭该对话框列表视图不会刷新。如果我preSS home键,并从最近的列表中打开该活动,列表视图将会刷新。

  @覆盖
保护无效onResume(){
    super.onResume();
    listItem.clear();
    ServerListDB DB =新ServerListDB(背景);
    光标光标= db.select();
    为(cursor.moveToFirst();!cursor.isAfterLast(); cursor.moveToNext()){
        HashMap的<弦乐,对象>地图=新的HashMap<弦乐,对象>();
        map.put(服务器名称,将String.valueOf(cursor.getString(1)));
        map.put(SERVERIP,cursor.getString(2));
        map.put(serverPort,cursor.getString(3));
        listItem.add(地图);
    }
    notifyDataSetChanged();
}

我添加了log.v中的onPause(),并在对话框中显示,在onPause()没有被调用。这是正确的DialogFragment?

  @覆盖
保护无效的onPause(){
    super.onPause();
    Log.v(暂停,的onPause()被称为!);
}


解决方案

您对话片段将被捆绑到活动。
一个简单的方法是直接从对话框通知你的父活动,如下所述:

在此基础上,我将创建一个接口活动必须为了得到一个回调时,数据库实现更新并重新加载列表(或其他):

 公共接口OnDBUpdatedListener {
    公共无效OnDBUpdated();
}

在你的活动()你实现这个接口:

 公共无效OnDBUpdated(){
    //刷新列表在这里
}

在您的对话片段,当保存数据,或者当你关闭对话框,把下面的code:

 (OnDBUpdatedListener)getActivity())。OnDBUpdated()

I create a custom dialog with DialogFragment, and in the dialog, I can add some data to the SQLite database. Also, there is a listview in the main activity, which show the data of the SQLite.

I want to refresh the listview when I add the data to the database from the dialog. However, I have some problems.

I call the notifyDataSetChanged() in the onResume(), but the listview doesn't refresh when I dismiss the dialog. And if I press the home button and open the activity from the recent list, the listview will refresh.

@Override
protected void onResume() {
    super.onResume();
    listItem.clear();
    ServerListDB db = new ServerListDB(context);
    Cursor cursor = db.select();
    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("serverName", String.valueOf(cursor.getString(1)));
        map.put("serverIp", cursor.getString(2));
        map.put("serverPort", cursor.getString(3));
        listItem.add(map);
    }
    notifyDataSetChanged();
}

I add the log.v in the onPause(), and when the dialog show, the onPause() isn't called. Is this right for DialogFragment?

@Override
protected void onPause() {
    super.onPause();
    Log.v("Pause", "onPause() called!");
}
解决方案

Your dialog fragment will be tied to activity.One simple approach is to notify your parent activity directly from dialog, as described here:

http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

Based on this, I would create an Interface that Activity must implement in order to get a callback when database is updated and to reload a list (or whatever):

public interface OnDBUpdatedListener {
    public void OnDBUpdated();
}

In your activity(s) you implement this interface:

public void OnDBUpdated() {
    // Reload list here
}

In your dialog fragment, when you save data or when you are dismissing the dialog, put the following code:

(OnDBUpdatedListener)getActivity()).OnDBUpdated()

这篇关于如何刷新ListView控件后更改与DialogFragment的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:02