防止将重复视图添加到列表适配器

时间:2016-09-02 13:04:22

标签: java android

我的代码[已解决所有问题]

有问题
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)

chatview     公共类ChatView {     private String id; ...

2 个答案:

答案 0 :(得分:1)

不知道您使用哪个容器进行聊天。但你可以这样写:

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();
}

答案 1 :(得分:0)

试试这个,

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.setId(json.getString(ChatViewUtils.TAG_ID));
            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();
        }

        // Linear search, see if the id exists
        boolean flag = false;
        for(ChatView chat : chatview){ 
            if(null != chat.getId() && null != chatvieww.getId()){
                if(chat.getId().equals(chatvieww.getId())){
                    // Item exists
                    flag = true;
                    break;
                }
            }
        }

        // if flag is true item exists, don't add.
        if(!flag){
            chatview.add(chatvieww);
        }
    }
    // Notifying the adapter that data has been added or changed
    adapter.notifyDataSetChanged();
}