【问题标题】:Cannot resolve method "setMultichoiceItem" error无法解决方法“setMultichoiceItem”错误
【发布时间】:2015-04-28 06:46:13
【问题描述】:

我正在从某本书中学习 Android,并且在 .setMultichoiceItem 块上不断出现错误:cannot resolve method .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)'

任何帮助都会很棒。

谢谢

【问题讨论】:

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

标签: java android


【解决方案1】:

试试这个,

更改此行
Boolean[] itemChecked = new Boolean[items.length];

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

因为它的第二个参数接受 boolean[],而不是 Boolean[] 对象

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

【讨论】:

  • 是的,它奏效了。非常感谢你 。它真的有帮助。非常感谢。
【解决方案2】:

导入android.content.DialogInterface.OnMultiChoiceClickListener;

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      您可以使用 Guava 以这种方式转换为 boolean 数组:

        boolean[] listBoolean = Booleans.toArray(checkedList);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-01-09
        • 2018-01-05
        • 2012-01-04
        • 1970-01-01
        • 2014-11-14
        • 1970-01-01
        • 2018-06-05
        相关资源
        最近更新 更多