2017-11-03 16 views
0

私は、チェックされたlistviewという簡単なダイアログを実装しています。これは私がこれまで何をやったかである:ダイアログボックスで配列リストをsetMultiChoiceItemsに渡す

CharSequence[] items = {"Brand A", "Brand B", "Brand C"}; 
    AlertDialog.Builder builder = new AlertDialog.Builder(StrengthOfDemandsView.this); 
    builder.setTitle("Select Brands"); 
    final ArrayList seletedItems=new ArrayList(); 

    builder.setMultiChoiceItems(items, null, 
      new DialogInterface.OnMultiChoiceClickListener() { 
       // indexSelected contains the index of item (of which checkbox checked) 
       @Override 
       public void onClick(DialogInterface dialog, int indexSelected, 
            boolean isChecked) { 
        if (isChecked) { 

         seletedItems.add(indexSelected); 
        } else{ 
         seletedItems.remove(Integer.valueOf(indexSelected)); 
        } 
       } 
      }) 

     .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int id) { 

     } 
    }) 
      .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 

       } 
      }); 

    dialog = builder.create(); 
    dialog.show(); 

問題:最初

、私はsetMultiChoiceItemsメソッドに配列を渡すことだし、それが正常に動作しますが、配列の代わりにArrayListを渡す方法?

ArrayList<Products> brandList = new ArrayList<>(); 

私はそれが私にこのエラーが発生しますArrayListsetMultiChoiceItemsにメソッドを渡すためにしようとしているときはいつでも:

Cannot resolve method 'setMultiChoiceItems(java.util.ArrayList<com.application.marketvisit.dataItem.Products>, null, anonymous android.content.DialogInterface.OnMultiChoiceClickListener)' 
+0

新しい配列を文字列で作成してアダプタに追加する –

+0

はい私は今arrayadapterで作業しています。動作する場合はソリューションを投稿します – 3iL

答えて

2

あなたはAlertDialog.Builder#setMultiChoiceItemsString配列を渡す必要があるので、それを文字列として収集し、このような配列

String arr = new String[brandList.size()]; 
for(int i=0 ; i< brandList.size();i++){ 
    arr[i] = brandList.get(i).getProductName(); 
    //getProductName or any suitable method 
} 
+0

ソリューションを実装することで名前を入力しますが、 ArrayListは、チェックされた製品のデータベースIDが必要なためです。しかし、ソリューションをありがとう! – 3iL

+0

POJOリストを渡す方法はありませんが、 'brandList'リストから対応する項目を取り出してidを取得するのに' indexSelected'を使用します。 –

+0

Ohhh!私は最初にその質問をするのはとても愚かでした!ありがとう、私はあなたの答えを受け入れています。 – 3iL

4

。これを試してみてください...そして、それが動作するかどうか私に教えて

ArrayList<String> strBrandList = new ArrayList<String>(); 
    for (int i = 0; i < brandList.size(); i++) { 
      strBrandList.add(brandList.get(i).getProductName()) 
    } 
関連する問題