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

Posted

技术标签:

【中文标题】切换按钮的保存和加载状态【英文标题】:Save and load status of toggle button 【发布时间】:2013-06-09 23:46:08 【问题描述】:

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

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】:

为什么不在启动前将 ToggleButton 的状态传递给您的下一个 Activity?或者将其设为全局静态成员,但如果 Activity 被销毁并重新创建,这将无济于事。可能只想将按钮的状态存储在 SharedPreferences 中并在您喜欢的任何地方访问它。

【讨论】:

以上是关于切换按钮的保存和加载状态的主要内容,如果未能解决你的问题,请参考以下文章

保存按钮的背景?

如何保存工具按钮开/关选择的状态?

如何在我的程序中保存一组单选按钮的状态?

Android - 在不同活动之间切换时保存当前的 onCreate 状态

重新加载时保存复选框状态

JQuery1.8 Datatables单击按钮保存状态