如果我将其添加到Android函数中,则无法访问Hashmap元素

时间:2018-08-14 06:15:27

标签: java android hashmap

我正在尝试编写一个垂直步进器,其中在每一步中,我都向在所有函数外部声明的哈希映射添加特定值。当我尝试检索添加的值时,它为空。

下面的代码显示了一些将值添加到哈希图中的函数:

private View SelectFilters() {
    LayoutInflater inflater = LayoutInflater.from(getActivity().getBaseContext());
    LinearLayout selectfiltercontent = (LinearLayout) inflater.inflate(R.layout.select_filters_layout, null, false);

    CheckBox eng = (CheckBox)selectfiltercontent.findViewById(R.id.engfilter);
    CheckBox fue = (CheckBox)selectfiltercontent.findViewById(R.id.fuefilter);
    CheckBox oil = (CheckBox)selectfiltercontent.findViewById(R.id.oilfilter);
    CheckBox air = (CheckBox)selectfiltercontent.findViewById(R.id.airfitler);
    CheckBox ac = (CheckBox)selectfiltercontent.findViewById(R.id.acfilter);
    eng.setOnCheckedChangeListener(this);
    fue.setOnCheckedChangeListener(this);
    oil.setOnCheckedChangeListener(this);
    air.setOnCheckedChangeListener(this);
    ac.setOnCheckedChangeListener(this);

    return selectfiltercontent;
}

private View SelectOil() {
    RadioGroup rg = new RadioGroup(getContext());
    final RadioButton Shell5000 = new RadioButton(getActivity().getApplicationContext());
    Shell5000.setId(R.id.Shell5000KM);
    Shell5000.setText(R.string.Shell5000);

    final RadioButton Shell10000 = new RadioButton(getActivity().getApplicationContext());
    Shell10000.setId(R.id.Shell10000KM);
    Shell10000.setText(R.string.Shell10000);

    final RadioButton Mobil15000 = new RadioButton(getActivity().getApplicationContext());
    Mobil15000.setText(getString(R.string.Mobil1_5000));
    Mobil15000.setId(R.id.Mobil5000);
    final RadioButton Mobil110000 = new RadioButton(getActivity().getApplicationContext());
    Mobil110000.setText(R.string.Mobil1_10000);
    Mobil110000.setId(R.id.Mobil10000);
    final RadioButton ACDelco5000 = new RadioButton(getActivity().getApplicationContext());
    ACDelco5000.setText(R.string.ACDelco5000);
    ACDelco5000.setId(R.id.ACDelco5000);

    final RadioButton ACDelco10000 = new RadioButton(getActivity().getApplicationContext());
    ACDelco10000.setText(R.string.ACDelco10000);
    ACDelco10000.setId(R.id.ACDelco10000);
    rg.addView(Shell5000);
    rg.addView(Mobil15000);
    rg.addView(ACDelco5000);
    rg.addView(Shell10000);
    rg.addView(Mobil110000);
    rg.addView(ACDelco10000);
    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch(checkedId){
                case R.id.Shell5000KM:
                    Data.put("OilType",Shell5000.getText().toString());
                    break;
                case R.id.Shell10000KM:
                    Data.put("OilType",Shell10000.getText().toString());
                    break;
                case R.id.Mobil5000:
                    Data.put("Oiltype",Mobil15000.getText().toString());
                    break;
                case R.id.Mobil10000:
                    Data.put("Oiltype",Mobil110000.getText().toString());
                    break;
                case R.id.ACDelco5000:
                    Data.put("OilType",ACDelco5000.getText().toString());
                    break;
                case R.id.ACDelco10000:
                    Data.put("OilType",ACDelco10000.getText().toString());
                    break;
                    default:
                        Data.put("OilType",getString(R.string.Empty));
            }
        }
    });
    return rg;
}

private View SelectTime() {
    TimePicker tp = new TimePicker(getContext());

    tp.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            String hout = String.valueOf(hourOfDay);
            String Minute = String.valueOf(minute);
            String TIME = hout+":"+Minute;
            Data.put("Time",TIME);
            Toast.makeText(getContext(),TIME,Toast.LENGTH_LONG).show();

        }
    });

这是尝试访问它们但失败的地方

private View finalOrdered() {
    LayoutInflater inflater = LayoutInflater.from(getActivity().getBaseContext());
    RelativeLayout finalOrder =
            (RelativeLayout) inflater.inflate(R.layout.order_layout, null, false);
    TextView a = (TextView)finalOrder.findViewById(R.id.SelectedDate);
    a.setText(Order.getCarID());
    return finalOrder;
}

0 个答案:

没有答案