保存并加载切换按钮的状态

时间:2013-06-09 23:46:08

标签: android button load save toggle

我正在尝试保存切换按钮的状态,我已动态创建。我无法保存当前状态。如果我在应用程序中更改活动然后返回切换按钮的状态又恢复为默认状态。这是我的代码。有人能帮我吗?

private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>>
{


    public CustomAdapter(Context context, int textViewResourceId, 
            ArrayList<HashMap<String, Object>> Strings){
        super(context,textViewResourceId, Strings);
    }

    List<Paper> papers_list = BetterActivity.masterConference.getPapers();


    private class ViewHolder
    {
        TextView paperTitle, author;
    }



    ViewHolder viewHolder;



    public View getView(final int position, View convertView, final ViewGroup parent){


        if(convertView == null)
        {
            convertView = inflater.inflate(R.layout.paper_row_item, null);
            viewHolder=new ViewHolder();

            viewHolder.paperTitle=(TextView) convertView.findViewById(R.id.paperTitle);
            viewHolder.author=(TextView) convertView.findViewById(R.id.author);
            addFavorite=(ToggleButton) convertView.findViewById(R.id.ToggleButton01);

            convertView.setTag(viewHolder);
        }
        else
            viewHolder = (ViewHolder) convertView.getTag();

        viewHolder.paperTitle.setText(searchResults.get(position).get("title").toString());
        viewHolder.author.setText(searchResults.get(position).get("author").toString());

        LoadPreferences();

        final List<Paper> pape = masterConference.getUser().getFavoritePapers();

        addFavorite.setOnClickListener(new View.OnClickListener() {

            Paper p = papers_list.get(position);
            @Override
            public void onClick(View v) {

                boolean on = ((ToggleButton) v).isChecked();
                System.out.println(parent.getChildAt(position).getId() + " " + position);
                SharedPreferences preference = getPreferences(MODE_PRIVATE);
                if(on)
                {
                    masterConference.addPaperToFavorites(p);
                    SharedPreferences.Editor editor = preference.edit();
                    editor.putBoolean(p.getTitle(),true);
                    editor.commit();
                    //SavePreferences(p.getTitle(), position);
                    Toast.makeText(getBaseContext(), "esta verde, " + p.getTitle() + "size " + pape.size(), Toast.LENGTH_SHORT).show();
                }
                else
                {
                    masterConference.removePaperFromFavorites(p);
                    SharedPreferences.Editor editor = preference.edit();
                    editor.putBoolean(p.getTitle(),false);
                    editor.commit();
                    Toast.makeText(getBaseContext(), "esta branco", Toast.LENGTH_SHORT).show();
                }
                // TODO Auto-generated method stub

            }
        });

        return convertView;

    }
}  
private void LoadPreferences(){
    SharedPreferences preferences = getPreferences(MODE_PRIVATE);

    for(int i = 0; i < papers_list.size(); i++)
    {
    String epa = papers_list.get(i).getTitle();
    boolean teste = preferences.getBoolean(epa, false);

    if(teste == true)
        addFavorite.setChecked(true);
    else
        addFavorite.setChecked(false);
    }

}
}

有人能告诉我什么是错的吗?我保存并加载,但这没有任何作用。

1 个答案:

答案 0 :(得分:0)

为什么不在启动之前将ToggleButton的状态传递给下一个Activity?或者使它成为一个全局静态成员,但如果活动被销毁并重新创建,这将没有多大帮助。可能只想在SharedPreferences中存储按钮的状态,并在任何你喜欢的地方访问它。