通过Android中的片段保存复选框

时间:2013-12-07 15:29:05

标签: android checkbox android-fragments

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

一些屏幕截图显示显示:

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;
}

因此,每个复选框都没有标识符。这是我能解决的问题吗?

非常感谢!

2 个答案:

答案 0 :(得分:1)

你可以用这个

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

答案 1 :(得分:0)

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