返回信息流我在listview中setadapter了一个adapter,其中的数据是通过接受广播来实时更新的。当我关闭了这个activity,再重新打开一次后,可以接收到广播数据,但是adapter不再更新了。只有跳转到其他页面,再返回这个页面时,才会更新。(我是用的notifyDataSetChanged方法)。我调试了一下,发现这两次的adapter的id是不一样的,相当于新new了一个adapter。现在我就不知道该怎么达到实时更新了。求大神指点一下。
这是一条镜像帖。来源:北邮人论坛 / mobile-terminal-at / #13136同步于 2014/4/9
该镜像源已超过 30 天没有更新,可能在源站已被删除。
MobileTerminalAT机器人发帖
android中listview设置的adapter不能更新问题,求解答啊
yangxiaoxin
2014/4/9镜像同步9 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
关闭activity是怎么关的?彻底么?
【 在 yangxiaoxin 的大作中提到: 】
: 我在listview中setadapter了一个adapter,其中的数据是通过接受广播来实时更新的。当我关闭了这个activity,再重新打开一次后,可以接收到广播数据,但是adapter不再更新了。只有跳转到其他页面,再返回这个页面时,才会更新。(我是用的notifyDataSetChanged方法)。我调试了一下,发现这两次的adapter的id是不一样的,相当于新new了一个adapter。现在我就不知道该怎么达到实时更新了。求大神指点一下。
【 在 kingkongman 的大作中提到: 】
: 关闭activity是怎么关的?彻底么?
其实是一个登陆功能,登陆后是在一个activity里面装了个fragment,用来显示广播的数据。退出登录后,再重新登陆,还会打开这个界面,我感觉也是有些缓存没有清空。所以无法更新。会不会是数据源的问题?
【 在 icyfox 的大作中提到: 】
: 看不太懂...要不。。上代码段?
代码有点长,就是在登录后第一次展示的时候是正常的,退出登录再次登陆进入展示的时候,出现了adapter.notifyDataSetChanged();的时候,不再执行getview。。。不知道是什么原因
【 在 icyfox 的大作中提到: 】
: 看不太懂...要不。。上代码段?
我还是把代码发上来希望大牛帮忙解决一下。万分感谢啦。就是我第二次进入的时候不再执行adapter的getview了。
public class ChatListFragment extends BaseFragment implements
OnChatListViewClickListener ,DeleteFriendInChat{
private ChatListView chatListView;
private BaseAdapter chatListAdapter = null;
private static Map<String, List<String>> msgMap = new HashMap<String, List<String>>();
private List<ChatEntity> chatEntities;// 存放当前用户与每个人的聊天记录
private static String gotoItemFriendName;
private static GetMessageReceiver msgReceiver;
private IntentFilter filter;
private static boolean isRegister = false;
private PersistableChat perChat;
private String MALE = "男";
private List<OffLineMsgEntity> offLineMsgs = new ArrayList<OffLineMsgEntity>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.chat_list_fragment, null);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
chatListView = (ChatListView) view.findViewById(R.id.chat_lv);
chatListView.setOnChatListViewClickListener(this);
}
@Override
public void onResume() {
super.onResume();
handlerMsg();
initData();
chatListAdapter.notifyDataSetInvalidated();
UserManager.getInstance().isMultiPlaying();
FriendEntity friendEntity = UserManager.getInstance()
.getPlayingTarget();
if (friendEntity != null) {
Timber.i("friendEntity:" + friendEntity.getFriendJid() + "---"
+ friendEntity.getNickName());
} else {
Timber.i("friendEntity:null");
}
}
private void initData() {
chatEntities = new ArrayList<ChatEntity>();
gotoItemFriendName = null;
perChat = new PersistableChat();
try {
chatEntities = DataManager.getInstance().query(perChat,
"userName='" + ASmackUtils.getRosterName() + "'", null,
"friendName", null, "msgTime desc");
} catch (IOException e) {
e.printStackTrace();
}
FriendListFragment.setDeleteFriendInChat(this);
chatListView.setAdapter(chatListAdapter = new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(getActivity()).inflate(
R.layout.chat_listview_cell, null);
ImageView friendAvatar = (ImageView) convertView
.findViewById(R.id.chat_friend_avatar);
ImageView friendGenderIV = (ImageView) convertView
.findViewById(R.id.item_chat_friend_gender_iv);
TextView friendNameTV = (TextView) convertView
.findViewById(R.id.item_chat_friend_name);
TextView friendMsgTV = (TextView) convertView
.findViewById(R.id.item_chat_msg);
TextView msgTimeTV = (TextView) convertView
.findViewById(R.id.item_chat_msg_date);
TextView msgCountTV = (TextView) convertView
.findViewById(R.id.item_msg_count);
List<FriendEntity> friendEntities = new ArrayList<FriendEntity>();
PersistableFriend perFriend = new PersistableFriend();
try {
friendEntities = DataManager.getInstance()
.query(perFriend,
USER_JID + "=? and " + NICK_NAME + "=?",
new String[] {
ASmackUtils.getUserJID(),
chatEntities.get(position)
.getFriendName() }, null,
null, null);
} catch (IOException e) {
e.printStackTrace();
}
Timber.d(ASmackUtils.getUserJID()+"---"+chatEntities.get(position)
.getFriendName());
Timber.i("friendEntities:" + friendEntities.size());
Timber.i("friendEntities.nickname:"
+ friendEntities.get(0).getNickName());
Drawable friendAvatarDrawable = null;
if (friendEntities.get(0).getUserAvatar() == null) {
friendAvatarDrawable = getActivity().getResources()
.getDrawable(
UserManager.getInstance()
.setUserDefaultAvatar(
friendEntities.get(0)
.getUserGender()));
} else {
friendAvatarDrawable = FormatUtils
.Bytes2Drawable(friendEntities.get(0)
.getUserAvatar());
}
friendAvatar.setImageDrawable(friendAvatarDrawable);
if (friendEntities.get(0).getUserGender().equals(MALE)) {
friendGenderIV.setBackgroundResource(R.drawable.ic_male);
} else {
friendGenderIV.setBackgroundResource(R.drawable.ic_female);
}
ChatEntity chatEntity = chatEntities.get(position);
String friendName = chatEntity.getFriendName();
String message = chatEntity.getMessage();
String msgTime = TimeUtils.toNowTime(chatEntity.getMsgTime());
friendNameTV.setText(friendName);
if (message.length() > 13) {
String shortMsg = message.substring(0, 13);
friendMsgTV.setText(shortMsg + "...");
} else {
friendMsgTV.setText(message);
}
Timber.d("message:"+message);
msgTimeTV.setText(msgTime);
if (message != null) {
if (msgMap.containsKey(friendNameTV.getText().toString())) {
List<String> showMsgList = new ArrayList<String>();
showMsgList = msgMap.get(friendNameTV.getText()
.toString());
if (showMsgList.size() > 0) {
msgCountTV.setText(showMsgList.size() + "");
}
}
}
return convertView;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Object getItem(int position) {
return chatEntities.get(position);
}
@Override
public int getCount() {
return chatEntities.size();
}
});
offLineMsgs = ChatMessageListener.getInstance().getOffLineMsgs();
for (OffLineMsgEntity offLineMsgEntity : offLineMsgs) {
saveMsg(offLineMsgEntity.getFrom(), offLineMsgEntity.getBody());
chatListAdapter.notifyDataSetChanged();
}
ChatMessageListener.getInstance().getOffLineMsgs().clear();
ChatMessageListener.getInstance().setOffLineMsgFlag(false);
}
/**
* 对收到的信息的操作
*/
public void handlerMsg() {
msgReceiver = new GetMessageReceiver();
msgReceiver.setiCallBack(new ASmackRequestCallBack() {
@Override
public void responseSuccess(Object object) {
Message msg = (Message) object;
switch (msg.what) {
case 0: {
Message message = msg;
String from = message.getData().getString("from");
String body = message.getData().getString("body");
Timber.i("chatListFragment--handlerMsg:" + from + "---" + body);
saveMsg(from, body);
Timber.d("chatListAdapter:"+chatListAdapter);
chatListAdapter.notifyDataSetChanged();
}
break;
default:
break;
}
}
@Override
public void responseFailure(int reflag) {
}
});
filter = new IntentFilter();
filter.addAction("com.linkcube.message");
filter.addCategory(Intent.CATEGORY_DEFAULT);
if (isRegister == false) {// 判断是否已经注册过广播
getActivity().registerReceiver(msgReceiver, filter);
isRegister = true;
}
}
private void saveMsg(String friendName, String body) {
// 需要将from转换成nickname
List<FriendEntity> friendEntities = new ArrayList<FriendEntity>();
PersistableFriend perFriend = new PersistableFriend();
try {
friendEntities = DataManager.getInstance().query(
perFriend,
FRIEND_JID + "=? and " + USER_JID + "=?",
new String[] { ASmackUtils.getFriendJid(friendName),
ASmackUtils.getUserJID() }, null, null, null);
} catch (IOException e1) {
e1.printStackTrace();
}
ChatEntity chatEntity = new ChatEntity();
chatEntity.setUserName(ASmackUtils.getRosterName());
chatEntity.setFriendName(friendEntities.get(0).getNickName());
chatEntity.setMsgFlag("get");
chatEntity.setMessage(body);
chatEntity.setMsgTime(TimeUtils.getNowDateAndTime());
DataManager.getInstance().insert(perChat, chatEntity);
if (!friendName.equals(gotoItemFriendName)) {
if (msgMap.containsKey(friendName)) {// 已经包含发送者
msgMap.get(friendName).add(body);
} else {// 第一次收到,不包含发送者
List<String> msgList = new ArrayList<String>();
msgList.add(body);
msgMap.put(friendName, msgList);
}
}
// 选择出与每个用户聊天的最后一条信息
try {
chatEntities = DataManager.getInstance().query(perChat,
"userName='" + ASmackUtils.getRosterName() + "'", null,
"friendName", null, "msgTime desc");
Timber.i("chatEntities:" + chatEntities.size());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
TextView itemFriendTV = (TextView) view
.findViewById(R.id.item_chat_friend_name);
String itemFriendName = itemFriendTV.getText().toString();
gotoItemFriendName = itemFriendName;
Intent intent = new Intent(getActivity(), ChatActivity.class);
Bundle bundle = new Bundle();
bundle.putString("nickName", itemFriendName);
bundle.putSerializable("msgMap", (Serializable) msgMap);
intent.putExtras(bundle);
if (msgMap.containsKey(itemFriendName)) {
msgMap.get(itemFriendName).clear();
}
startActivity(intent);
}
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int position, long arg3) {
final String friendName = chatEntities.get(position).getFriendName();
AlertUtils.showAlert(mActivity, "是否删除与对方的聊天纪录", null,
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 从数据库中删除与对方的聊天信息
ChatEntity chatEntity = new ChatEntity();
chatEntity.setFriendName(friendName);
DataManager.getInstance().delete(perChat, chatEntity);
// 从msgMap中删除
if (msgMap.containsKey(friendName)) {
msgMap.remove(friendName);
}
// 从聊天展示列表中删除对方
chatEntities.remove(position);
chatListAdapter.notifyDataSetChanged();
}
}, null);
return true;
}
@Override
public void deleteFriendInChat(String friendNickname) {
// 从数据库中删除与对方的聊天信息
ChatEntity chatEntity = new ChatEntity();
chatEntity.setFriendName(friendNickname);
DataManager.getInstance().delete(perChat, chatEntity);
// 从msgMap中删除
if (msgMap.containsKey(friendNickname)) {
msgMap.remove(friendNickname);
}
// 从聊天展示列表中删除对方
//chatEntities.lastIndexOf(object)
int position=0;
for(int i=0;i<chatEntities.size();i++){
if(friendNickname.equals(chatEntities.get(i).getFriendName())){
Timber.d("position:"+position);
position=i;
break;
}
}
chatEntities.remove(position);
chatListAdapter.notifyDataSetChanged();
}
}
分析一下,不一定对
请看一下下面这句代码:
BaseAdapter.notifyDataSetInvalidated();
你在 initData() 中实例化了 adapter,接着你又将它 invalidate,然后请再瞄一眼 Android Developer 中对 notifyDataSetInvalidated() 的定义:
Notifies the attached observers that the underlying data is no longer valid or available. Once invoked this adapter is no longer valid and should not report further data set changes.
重点是最后一句话:如果调用了这个方法,该 adapter 将不再有效且不会再报告后续的数据更改。
请检查一下是否是该方法导致了你所描述的问题。
Good Luck!
【 在 yangxiaoxin 的大作中提到: 】
:
: 我还是把代码发上来希望大牛帮忙解决一下。万分感谢啦。就是我第二次进入的时候不再执行adapter的getview了。
: public class ChatListFragment extends BaseFragment implements
: ...................
[upload=1][/upload][upload=1][/upload]
【 在 yangxiaoxin 的大作中提到: 】
:
: 其实是一个登陆功能,登陆后是在一个activity里面装了个fragment,用来显示广播的数据。退出登录后,再重新登陆,还会打开这个界面,我感觉也是有些缓存没有清空。所以无法更新。会不会是数据源的问题?
你的fragment是怎么添加到页面的?如果你是在activity里有一个成员变量 mFragment,new之后就不会变的,而且使用transaction.replace方法置换页面元素的话,那你mFragment的adapter应该是不会变的。但是如果你每次打开这个页面都是new一个fragment那它里面的数据肯定就变了。
SimpleAdapter simpleAdapter = new SimpleAdapter(context, data, resource, from, to);
ListView listView = new ListView(this);
listView.setAdapter(simpleAdapter );
simpleAdapter.notifyDataSetChanged();
更新数据应该用的是适配器中的notifyDataSetChanged()方法,不是你那个吧。。。你可以试试这个