我在片段中有复选框。选中/取消选中它们后,它们会将值保存到 pref_general.xml 。这工作正常。但是当我尝试检索值并以编程方式检查片段中的框时,它会继续返回可空值。我有输出,所以我可以测试实际读取的内容。
基本上,如何让我的片段从我的首选项文件中检索值?从我所看到的是一个上下文问题,但我看不出我做错了什么。谢谢!
checklist1415_2.java
import android.content.Context;
import android.content.SharedPreferences;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
public class checklist1415_2 extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checklist1415_2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
CheckBox myBox;
myBox = (CheckBox) findViewById(R.id.edu1415_3);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
SharedPreferences loadPref = getPreferences(Context.MODE_PRIVATE);
String myedu14153 = loadPref.getString("edu1415_3", "0");
System.out.println("load main pref " + myedu14153);
}
public void onChecklistChecked(View view){
boolean checked = ((CheckBox) view).isChecked();
String checkedItem = getResources().getResourceEntryName(view.getId());
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
String checkVal = "1";
String pref_key = checkedItem;
if(checked){
editor.putString(pref_key, checkVal);
editor.commit();
} else {
editor.putString(pref_key, "-1");
editor.commit();
}
System.out.println(pref_key);
System.out.println(sharedPref.getString("edu1415_3", "0"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_checklist1415_2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public static SharedPreferences myPrefs(Context context){
return context.getSharedPreferences("pref_general", MODE_PRIVATE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View A = inflater.inflate(R.layout.edu1415, container, false);
View B = inflater.inflate(R.layout.employ1415, container, false);
View C = inflater.inflate(R.layout.comm1415, container, false);
CheckBox check3 = (CheckBox) A.findViewById(R.id.edu1415_3);
//if(myPrefs(getContext().getString("edu1415_3", "0")=="1"){
// check3.setChecked(true);
//
// }
System.out.println("frag pref " + getActivity().getSharedPreferences("pref_general", 0).getString("edu1415_3", "0"));
switch (getArguments().getInt(ARG_SECTION_NUMBER)){
case 1:
return A;
case 2:
return B;
case 3:
return C;
}
return null;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Education";
case 1:
return "Employment";
case 2:
return "Community";
}
return null;
}
}
}
pref_general.xml
<ListPreference
android:defaultValue="-1"
android:entries="@array/pref_example_list_titles"
android:entryValues="@array/pref_example_list_values"
android:key="user_age"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:title="@string/pref_title_age" />
<ListPreference
android:defaultValue="-1"
android:key="edu1415_3" />
</PreferenceScreen>