具有嵌套if语句的数组列表

时间:2017-04-30 18:24:46

标签: java android arraylist

嘿伙计们我正在尝试使用语音识别创建一个Android程序,并在嵌套if语句中初始化程序时出错。请帮助。

public void onClick(View v) {
    startvoiceRecognition();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
        ArrayList matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        ArrayList program = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        //mlist.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, matches));
        mlist.setAdapter(new ArrayAdapter<MainActivity>(this, android.R.layout.simple_list_item_1, matches));
        //proglist.setAdapter(new ArrayAdapter<MainActivity>(this, android.R.layout.simple_list_item_1, program));
        if (matches.contains("start")){
            start();
        }
        else if (matches.contains("program")){
            program();
            if (matches.contains("cotton")) {
                cotton();
            }
        }
        else if (matches.contains("stop")) {
            stop();
        }

matches.contains("cotton")必须在程序之后执行但不执行,是否可以将开关案例与arraylist一起使用?

P.S-在1周前启动了android studio和java。

1 个答案:

答案 0 :(得分:0)

你的&#34;棉花&#34; if语句是双嵌套的,也许这是你的问题?因为现在要去棉花&#34;声明的一部分,你必须首先满足matches.contains的条件(&#34; program&#34;)。换句话说,你的&#34;棉花&#34;条件实际上相当于需要匹配&#34;棉花&#34;和&#34;程序&#34;

不确定您要完成的是什么,但是一个支架会带来您的&#34;棉花&#34;第二级嵌套声明。即:

public void onClick(View v) {
    startvoiceRecognition();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
        ArrayList matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        ArrayList program = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
        //mlist.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, matches));
        mlist.setAdapter(new ArrayAdapter<MainActivity>(this, android.R.layout.simple_list_item_1, matches));
        //proglist.setAdapter(new ArrayAdapter<MainActivity>(this, android.R.layout.simple_list_item_1, program));
        if (matches.contains("start")){
            start();
        } else if (matches.contains("program")){
            program();
        }
        if (matches.contains("cotton")) {
            cotton();
        }
        }
        else if (matches.contains("stop")) {
            stop();
        }