Android R.string没有从strings.xml文件返回默认值

时间:2016-10-13 23:21:58

标签: android android-studio android-fragments

我的R.string.display_preferences_sort_default_value未从strings.xml文件返回正确的值。 strings.xml中的值声明是"流行",但onStart()方法返回的值是" popular.desc"。

在onCreate方法中,我初始化首选项管理器,并在onStart中获取strings.xml中声明的排序顺序

 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setHasOptionsMenu(true);
     prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
     ...
 }

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

    // get sort order to see if it has recently changed
    String prefSortOrder = prefs.getString(
            getString(R.string.display_preferences_sort_order_key),
            getString(R.string.display_preferences_sort_default_value)
    );

    ...
 }

的strings.xml

<string name="display_preferences_sort_order_key">sortType</string>
<string name="display_preferences_sort_default_value">popular</string>

片段类:下面是我的片段类。 sortOrder默认具有值popular.desc

public class MovieListFragment extends Fragment{

private final String LOG_TAG = MovieListFragment.class.getSimpleName();
private final String STORED_MOVIES = "stored_movies";
private SharedPreferences prefs;
private ImageAdapter mMoviePosterAdapter;
String sortOrder;
List<Movie> movies = new ArrayList<Movie>();

public MovieListFragment() {
    setHasOptionsMenu(true);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
    sortOrder = prefs.getString(getActivity().getString(R.string.display_preferences_sort_order_key),
            getActivity().getString(R.string.display_preferences_sort_default_value));

    if(savedInstanceState != null){
        ArrayList<Movie> storedMovies = new ArrayList<Movie>();
        storedMovies = savedInstanceState.<Movie>getParcelableArrayList(STORED_MOVIES);
        movies.clear();
        movies.addAll(storedMovies);
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    return super.onOptionsItemSelected(item);
}

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

    mMoviePosterAdapter = new ImageAdapter(
            getActivity(),
            R.layout.list_item_poster,
            R.id.list_item_poster_imageview,
            new ArrayList<String>());

    View rootView = inflater.inflate(R.layout.fragment_movie_list, container, false);
    GridView gridView = (GridView) rootView.findViewById(R.id.main_movie_grid);
    gridView.setAdapter(mMoviePosterAdapter);

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            Movie details = movies.get(position);
            Intent intent = new Intent(getActivity(), MovieDetailActivity.class)
                    .putExtra("movies_details", details);
            startActivity(intent);
        }

    });

    return rootView;
}

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

    // get sort order to see if it has recently changed
    String prefSortOrder = prefs.getString(getString(R.string.display_preferences_sort_order_key),
            getString(R.string.display_preferences_sort_default_value));

    if(movies.size() > 0 && prefSortOrder.equals(sortOrder)) {
        updatePosterAdapter();
    }else{
        sortOrder = prefSortOrder;
        getMovies();
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    ArrayList<Movie> storedMovies = new ArrayList<Movie>();
    storedMovies.addAll(movies);
    outState.putParcelableArrayList(STORED_MOVIES, storedMovies);
}

private void getMovies() {
    FetchMoviesTask fetchMoviesTask = new FetchMoviesTask(new AsyncResponse() {
        @Override
        public void onTaskCompleted(List<Movie> results) {
            movies.clear();
            movies.addAll(results);
            updatePosterAdapter();
        }
    });
    fetchMoviesTask.execute(sortOrder);
}

// updates the ArrayAdapter of poster images
private void updatePosterAdapter() {
    mMoviePosterAdapter.clear();
    for(Movie movie : movies) {
        mMoviePosterAdapter.add(movie.getPoster());
    }
}

}

2 个答案:

答案 0 :(得分:0)

试试这个:

String prefSortOrder = prefs.getString(getActivity().getString(R.string.display_preferences_sort_order_key),getActivity().getString(R.string.display_preferences_sort_default_value));

参考: Access to getString() in android.support.v4.app.FragmentPagerAdapter?

答案 1 :(得分:0)

先前的首选项值存储在先前版本代码的SharedPreferences中。我卸载并重新安装了包,它运行了