gpt4 book ai didi

java - 防止将重复 View 添加到列表适配器

转载 作者:行者123 更新时间:2023-12-01 09:32:29 24 4
gpt4 key购买 nike

我的代码有问题 [已解决,THX ALL]

private void parseData(JSONArray array) {
for (int i = 0; i < array.length(); i++) {
ChatView chatvieww = new ChatView();
JSONObject json = null;
try {
json = array.getJSONObject(i);
chatvieww.setCodigo(json.getString(ChatViewUtils.TAG_CODIGO));
chatvieww.setUsername(json.getString(ChatViewUtils.TAG_USERCHAT));
chatvieww.setMensagem(json.getString(ChatViewUtils.TAG_MENSAGEM));

} catch (JSONException e) {
e.printStackTrace();
}

if (chatview.size() > 1) {
if (!chatview.get(i).getId().equals(chatview.get(i-1).getId())) {
chatview.add(chatvieww);
}
} else {
this.chatview.add(chatvieww);
}
}
//Notifying the adapter that data has been added or changed
adapter.notifyDataSetChanged();
}

我有错误,

if (!chatview.get(i).getId().equals(chatview.get(i-1).getId()))

如何解决这个问题?

这是我的Logcat

09-02 10:22:21.163 8263-8263/pet.com.br.pet E/AndroidRuntime: FATAL EXCEPTION: main
Process: pet.com.br.pet, PID: 8263
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at pet.com.br.pet.chat.ChatViewActivity.parseData(ChatViewActivity.java:165)

聊天 View 公共(public)类 ChatView { 私有(private)字符串 ID;...

最佳答案

不知道您使用哪个容器用于聊天 View 。但你可以写这样的东西:

private void parseData(JSONArray array) {
Set<String> current_ids = new HashSet<String>();
//cache current ids
for(ChatView current : chatveiww) {
current_ids.add(current.getId());
}

for (int i = 0; i < array.length(); i++) {
ChatView newView = new ChatView();
JSONObject json = null;
try {
json = array.getJSONObject(i);
newView.setId(json.getString(ChatViewUtils.TAG_ID));
newView.setCodigo(json.getString(ChatViewUtils.TAG_CODIGO));
newView.setUsername(json.getString(ChatViewUtils.TAG_USERCHAT));
newView.setMensagem(json.getString(ChatViewUtils.TAG_MENSAGEM));

} catch (JSONException e) {
e.printStackTrace();
}

if (current_ids.contains(newView.getId()) {
//skip duplicate item
continue;
}

current_ids.add(newView.getId());
chatview.add(newView);
}
//Notifying the adapter that data has been added or changed
adapter.notifyDataSetChanged();
}

这可以根据您使用的容器以及 ID 是否可以转换为整数值进行优化

但为了让它更好,更新你的 ChatView 看起来像:

class ChatView {
private String mID; //the id string

/*** Your rest code***/

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

ChatView other = (ChatView) o;

return !(mID != null ? !mID.equals(other.mID) : other.mID != null);
}

@Override
public int hashCode() {
return mID != null ? mID.hashCode() : 0;
}
}

现在,您可以将 ChatView 项存储在 HashSetLinkedHashSet 中(如果需要保留顺序),并且 parseData 变得更简单:

private void parseData(JSONArray array) {
for (int i = 0; i < array.length(); i++) {
ChatView newView = new ChatView();
JSONObject json = null;
try {
json = array.getJSONObject(i);
newView.setId(json.getString(ChatViewUtils.TAG_ID));
newView.setCodigo(json.getString(ChatViewUtils.TAG_CODIGO));
newView.setUsername(json.getString(ChatViewUtils.TAG_USERCHAT));
newView.setMensagem(json.getString(ChatViewUtils.TAG_MENSAGEM));

} catch (JSONException e) {
e.printStackTrace();
}

if (chatview.contains(newView) {
//skip duplicate item
continue;
}

chatview.add(newView);
}
//Notifying the adapter that data has been added or changed
adapter.notifyDataSetChanged();
}

关于java - 防止将重复 View 添加到列表适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39293167/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com