如何在搜索栏上保存选择onSaveInstancestate

时间:2014-07-15 12:24:05

标签: android android-seekbar

背景 ::

我正在使用此处显示的DOUBLE ENDED SEEKBAR

我想做什么 ::

  • 我在双端搜索栏中设置值
  • 在方向更改时,我想存储选择并加载数据 在onSaveInstanceState
  • 的帮助下
  • 我如何实现这个目标

FrgMdFilter.java

public class FrgMdFilter extends Fragment {

    //Declaring View Objects references
    private Button btnFilterList;
    private Spinner spinViewBy;
    private TextView txtMinPrice,txtMaxPrice,txtMinDistance,txtMaxDistance,txtMinRating,txtMaxRating;
    private CheckBox chkPrice,chkDistance,chkRating;

    //Declaring Objects Declaration references
    private ArrayList<HashMap<String, String>> objListBufType=null;
    private FragmentTransaction ft;
    private Fragment objFragment=null;

    private static String errMsg=null;
    private static boolean isErr=false;

    //Constructor declaration on type newInstance
    public static FrgMdFilter newInstance() {
        FrgMdFilter fragment = new FrgMdFilter();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) { 

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

        try {
            //For using actionbar menu from fragment
            setHasOptionsMenu(true);
            //For retaining fragment on configuration changes
            setRetainInstance(true);
            //Set the Application Variable to track Which class is currently loaded to the container
            AppController.CURRENT_FRAGMENT=FrgMdFilter.class.getSimpleName();

            //Set the title and sub-title for mobiles having API>=11
            if (android.os.Build.VERSION.SDK_INT >= 11) {
                //Set the title and sub-title for the actionbar
                ActionBar ab = getActivity().getActionBar();
                ab.setTitle(getResources().getString(R.string.app_name));
                ab.setSubtitle(getResources().getString(R.string.FrgMdFilterScreenName));
            }

            objListBufType=new ArrayList<HashMap<String, String>>();

        } catch (Exception e) {
            if(isErr==false){
                errMsg=e.toString();
                isErr=true;
                //Pop the dialog
                DlgUniversalError.showQuitAlert(getActivity(),errMsg);  
            }
        }
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();

        try {
            int minOnlinePrice=Integer.valueOf(txtMinPrice.getText().toString());
            int maxOnlinePrice=Integer.valueOf(txtMaxPrice.getText().toString());


            // create RangeSeekBar as Integer range between 20 and 75
            UtilRangeSeekBar<Integer> seekBarPrice = new UtilRangeSeekBar<Integer>(minOnlinePrice, maxOnlinePrice, getActivity().getApplicationContext());
            seekBarPrice.setOnRangeSeekBarChangeListener(new OnRangeSeekBarChangeListener<Integer>() {


                @Override
                public void onRangeSeekBarValuesChanged(UtilRangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
                    // handle changed range values
                    txtMinPrice.setText(String.valueOf(minValue));
                    txtMaxPrice.setText(String.valueOf(maxValue));

                    if(chkPrice.isChecked()!=true){
                        chkPrice.setChecked(true);
                    }

                }
            });

            ViewGroup layoutPrice = (ViewGroup) getActivity().findViewById(R.id.seekPriceBar);
            layoutPrice.addView(seekBarPrice);

        } catch (Exception e) {
            if(isErr==false){
                errMsg=e.toString();
                isErr=true;
                //Pop the dialog
                DlgUniversalError.showQuitAlert(getActivity(),errMsg);  
            }
        }
    }

    private void initializeSeekBar() throws Exception {
        DatabaseHandler mHelper;
        SQLiteDatabase db = null;
        Cursor mCursor = null;
        try {
            mHelper = new DatabaseHandler(getActivity());
            db = mHelper.getReadableDatabase();
            mCursor = db.rawQuery("select Min(online_price),Max(online_price),Min(rating),Max(rating),Min(distance),Max(distance) " +"from "+ view_buffet.VIEW_NAME_VW_BUFFET, null);
            if (mCursor.getCount()>0) {
                //It is a necessary to move the cursor to the beginning every time we perform the cursor operation
                mCursor.moveToFirst();

                /* Math.floor:: Rounding to low value
                 * Math.ceil:: Rounding to high value*/
                txtMinPrice.setText(String.valueOf(Integer.valueOf((int) Math.floor(Integer.valueOf(mCursor.getString(0))))));
                txtMaxPrice.setText(String.valueOf(Integer.valueOf((int) 
            }
        } catch (Exception e) {
            if(isErr==false){
                errMsg=e.toString();
                isErr=true;
                throw e;
            }
        }finally{
            if(db!=null){
                if(db.isOpen()) db.close();
            }
            if(mCursor!=null){
                if(!mCursor.isClosed())mCursor.close();
            }
        }
    }


    @Override
    public void onCreateOptionsMenu(
            Menu menu, MenuInflater inflater) {

        //Now inflate the menu items required
        inflater.inflate(R.menu.screen_refresh, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {   
        ft = getFragmentManager().beginTransaction();

        switch(item.getItemId()) {

        case R.id.screen_refresh:

            //REFRESH THE FRAGMENT      
            getFragmentManager().beginTransaction().remove(objFragment).commit();
            objFragment = FrgMdFilter.newInstance();
            getFragmentManager().beginTransaction().add(R.id.content_frame, objFragment,"FrgMdFilter").commit();

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

1 个答案:

答案 0 :(得分:0)

根据RangeSeekBar类的源代码尝试获取选定的值 - 使用此方法:

  • getSelectedMinValue()
  • getSelectedMaxValue()

并将这些值放到将要保存的包中:

@Override
protected void onSaveInstanceState(Bundle outState) {
  outState.putInt("min", getSelectedMinValue());
  outState.putInt("max", getSelectedMaxValue());
  super.onSaveInstanceState(outState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  int min = savedInstanceState.getInt("min");
  int max = savedInstanceState.getInt("max");

  //place those value in some field of FrgMdFilter class and use 
  //them when RangeSeekBar is initialized
  //use those methods: setSelectedMinValue(min) and setSelectedMinValue(max)

}

如果我理解正确,您将在方法onStart()中初始化RangeSeekBar。所以尝试比这样:

UtilRangeSeekBar<Integer> seekBarPrice = new UtilRangeSeekBar<Integer>(minOnlinePrice, maxOnlinePrice, getActivity().getApplicationContext());
seekBarPrice.setSelectedMinValue(min) 
seekBarPrice.setSelectedMinValue(max)