基本的に、共有設定に状態を保存するチェックボックスを備えた拡張可能なリストビューがあります。私が実装している方法は機能しますが、それだけではないと感じています。クラッシュしても驚かない。基本的に私のメソッドでは、シリアル化を使用してアクティビティ クラスの状態を読み込んで保存します。
SharedPreferences settings3 = getSharedPreferences("MYPREFS", 0);
try {
check_states = (ArrayList<ArrayList<Integer>>) ObjectSerializer.deserialize(settings3.getString("CBSTATES", ObjectSerializer.serialize(new ArrayList<ArrayList<Integer>>())));
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
間にいくつかのコード。
protected void onStop() {
SharedPreferences settings3 = getSharedPreferences("MYPREFS", 0);
SharedPreferences.Editor editor3 = settings3.edit();
try {
editor3.putString("CBSTATES", ObjectSerializer.serialize(check_states));
} catch (IOException e) {
e.printStackTrace();
}
editor3.commit();
}
実際の拡張可能なアダプターから配列リストを取得するには、それを静的変数としてインスタンス化して転送します。私のespandablelistadpaterクラスで -->
// set checkbox states
static ArrayList<ArrayList<Integer>> check_states = new ArrayList<ArrayList<Integer>>();
public void setChildrenAndValues() {
//initialize the states to all 0;
for(int i = 0; i < children.length; i++) {
ArrayList<Integer> tmp = new ArrayList<Integer>();
for(int j = 0; j < children[i].length; j++) {
tmp.add(0);
}
check_states.add(tmp);
}
}
そして、ここにgetChildViewがあります
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
LayoutInflater childInflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View childView = childInflate.inflate(R.layout.mtopics_childview, parent, false);
TextView childtxt = (TextView)childView.findViewById(R.id.mtopicschildtv);
childtxt.setText(getChild(groupPosition, childPosition).toString());
// Load the checkbox states
setChildrenAndValues();
final CheckBox childcb = (CheckBox)childView.findViewById(R.id.mtopicchildchkbox);
if (check_states.get(groupPosition).get(childPosition) == 1) {
childcb.setChecked(true);
}else{ childcb.setChecked(false);
}
childcb.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (childcb.isChecked()) {
check_states.get(groupPosition).set(childPosition, 1);
}else{ check_states.get(groupPosition).set(childPosition, 0);
}
}
});
return childView;
私はすべてをうまくやっていたのか、それともステップが欠けていたのか、特に逆シリアル化するときに実際に減速を感じることができるのか知りたかっただけです。おそらく、これを別のスレッドで行う必要があります(それが何を意味するにせよ)。私は正しい方法で静的変数を使用していますか?私はそれらを両方向に変更しています。たぶん、if null ステートメントがありません。