Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中使用if-else_Android_String_If Statement - Fatal编程技术网

如何在android中使用if-else

如何在android中使用if-else,android,string,if-statement,Android,String,If Statement,我正在尝试使用if-else方法初始化字符串数组。。但它不起作用。怎么做 if(itemno==2){ String[] values = new String[] { "Category31", "Category32", "Category33", "Category34", "Category35", "Cat

我正在尝试使用if-else方法初始化字符串数组。。但它不起作用。怎么做

if(itemno==2){
        String[] values = new String[] { "Category31", 
                "Category32",
                "Category33",
                "Category34", 
                "Category35", 
                "Category36", 
                "Category37", 
                "Android Example List View" ,
                "daniel",
                "dude",
                "hello",
                "super,","dukker"

               };}

else if(itemno==3){
        String[] values = new String[] { "Category31", 
                "Category32",
                "Category33",
                "Category34", 
                "Category35", 
                "Category36", 
                "Category37", 
                "Android Example List View" ,
                "daniel",
                "dude",
                "hello",
                "super,","dukker"

               };}


    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, values);
    l.setAdapter(adapter); 
if(itemno==2){
字符串[]值=新字符串[]{“类别31”,
“类别32”,
“类别33”,
“类别34”,
“类别35”,
“类别36”,
“类别37”,
“Android示例列表视图”,
“丹尼尔”,
“伙计”,
“你好”,
“超级”,“杜克”
};}
否则如果(itemno==3){
字符串[]值=新字符串[]{“类别31”,
“类别32”,
“类别33”,
“类别34”,
“类别35”,
“类别36”,
“类别37”,
“Android示例列表视图”,
“丹尼尔”,
“伙计”,
“你好”,
“超级”,“杜克”
};}
ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_list_item_1,android.R.id.text1,值);
l、 设置适配器(适配器);

但在适配器中,显示变量不被接受的错误。。“帮助我”

因为如果无法访问,则您的值变量位于括号内
外部请将其声明为类变量,然后在if块中初始化。

您被声明为
local
。因此它只能访问本地,这意味着在
If
块内。它不能在
If
语句之外访问。所以请制作
String[]值
在全局变量处

关于if-else的语法是正确的。若要删除错误,请在if-else语句之外声明“String[]values;”。

您在
if
的块内声明
变量。在您的案例中,变量的范围是从
if
的开始到结束

if(something)
{
   //start of if scope

   String[] values = {"one"};

   //values available here with contents: {"one"}
   //end of if scope
}
else
{
   //start of else scope

   String[] values;

   //values available here - different from the values above. this variable value is null here
   //end of else scope
}
//none of the values declared above are available here. From the compiler's PoW, no values variable has been declared here.

您试图实现的解决方案是将
声明移到
if
else
范围之外:放置
字符串[]值if
之前进行编码,并删除
if
else
块中的
String[]
声明。

您需要在if-else块的范围之外声明String[]值。

您应该查看“java scopes variable”并了解它