1

我想要完成的是当有人从设置中返回并更改他们的偏好时刷新我的视图。我认为这会起作用,但事实并非如此。关于我如何做到这一点的任何想法?

    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import com.projectcaruso.naturalfamilyplanning.R;
    import android.annotation.SuppressLint;
    import android.app.DatePickerDialog.OnDateSetListener;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.preference.PreferenceManager;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.DatePicker;
    import android.widget.TextView;

    public class ChartingFragment extends Fragment implements OnDateSetListener {
        SharedPreferences mPreferences;
        Boolean symptothermal;
        Boolean mucus_stamps;
        Boolean fertile_infertile;

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            // TODO Auto-generated method stub
        }    

        public void showDatePickerDialog(View v) {
            DatePickerFragment newFragment = new DatePickerFragment();
            newFragment.show(getChildFragmentManager(), "datePicker");
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            init();
        }           

        private void init() {
            mPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
            symptothermal = mPreferences.getBoolean("symptothermal", true);
            mucus_stamps = mPreferences.getBoolean("mucus_stamps", true);
            fertile_infertile = mPreferences.getBoolean("fertile_infertil", true);
        }

        @SuppressLint("SimpleDateFormat")
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view;

            view = setcontrolvisability(inflater);

            return view;
           }

        @Override
        public void onResume() { 
              super.onResume(); 
              View vg = getView().findViewById(R.id.charting);
              vg.invalidate();
              init();

        }

        private View setcontrolvisability(LayoutInflater inflater) {
            // TODO Auto-generated method stub
            View view = inflater.inflate(R.layout.fragment_charting, null);
            TextView temp;
            if (!symptothermal) {
                    temp = (TextView) view.findViewById(R.id.temp);
                    temp.setVisibility(View.GONE);
                    temp = (TextView) view.findViewById(R.id.tempvalue);
                    temp.setVisibility(View.GONE);
                }

            if (!mucus_stamps) {
                temp = (TextView) view.findViewById(R.id.stamp);
                temp.setVisibility(View.GONE);
                temp = (TextView) view.findViewById(R.id.stampvalue);
                temp.setVisibility(View.GONE);
            }

            if (!fertile_infertile) {
                temp = (TextView) view.findViewById(R.id.fertile);
                temp.setVisibility(View.GONE);
                temp = (TextView) view.findViewById(R.id.fertileswitch);
                temp.setVisibility(View.GONE);
            }

            temp  = (TextView) view.findViewById(R.id.dateselected);
            SimpleDateFormat dfDate_day= new SimpleDateFormat("MM/dd/yyyy");
            String dt="";
            Calendar c = Calendar.getInstance(); 
            dt=dfDate_day.format(c.getTime());
            temp.setText(dt);

            return view;
        }


    }
4

1 回答 1

0

如果子活动(FragmentActivity)已更改,我建议使用 startActivityForResult 并传递一个 Bundle。

1)从你的活动开始偏好活动:

Intent intent = new Intent(this, PreferenceActivity.class);
if (!this.isFinishing())
startActivityForResult(intent, REQUEST_UPDATE);

2)覆盖onActivityResult

@Override
protected void onActivityResult(int reqCode, int resCode, Intent data) {
    super.onActivityResult(reqCode, resCode, data);
    Log.d(TAG, "Received Result: " + reqCode + " / " + resCode);
}

3)在子活动(PreferenceActivity)中监视哪些键已更改,如果您愿意,将其作为捆绑传递

public class PrefsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener,
    OnPreferenceChangeListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       // Load the preferences from an XML resource
       addPreferencesFromResource(R.xml.preferences);
    }

    @Override
    public boolean onPreferenceChange(Preference arg0, Object arg1) {
        getParent().setResult(MainActivity.RESULT_PREFERENCES_CHANGED); // constant
    }

@Override
public void onResume() {
    super.onResume();
    getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onPause() {
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    super.onPause();
}
}
于 2013-12-25T23:04:01.723 回答