2017-09-17 2 views
0

私のアプリケーションではをサーバから取得し、このリストを表示します。私はどのようにAndroidでArrayListの項目を取得できますか

私は以下のコードを書いている:私はそれが正しいだとショーは、データのユーザーには、このデータを表示すると、私はこの項目をクリックすると

private String[] mostlyMatchedKeywordsStrings; 
mostlyMatchedKeywordsStrings = searchResponse.getData().getMostlyMatchedKeywordsText(); 
cloudChipList.clear(); 
fullSearchMini_chipCloud.removeAllViews(); 

for (int i = 0; i < mostlyMatchedKeywordsStrings.length; i++) { 

    cloudChipList.add(mostlyMatchedKeywordsStrings[i]); 
    if (i >= mostlyMatchedKeywordsStrings.length - 2) { 

     fullSearchMini_didYouMeanLay.setVisibility(View.VISIBLE); 
     fullSearchMini_chipCloud.addChip(mostlyMatchedKeywordsStrings[i]); 
     Log.e("searchKeys", mostlyMatchedKeywordsStrings[i]); 

     fullSearchMini_chipCloud.setChipListener(new ChipListener() { 
      @Override 
      public void chipSelected(int i) { 
       try { 
        Log.e("searchKeys", "new : " + mostlyMatchedKeywordsStrings[i]); 

       } 
       catch (Exception e) { 
       } 
      } 

      @Override 
      public void chipDeselected(int i) { 

      } 
     }); 
    } 
} 

、しかし、それは別のアイテム私を示しています! Logcatで

それが項目の下に私を示しています

searchKeys: Recep Ivedik 5 

しかし、私はこの項目をクリックすると、それは私にlogcatで別の項目を示しています。アイテムを表示するには

searchKeys: new : Recep Ivedik 3 

をし、私はこのコードmostlyMatchedKeywordsStrings[i]を使用しています。なぜlogCatでこのアイテムをクリックすると別のアイテムが表示されるのですか?

+0

を? if(i> = mostlyMatchedKeywordsStrings.length - 2) – firegloves

+0

@firegloves、私はちょうど2最後の項目を表示します。すべての商品を表示しないちょうど2最後に。手伝って頂けますか? –

答えて

0

あなたの問題は、配列の間違った混合によるものだと思います。 私はChipCloudを使ったことはありませんが、mostlyMatchedKeywordsStrings配列のいくつかの項目に対してのみリスナーを登録すると、登録された配列のインデックスが異なります。

たとえば、mostlyMatchedKeywordsStringsの5つのアイテム(0 => 1,1 => 2,2 => 3,3 => 4,4 => 5)があり、最後の2つのアイテムのみを登録すると、 ChipListenerコスリストを2つのインデックス(0 => 4,1 => 5)の配列に登録します。ご覧のとおり、配列が整列していない2つの配列があります。 public void chipSelected(int i)に受信されたiは、2番目の配列に相対的ですが、1番目の配列のデータにアクセスするために使用します。

私はこのコードを試してみてください、これが問題であることを確認しないんだけど、私はうまく予測しているなら、私に知らせて:)

:この行を何をすべきか

private String[] mostlyMatchedKeywordsStrings; 
     mostlyMatchedKeywordsStrings = searchResponse.getData().getMostlyMatchedKeywordsText(); 
     cloudChipList.clear(); 
     fullSearchMini_chipCloud.removeAllViews(); 

    for (int i = 0; i < mostlyMatchedKeywordsStrings.length; i++) { 

     cloudChipList.add(mostlyMatchedKeywordsStrings[i]); 

     if (i >= mostlyMatchedKeywordsStrings.length - 2) { 
      fullSearchMini_didYouMeanLay.setVisibility(View.VISIBLE); 
     } else { 
fullSearchMini_didYouMeanLay.setVisibility(View.GONE); 
} 


     fullSearchMini_chipCloud.addChip(mostlyMatchedKeywordsStrings[i]); 
     Log.e("searchKeys", mostlyMatchedKeywordsStrings[i]); 

     fullSearchMini_chipCloud.setChipListener(new ChipListener() { 
      @Override 
      public void chipSelected(int i) { 
       try { 
        Log.e("searchKeys", "new : " + mostlyMatchedKeywordsStrings[i]); 

       } catch (Exception e) { 
       } 
      } 

      @Override 
      public void chipDeselected(int i) { 

      } 
     }); 

    } 
+0

あなたは私の上記のコードで私に真のコードを送ることができますか? –

+0

@ Miss.LiLi編集コード – firegloves

+0

で私にすべてのアイテムを見せてください。私はショーをしたいだけ2項目続けた。すべての項目はありません –

関連する問題