【问题标题】:Save checkboxes states through fragments in Android在Android中通过片段保存复选框状态
【发布时间】:2013-12-24 22:27:04
【问题描述】:

我目前正在开发一个带有两个列表(在不同片段中)和复选框的应用程序。

几张截图展示给你看:

Talks Screens

Papers Screens

现在我希望能够

1) 使用 «save» 按钮保存所有复选框状态(并使用 «load» 按钮加载它们)

2) “链接”复选框,这样如果我在会谈屏幕中点击一个复选框,它也会在论文屏幕中被点击。

我知道我可以通过使用 SharedPreferences 来保存这些,但它们是使用适配器构建的,所以我所拥有的只是

    @Override
public View getView(int position, View view, ViewGroup viewGroup) {

    // We only create the view if its needed
    if (view == null) {
        view = inflater.inflate(R.layout.child_row, null);

        // Set the click listener for the checkbox
        view.findViewById(R.id.check1).setOnClickListener(this);
    }

    Paper p = (Paper) getItem(position);

    // Set the example text and the state of the checkbox
    CheckBox cb = (CheckBox) view.findViewById(R.id.check1);
    //cb.setChecked(p.isSelected());
    // We tag the data object to retrieve it on the click listener.

    TextView paper = (TextView)view.findViewById(R.id.papername);
    if (paper != null)
        paper.setText(p.getTitle());

    TextView author = (TextView)view.findViewById(R.id.authorname);
    if( author!= null )
        author.setText( p.getAuthor() );

    return view;
}

所以每个复选框都没有标识符。这是我可以简单处理的问题吗?

非常感谢!

【问题讨论】:

    标签: android checkbox android-fragments


    【解决方案1】:

    你可以用这个

    SharedPreferences settings = getSharedPreferences("syllabus", 0);
    Boolean isChecked = settings.getBoolean("cbx1_ischecked", false);
    checkbox1.setChecked(isChecked );
    

    【讨论】:

    • 感谢您的帮助!我可以在哪里使用这个?在我的适配器中?还是片段?
    • 在您的适配器中使用它,因为您只能在适配器中获取复选框的状态。
    • 从activity或fragment中的共享首选项中获取数据,然后设置为adapter 不要在adapter中获取!
    【解决方案2】:

    在页面开头Fragment tutorial U 可以得到重要的概念。
    您也可以使用 getActivity().findviewbyId(...) 或 getView().findviewbyId(...) 引用任何 UI 组件
    跳到有用的地方

    【讨论】:

      猜你喜欢
      • 2013-04-04
      • 2014-04-25
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 2012-04-18
      相关资源
      最近更新 更多