2015-04-28 151 views
0

我从某本书学习Android,并且不断收到.setMultichoiceItem块上的错误:无法解析方法.setMultichoiceItems。 我检查了多次,我的代码全部区分大小写,并且没有拼写错误的单词。无法解析方法“setMultichoiceItem”错误

import android.app.AlertDialog; 
import android.app.Dialog; 

import android.content.DialogInterface; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 
import android.app.Activity; 


public class MainActivity extends ActionBarActivity { 

    CharSequence[] items = {"Google","Safari","Yahoo"}; 
    Boolean[] itemChecked = new Boolean[items.length]; 
    Button btn ; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     btn = (Button) findViewById(R.id.button); 
     btn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       showDialog(0); 


      } 
     }); 
    } 

    protected Dialog onCreateDialog(int i) { 
     switch (i) { 
      case 0: 
       return new AlertDialog.Builder(this) 

         .setTitle("Test of Dialog") 
         .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int which) { 
           Toast.makeText(getApplication(), "OK Clicked !", Toast.LENGTH_LONG).show(); 
          } 
         }) 
         .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int which) { 
           Toast.makeText(getApplicationContext(), "Cancel Clicked !", Toast.LENGTH_LONG).show(); 
          } 
         }) 

          .setMultiChoiceItems(items, itemChecked, 
            new DialogInterface.OnMultiChoiceClickListener() { 
             @Override 
             public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
     Toast.makeText(getApplicationContext(), items[which] + (isChecked ? " checked!" : " unchecked!"), Toast.LENGTH_SHORT).show(); 

             } 

            }).create(); 
     } 
     return null; 
    } 

} 

logcat的错误无法解析方法 'setMultiChoiceItems(java.lang.CharSequence中[],java.lang.Boolean的[],android.content.DialogInterface.OnMultiChoiceClickListener)'

不限帮助会很棒。

感谢

+0

后logcat的错误 – Dilip

+0

无法解析法 'setMultiChoiceItems(java.lang.CharSequence中的[],java.lang.Boolean中的[],android.content.DialogInterface.OnMultiChoiceClickListener)' –

回答

2

尝试这一个,

改变这一行
Boolean[] itemChecked = new Boolean[items.length];

boolean[] itemChecked = new boolean[items.length]; 

,因为它的第二参数接受布尔[],NOT布尔[]对象

setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener) 
+0

是的,它的工作。非常感谢你 。它确实有帮助。非常感谢 。 –

1

进口android.content.DialogInterface.OnMultiChoiceClickListener;

+0

空指令,仍然没有解决错误): –

1

我找到了更好的解决方案:

public void alertMultipleChoiceItems(){ 
    final CharSequence[] dialogList = Symbollist.toArray(new CharSequence[Symbollist.size()]); 
    HashSet<String> uniqueValues = new HashSet<>(Symbollist); 
    Symbollist.clear(); 
    for (String value : uniqueValues) { 
     //... //Do something 
     Symbollist.add(value); 
    } 
    AlertDialog.Builder builder = new AlertDialog.Builder(AddPackageStep3.this); 
    selectedItems = new ArrayList<Integer>(); 
    // set the dialog title 
    boolean[] itemChecked = new boolean[selectedItems.size()]; 

    builder.setMultiChoiceItems(dialogList,null, new DialogInterface.OnMultiChoiceClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which, boolean isChecked) { 

        if (isChecked) { 
         // if the user checked the item, add it to the selected items 
         selectedItems.add(which); 
        } 

        else if (selectedItems.contains(which)) { 
         // else if the item is already in the array, remove it 
         selectedItems.remove(Integer.valueOf(which)); 
        } 

        // you can also add other codes here, 
        // for example a tool tip that gives user an idea of what he is selecting 
        // showToast("Just an example description."); 
       } 

      }) 
      // Set the action buttons 
      .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 

        // user clicked OK, so save the mSelectedItems results somewhere 
        // here we are trying to retrieve the selected items indices 
        String selectedIndex = ""; 
        for(Integer i : selectedItems){ 
         selectedIndex += i + ", "; 
        } 

        //showToast("Selected index: " + selectedIndex); 

       } 
      }) 

      .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 
        // removes the AlertDialog in the screen 
       } 
      }) 
      .show(); 

}