Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.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
Java 如何在android中选择上一个微调器时获取微调器项?_Java_Android_Mysql_Android Spinner - Fatal编程技术网

Java 如何在android中选择上一个微调器时获取微调器项?

Java 如何在android中选择上一个微调器时获取微调器项?,java,android,mysql,android-spinner,Java,Android,Mysql,Android Spinner,我的家庭活动中有3个微调器。如果我选择1个其他微调器,则应根据第一个微调器进行更改。我将国家、城市和位置作为微调器,如果我选择国家,则城市和位置应根据该微调器进行更改。到目前为止,我在微调器中设法获取国家项目(数据库中的项目).现在如何在微调器中仅获取选定项..我很困惑。?? 这是我的家庭活动(我从中获得spinner(国家)项目): 这是City.java public class City { private String name; public String ge

我的家庭活动中有3个微调器。如果我选择1个其他微调器,则应根据第一个微调器进行更改。我将国家、城市和位置作为微调器,如果我选择国家,则城市和位置应根据该微调器进行更改。到目前为止,我在微调器中设法获取国家项目(数据库中的项目).现在如何在微调器中仅获取选定项..我很困惑。?? 这是我的家庭活动(我从中获得spinner(国家)项目):

这是City.java

public class City {

    private String name;



    public String getName() {
        return name;
    }



    @Override
    public String toString() {
        return name;
    }
}
这是Location.java

public class Location {

    private String name;



    public String getName() {
        return name;
    }



    @Override
    public String toString() {
        return name;
    }
}
这是Config.java

public class Config {
    //JSON URL
    public static final String DATA_URL = "http://demo5...../get_country.php";

    public static final String DATA_URL1 = "http://demo5...../get_jsoncity.php?id=";

    //Tags used in the JSON String

 public static final String DATA_URL2 = "http://demo5...../get_jsonlocation.php?id=";

    //Tags used in the JSON String
    //JSON array name
    public static final String JSON_ARRAY = "result";

}
这是我的第一个微调器的json:

[
  {
    "id": "1",
    "country": "UAE"
  },
  {
    "id": "2",
    "country": "UK"
  },
  {
    "id": "3",
    "country": "SAUDI ARABIA"
  },
  {
    "id": "4",
    "country": "OMAN"
  },
  {
    "id": "5",
    "country": "BAHRAIN"
  },
  {
    "id": "6",
    "country": "INDIA"
  }
]
如果我选择id=1,这是针对城市的

[
  {
    "id": "1",
    "city_name": "Abu Dhabi"
  },
  {
    "id": "2",
    "city_name": "Dubai"
  },
  {
    "id": "3",
    "city_name": "Sharjah"
  },
  {
    "id": "4",
    "city_name": "Ajman"
  },
  {
    "id": "5",
    "city_name": "Ummal Qwain"
  }
]

我已经获得了第一个微调器项目,即国家,我需要根据第一个微调器的选择获得城市(第二个微调器)和位置(第三个微调器)的项目。

您的国家。班级应该有城市和位置的getter setter

    public class Country {
         private String name;
            private String id;
 private Location loc;
        ArrayList<City> Cityarr = new ArrayList<City>();

        public ArrayList<City> getCityarr() {
            return Cityarr;
        }

        public void setCityarr(ArrayList<City> Citya) {
            this.Cityarr = Citya;
        }

       public Country(String id, String name) {
                this.name = name;
                this.id = id;
            }

            public String getName() {
                return name;
            }

            public String getId() {
                return id;
            }
public Location getLocation() {
        return loc;
    }
       @Override
            public String toString() {
                return name;
            }
        }
公共类国家{
私有字符串名称;
私有字符串id;
私人位置loc;
ArrayList Cityarr=新的ArrayList();
公共阵列列表getCityarr(){
返回城市R;
}
公共无效设置城市(ArrayList Citya){
this.cityar=Citya;
}
公共国家(字符串id、字符串名称){
this.name=名称;
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共字符串getId(){
返回id;
}
公共位置getLocation(){
返回loc;
}
@凌驾
公共字符串toString(){
返回名称;
}
}
和spinner.OnItemSelectedLisenter应嵌套

country.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                        vbopr.get(i).getCityarr().size();

                        cityname.clear();
                        cityname.add("All");
                        for (int j = 0; j < vbopr.get(i).getCityarr().size(); j++) {
                            cityname.add(vbopr.get(i).getCityarr().get(j).getCityname());
                        }
                        try {
                            adpcity.notifyDataSetChanged();
                        } catch (NullPointerException ne) {

                        }
                        adpcity = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_items, cityname);
                        city.setAdapter(adpcity);

                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {

                    }
                });
