2016-09-02 5 views
0

をリストに追加されることから、重複するビューを防止[解決しよう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) 

chatview パブリッククラスChatView { プライベート文字列IDです。...

+0

このエラーに関する情報を追加する必要があります。 – peterremec

+0

エラーは何ですか? –

+0

あなたのコードでは、おそらくiの代わりにインデックスi-1を使うべきでしょう。あなたのコードに '!chatview.get(i).getId()。equals(chatview.get(i).getId())'がありますが、その下には '!chatview.get(i).getId()があります。は(chatview.get(i-1).getId())と等しくなります。したがって、条件は常にfalseと評価されます。 – peterremec

答えて

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

09-02 10:34:51.473 15125-15125/pet.com.br.pet E/AndroidRuntime:致命的除外:メイン プロセス:pet.com.br.pet、PID:15125 java.lang.NullPointerException :空のオブジェクト参照で仮想メソッド 'boolean java.lang.String.equals(java.lang.Object)'を呼び出そうとしています pet.com.br.pet.chat.ChatViewActivity.parseData(ChatViewActivity.java:167) – yakonazaki

+0

それは生成していて、情報はまだ複写されています。 – yakonazaki

+0

あなたは何か考えていますか? – yakonazaki

1

がどのコンテナ知ってはいけない、これを試してみてくださいあなたはチャットビューに使用しています。 IDが値

を整数に。しかし、それはさらに良く見えるように、あなたのChatViewを更新するために変換することができれば、これはあなたが使用しているコンテナに応じて最適化することができ

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

:しかし、あなたはそのような何かを書くことができます以下のような:

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

今(あなたが順序を保持する必要がある場合)あなたはHashSetLinkedHashSetであなたのChatViewのアイテムを格納することができますし、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(); 
} 
関連する問題