错误:需要常量表达式

时间:2018-01-04 23:19:24

标签: java android-studio

当我点击其中一个“字符串”名称时,我想打开一个新活动,例如,点击“pila 12v”并打开新活动,在这种情况下打开“Main5Activity”。

当放置“案例值[0]”时,会出现一条消息:需要持续表达。

    package com.example.epson.mp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


AutoCompleteTextView values;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final String[] values = new String[]{"bateria 9v", "pila 9v", "bateria 12v", "pila 12v"};
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,values);
        AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtMercancias);
        textView.setThreshold(3);//will start working from third character
        textView.setAdapter(adapter); //setting the adapter data into the AutoCompleteTextView
        textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LinearLayout ll = (LinearLayout) view;
                //This will give you the string value of selected list item
                TextView listItem = (TextView) ll.getChildAt(0);
                //You can do this or apply own logic to find the selected value case
                switch (listItem.getText().toString()){
                    case values[0]:
                        Intent myintent0 = new Intent(view.getContext(), Main2Activity.class);
                        startActivityForResult(myintent0, 0);
                        break;
                    case values[1]:
                        Intent myintent1 = new Intent(view.getContext(), Main3Activity.class);
                        startActivityForResult(myintent1, 1);
                        break;
                    case values[2]:
                        Intent myintent2 = new Intent(view.getContext(), Main4Activity.class);
                        startActivityForResult(myintent2, 2);
                        break;
                    case values[3]:
                        Intent myintent3 = new Intent(view.getContext(), Main5Activity.class);
                        startActivityForResult(myintent3, 3);
                        break;
                }
            }
        });


    }
}

0 个答案:

没有答案