我正在从某本书中学习Android并且继续在.setMultichoiceItem块上出错:无法解析方法.setMultichoiceItems 。 我多次检查它,我的代码都是区分大小写的,没有拼写错误的单词。
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)& #39;
任何帮助都会很棒。
由于
答案 0 :(得分:4)
尝试这个,
更改此行
Boolean[] itemChecked = new Boolean[items.length];
到
boolean[] itemChecked = new boolean[items.length];
因为它的第二个参数接受boolean [],而不是Boolean []对象
setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener)
答案 1 :(得分:1)
import android.content.DialogInterface.OnMultiChoiceClickListener;
答案 2 :(得分: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();
}
答案 3 :(得分:0)
您可以使用番石榴以这种方式转换为 boolean 数组:
boolean[] listBoolean = Booleans.toArray(checkedList);