我有一个使用 ViewPager 和 Fragments 来创建标签的应用。
在我的应用程序中,我有一个聊天功能。每当接收或发送聊天时,它都会存储在数据库的表中。收到的聊天来自不同的线程,用户的聊天被输入到 Chat fragmenet 的 EditText 窗口中。
我有一个名为“LogChat.java”的类,用于将聊天记录保存到数据库中。
我有一个 ListView,它由显示聊天的 SimpleCursorAdapter 支持。此 ListView 在我的应用程序的聊天 fragment 中。
除了 ListView 不会自动刷新以显示最新的聊天记录外,一切都运行良好。在聊天 fragment 中,我有这个方法来显示聊天:
displayChats()
public void displayChats(){
DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
Cursor chatsCursor = databaseHelper.getChatsCursor();
String[] fromColumns = {chatInfo, chatContent};
int[] toViews = {R.id.chat_info, R.id.chat_content};
SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(getContext(), R.layout.line_of_chat, chatsCursor, fromColumns, toViews, 0);
ListView listView = (ListView) rootView.findViewById(R.id.chat_text_display);
listView.setAdapter(simpleCursorAdapter);
databaseHelper.close();
}
我用这个从输入中读取聊天,并将其保存到数据库中:
getView()
@Override
public View getView(){
Button sendChatButton = (Button) rootView.findViewById(R.id.send_chat_button);
EditText chatEntryWindow = (EditText) rootView.findViewById(R.id.chat_entry_window);
sendChatButton.setOnClickListener(new View.OnClickListener(){
String message = chatEntryWindow.getText().toString();
LogChat logChat = new LogChat();
logChat.addMessage(message);
displayChats();
});
}
在监听器线程中,我传入了对主 Activity 的引用,并使用它将聊天发送到 LogChat.java 以存储在数据库中:
Listener.java
public class Listener implements Runnable(){
private MainActivity mainActivity;
public Listener(MainActivity mainActivity){
this.mainActivity = mainActivity;
}
@Override
public void run(){
LogChat logChat = new LogChat();
logChat.addMessage(message);
}
}
就像我说的,除了刷新 ListView 以显示新收到的聊天之外,这一切都有效。我成功地存储了所有聊天记录(用户的和所有接收到的聊天记录),如果我的操作方式很棘手(即输入本地聊天记录),它们将正确显示在我的 ListView 中。
现在,为了查看收到的聊天记录,我必须在本地输入一个聊天记录,它调用 displayChats() 方法更新 ListView,在 ListView 中显示我输入的聊天记录和所有其他最近收到的聊天记录。
我知道我应该使用 notifyDataSetChanged()
,但我终究无法弄清楚如何使用它。一段时间以来,我一直在努力思考它,但就是无法“理解”。
我知道我应该像这样在 simpleCursorAdapter 变量上调用 notifyDataSetChanged()
:
simpleCursorAdapter.notifyDataSetChanged();
但我不确定如何从我的 LogChats 类或我的监听器线程访问它。或者,就此而言,从同一类的 getView()
方法中使用 SimpleCursorAdapter。我认为从 LogChats 类中执行此操作是个好主意,因此有一个中央用于在数据库中记录聊天并在一个地方刷新 View 的点。
我知道 notifyDataSetChanged()
一直被使用,所以我想我只是无法弄清楚如何正确使用它,但我还是被阻止了。
最佳答案
看起来这里的主要问题是将数据添加事件向下传递到适配器所在的 fragment 。
您可以只从监听器调用 MainActivity 中的公共(public)方法,该方法使用对 Fragment 的引用来调用将调用适配器上的 notifyDataSetChanged()
的公共(public)方法。
首先,在您的 FragmentPagerAdapter 中使用 instantiateItem()
覆盖来获取对您的 ChatFragment 的有效引用:
public class ViewPagerAdapter extends FragmentPagerAdapter {
public ChatFragment chatFragment;
//...
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ChatFragment();
case 1:
...
default:
return null;
}
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
if (position == 0) {
chatFragment = (ChatFragment) createdFragment;
}
return createdFragment;
}
}
然后,在 MainActivity 中定义将中继数据更改事件的方法:
public void dataChanged() {
//only update if the user is currently on the ChatFragment
if (mViewPager.getCurrentItem() == 0 && mPagerAdapter.chatFragment != null) {
mPagerAdapter.chatFragment.listViewDataChanged();
}
}
然后在你的 ChatFragment 中定义 listViewDataChanged()
方法,并确保适配器是 Fragment 类的成员变量:
public void listViewDataChanged() {
simpleCursorAdapter.notifyDataSetChanged();
}
//Class member variable:
SimpleCursorAdapter simpleCursorAdapter;
public void displayChats(){
DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
Cursor chatsCursor = databaseHelper.getChatsCursor();
String[] fromColumns = {chatInfo, chatContent};
int[] toViews = {R.id.chat_info, R.id.chat_content};
//modified:
simpleCursorAdapter = new SimpleCursorAdapter(getContext(), R.layout.line_of_chat, chatsCursor, fromColumns, toViews, 0);
ListView listView = (ListView) rootView.findViewById(R.id.chat_text_display);
listView.setAdapter(simpleCursorAdapter);
databaseHelper.close();
}
要将它们链接在一起,请在数据更改时从监听器调用 dataChanged()
。请注意,如果此 Runnable 在其自己的线程上运行,您将需要使用 runOnUiThread()
:
public class Listener implements Runnable(){
private MainActivity mainActivity;
public Listener(MainActivity mainActivity){
this.mainActivity = mainActivity;
}
@Override
public void run(){
LogChat logChat = new LogChat();
logChat.addMessage(message);
//added:
runOnUiThread(new Runnable() {
@Override
public void run() {
mainActivity.dataChanged();
}
});
}
}
关于android - 我不知道如何在我的应用程序中正确使用 notifyDataSetChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36919239/