如何将Onclick侦听器设置为动态微调器项

时间:2015-09-02 11:04:21

标签: java android

我有三个微调器(1.Vehicle make,2.Vehicle Type,3.Model)。车辆只有选项“2 Wheeler”,“4 Wheeler”。

我做了什么,如果我从车辆选择任何选项只显示那种车型(例如:如果我选择2 Wheeler意味着车型显示Bajaj,Honda等,就像这样)。

我想,如果我从车辆类型中选择任何一个选项,则意味着在第三个微调器中显示该模型的类型。

当第二个微调器被选中时,我不知道如何访问/更改第三个微调器。

请有人帮助我!

我的代码在这里:

public class VehicleFragment extends Fragment {

	Spinner vehicleType;
	Spinner vehicleMake;
	Spinner model;

	String[] vehicleItems = {"Please Choose Vehicle Type","2 Wheeler","4 Wheeler"};
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_layout_vehicle, container,
                false);

        vehicleType = (Spinner)view.findViewById(R.id.veh_typeSpinner);
        vehicleMake = (Spinner)view.findViewById(R.id.makeSpinner);
        model = (Spinner)view.findViewById(R.id.modelSpinner);

		//VehicleType adapter
		ArrayAdapter vehicleAdapter = new ArrayAdapter(this.getActivity(),android.R.layout.simple_spinner_item,vehicleItems);
		vehicleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        vehicleType.setAdapter(vehicleAdapter);
        vehicleType.setOnItemSelectedListener(new VehiclTypeSelectedListener());

        return view;
    }

    public class VehiclTypeSelectedListener implements AdapterView.OnItemSelectedListener {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String item = parent.getItemAtPosition(position).toString();
            if (item == "2 Wheeler"){
                addItemOnVehicleMake();
            }
            else if (item == "4 Wheeler"){
                addItemOnFourVehicleMake();
            }
        }

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

        }


    }

    public void addItemOnVehicleMake(){

        vehicleMake = (Spinner) getActivity().findViewById(R.id.makeSpinner);
        String[] myState = getResources().getStringArray(R.array.twoWheeler_make);
        List<String> makeArrayList = Arrays.asList(myState);
        ArrayAdapter dataAdapter = new ArrayAdapter(this.getActivity(), android.R.layout.simple_spinner_item, makeArrayList);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        vehicleMake.setAdapter(dataAdapter);
    }

    public void addItemOnFourVehicleMake(){

        vehicleMake = (Spinner) getActivity().findViewById(R.id.makeSpinner);
        String[] myState = getResources().getStringArray(R.array.fourWheeler_make);
        List<String> makeArrayList = Arrays.asList(myState);
        ArrayAdapter dataAdapter = new ArrayAdapter(this.getActivity(), android.R.layout.simple_spinner_item, makeArrayList);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        vehicleMake.setAdapter(dataAdapter);
    }


}

提前致谢!!

3 个答案:

答案 0 :(得分:0)

获取布局的子项并检查微调器的实例,然后设置onClick listenet。

答案 1 :(得分:0)

当调用addItemOnVehicleMake()方法时,假设&#34; vehicleMake&#34;斯宾纳有汽车制造公司喜欢,

        Bajaj, 
        Honda,
        .
        . etc,

并且在4-wheeler的addItemOnFourVehicleMake()方法中,

        Honda,
        Ford,
        .
        .. etc.

当填充微调器时,它总是默认设置列表中的第一个值,除非我们手动设置不同的项目位置。

If two wheeler has set Bajaj, get list of models of Bajaj and populate spinner of "VehicleMake" inside addItemOnVehicleMake() for the first item only.

            public void addItemOnVehicleMake()
            {
               ...
               ... 
               // code here to bind model spinner and set default "0" position item.
               model.setSelection(0);
            }

    Also write down the VehicleMake spinner item select listener method,to populate the "VehicleModel" spinner based on selected item in "VehicleMake" spinner.

    vehicleMake.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                            //Here is the code to populate model spinner.
                        }

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

                        }
                    });

答案 2 :(得分:0)

当你选择&#34; 2 Wheeler&#34;时,你需要填充你的微调器。为此,您需要拥有2轮车辆的数据集和单独的车辆数据集,例如4轮车。 所以基本上在你的addItemOnVehicleMake(String s)方法中,将你的项目字符串发送到这个方法:

public void addItemOnFourVehicleMake(String vehicleType){
    if(vehicleType == "4 Wheeler"){
   //add dataset to spinner that shows only 4 wheelers.
}
else if(vehicleType == "2 Wheeler"){
   //add dataset to spinner that shows only 2 wheelers.
}
//and so on....
}