country.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView AdapterView、View视图、int i、long l){
vbopr.get(i).getcityar().size();
cityname.clear();
城市名称。添加(“全部”);
对于(int j=0;j
我的url包含xml格式的数据 范例


2.
印度
1462
abc
1460
def
1461
jkl工厂
3.
澳大利亚
1462
gdfg工厂
1460
xdfPLANT
1461
dfgPLANT
617
dgvdfgg
试试这个:

spinner_1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                                           int position, long arg3) {

                    if (spinner_1.getSelectedItem().equals("UAE")) {

                        //cal api url here to get data
                        // set adapter to spinner_2 here for "UAE" selected

                    } else if (spinner_1.getSelectedItem().equals("UK")) {
                        //same..
                    }
                }


                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                }
            }
    );
spinner_1.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView arg0、视图arg1、,
整数位置,长arg3){
如果(微调器_1.getSelectedItem().equals(“UAE”)){
//cal api url在此获取数据
//在此处为选定的“UAE”将适配器设置为微调器_2
}else if(微调器_1.getSelectedItem().equals(“UK”)){
//一样。。
}
}
@凌驾
未选择公共无效(AdapterView arg0){
}
}
);

您的意思是说,在选定的一个项目上,我们可以从3个微调器获取所有项目..因为我在config.java中有3个url..我是否只需要生成php文件…我有点困惑,您能告诉我..您有任何形式的数据,但概念是,你必须使它嵌套。但这应该在一个单一的url中,或者我们可以有3个不同的类型。我有数据,但在不同的url中,如何合并它们?我的问题请参阅本教程;是的,我看到了,但我需要3个旋转器如果我选择1个其他2个应该从服务器获取所选项目不是所有项目你可以通过编码告诉我什么可以用我的代码完成…你也帮了我更…我知道你在编码方面很好:)不,我不能这样做。国家名称可以更改。我不能用硬编码名称你做过任何例子吗比如,如果你在微调器中单击“国家”,你将在其他2微调器中获得项目dynamically@z.al是的…但在你的情况下是不同的..我认为如果你想让你的选择动态…你可以把第一个微调器选择的值传递给服务器..在服务器端,你可以为其他微调器返回相应的JSON响应..你可以在一个php中完成脚本…只需传递国家名称,如“阿联酋”,它将返回u的城市,但thts是一个问题,我不知道在未来的客户端可以编辑国家名称…在php文件中,我还需要检查与选定的id名称
country.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                        vbopr.get(i).getCityarr().size();

                        cityname.clear();
                        cityname.add("All");
                        for (int j = 0; j < vbopr.get(i).getCityarr().size(); j++) {
                            cityname.add(vbopr.get(i).getCityarr().get(j).getCityname());
                        }
                        try {
                            adpcity.notifyDataSetChanged();
                        } catch (NullPointerException ne) {

                        }
                        adpcity = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_items, cityname);
                        city.setAdapter(adpcity);

                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {

                    }
                });
<world>
<country>
<countryid>2</countryid>
<countryname>India</countryname>
<city>
<city_id>1462</city_id>
<city_name>abc</city_name>
</city>

<city>
<city_id>1460</city_id>
<city_name>def</city_name>
</city>

<city>
<city_id>1461</city_id>
<city_name>jkl PLANT</city_name>
</city>

</country>

<country>
<countryid>3</countryid>
<countryname>Australia</countryname>
<city>
<city_id>1462</city_id>
<city_name>gdfg PLANT</city_name>
</city>

<city>
<city_id>1460</city_id>
<city_name>xdfPLANT</city_name>
</city>

<city>
<city_id>1461</city_id>
<city_name>dfgPLANT</city_name>
</city>

<city>
<city_id>617</city_id>
<city_name>dgvdfgg</city_name>
</city>
</country>
</world>
spinner_1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                                           int position, long arg3) {

                    if (spinner_1.getSelectedItem().equals("UAE")) {

                        //cal api url here to get data
                        // set adapter to spinner_2 here for "UAE" selected

                    } else if (spinner_1.getSelectedItem().equals("UK")) {
                        //same..
                    }
                }


                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                }
            }
    );