问题描述
我有一个列表,并linkedhashmap.I我在主类的顶部以下行定义:
列表<会话与GT; conversationsList =新的ArrayList<会话与GT;();
LinkedHashMap的<弦乐,会话与GT; conversationsMap =新的LinkedHashMap<弦乐,会话与GT;();
在的onCreate()
我从数据库将数据传输到HashMap的,然后转移映射值到ArrayList和我使用此ArrayList的适配器视图与下面几行:
conversationsMap = _db.getAllConversations();
conversationsList =新的ArrayList<会话与GT;(conversationsMap.values());
conversationsAdapter =新ConversationsAdapter(这一点,conversationsList);
但有件事我不understand.I可以更新这些行名单:
conversationsMap.get(ROOM_NAME).increaseUnread();
((ConversationsAdapter)conversationsAdapter).updateConversations(conversationsList);
它的工作,但我没有更新 conversationsList
为什么这工作,我不明白?我刚刚更新 conversationsMap
不是 conversationsList
。任何人都可以,为什么这是工作向我解释?
在数据库处理程序 getAllConversations
方法:
公共LinkedHashMap的<弦乐,会话与GT; getAllConversations(){
清单<会话与GT; conversationsList =新的ArrayList<会话与GT;();
最终的LinkedHashMap<弦乐,会话与GT;对话=新的LinkedHashMap<弦乐,会话与GT;();
字符串selectQuery =SELECT * FROM+ TABLE_CONVERSATIONS +ORDER BY ID ASC; SQLiteDatabase分贝= this.getWritableDatabase();
光标光标= db.rawQuery(selectQuery,NULL);
如果(cursor.moveToFirst()){
做{
交谈交谈=新的对话();
Conversation.setId(的Integer.parseInt(cursor.getString(0)));
Conversation.setSender(cursor.getString(1));
Conversation.setTo(cursor.getString(2));
Conversation.setName(cursor.getString(3));
Conversation.setBio(cursor.getString(4));
Conversation.setPicture(cursor.getString(5));
Conversation.setTime(的Integer.parseInt(cursor.getString(6)));
Conversation.setUnread(的Integer.parseInt(cursor.getString(7)));
conversations.put(cursor.getString(2),对话);
}而(cursor.moveToNext());
}
cursor.close();
回到对话;
}
和这是会话
类:
类对话
{
串发件人,于姓名,生物,图片;
整数ID,时间,未读;
公共会话(){ }
公共会话(INT ID,字符串发件人,字符串,字符串名称,生物字符串,字符串的画面,诠释时间,INT读){
this.sender =发送;
this.to =到;
this.id = ID;
this.name =名称;
this.bio =生物;
this.picture =图片;
this.time =时间;
this.unread =未读;
} 公共无效setSender(字符串发送){
this.sender =发送;
}
公共无效setTo(字符串){
this.to =到;
}
公共无效SETID(INT ID){
this.id = ID;
}
公共无效的setTime(INT时间){
this.time =时间;
}
公共无效setUnread(INT读){
this.unread =未读;
}
公共无效increaseUnread(){
this.unread ++;
}
公共无效setname可以(字符串名称){
this.name =名称;
}
公共无效setBio(字符串生物){
this.bio =生物;
}
公共无效setPicture(字符串图片){
this.picture =图片;
} 公共字符串getSender(){
返回this.sender;
}
公共字符串会过(){
返回this.to;
}
公众诠释的getId(){
返回this.id;
}
公众诠释的getTime(){
返回this.time;
}
公众诠释getUnread(){
返回this.unread;
}
公共字符串的getName(){
返回this.name;
}
公共字符串getBio(){
返回this.bio;
}
公共字符串Getpicture中(){
返回this.picture;
}
}
你熟悉 notifyDataSetChanged()
和 notifyDataSetInvalidated()
在适配器的方法呢?数据集的每一个变化,这是用来在列表/方格被绘制后,你应该 updateConversations(conversationsList)之后调用这两个方法(可能是第一个)之一;
也:行新的ArrayList<会话>(conversationsMap.values());
应该做你的地图值的浅拷贝,检查的 //stackoverflow.com/questions/689370/java-collections-copy-list-i-dont-understand\">this堆栈线程获取更多信息。
I have a list and linkedhashmap.I am defining with following lines in top of main class:
List<Conversation> conversationsList=new ArrayList<Conversation>();
LinkedHashMap<String, Conversation> conversationsMap=new LinkedHashMap<String, Conversation>();
In onCreate()
I am transferring data from db to hashmap then transferring map values to arraylist and I am using this arraylist in adapterview with following lines:
conversationsMap=_db.getAllConversations();
conversationsList=new ArrayList<Conversation>(conversationsMap.values());
conversationsAdapter=new ConversationsAdapter(this,conversationsList);
But there is something I don't understand.I can update the list with these lines:
conversationsMap.get(room_name).increaseUnread();
((ConversationsAdapter) conversationsAdapter).updateConversations(conversationsList);
It's working but I didn't update conversationsList
why this is working I don't understand ? I just updated conversationsMap
not the conversationsList
. Can anyone explain to me why this is working ?
In db handler getAllConversations
method:
public LinkedHashMap<String, Conversation> getAllConversations() {
List<Conversation> conversationsList=new ArrayList<Conversation>();
final LinkedHashMap<String, Conversation> conversations = new LinkedHashMap<String, Conversation>();
String selectQuery = "SELECT * FROM " + TABLE_CONVERSATIONS+" order by id asc";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Conversation Conversation = new Conversation();
Conversation.setId(Integer.parseInt(cursor.getString(0)));
Conversation.setSender(cursor.getString(1));
Conversation.setTo(cursor.getString(2));
Conversation.setName(cursor.getString(3));
Conversation.setBio(cursor.getString(4));
Conversation.setPicture(cursor.getString(5));
Conversation.setTime(Integer.parseInt(cursor.getString(6)));
Conversation.setUnread(Integer.parseInt(cursor.getString(7)));
conversations.put(cursor.getString(2),Conversation);
} while (cursor.moveToNext());
}
cursor.close();
return conversations;
}
And this is the Conversation
class:
class Conversation
{
String sender,to,name,bio,picture;
Integer id,time,unread;
public Conversation() {
}
public Conversation (int id,String sender,String to,String name,String bio,String picture,int time,int unread) {
this.sender=sender;
this.to=to;
this.id=id;
this.name=name;
this.bio=bio;
this.picture=picture;
this.time=time;
this.unread=unread;
}
public void setSender(String sender) {
this.sender=sender;
}
public void setTo(String to) {
this.to=to;
}
public void setId(int id) {
this.id=id;
}
public void setTime(int time) {
this.time=time;
}
public void setUnread(int unread) {
this.unread=unread;
}
public void increaseUnread() {
this.unread++;
}
public void setName(String name) {
this.name=name;
}
public void setBio(String bio) {
this.bio=bio;
}
public void setPicture(String picture) {
this.picture=picture;
}
public String getSender() {
return this.sender;
}
public String getTo() {
return this.to;
}
public int getId() {
return this.id;
}
public int getTime() {
return this.time;
}
public int getUnread() {
return this.unread;
}
public String getName() {
return this.name;
}
public String getBio() {
return this.bio;
}
public String getPicture() {
return this.picture;
}
}
are you familiar with notifyDataSetChanged()
and notifyDataSetInvalidated()
methods in your adapter? after every change of data set which is used to be drawn in List/Grid you should call one of these two methods (probably first one) after updateConversations(conversationsList);
also: line new ArrayList<Conversation>(conversationsMap.values());
should do a shallow copy of your map values, check this stack thread about set copying for more info
这篇关于我可以更新列表视图但如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!