0

基本的に、共有設定に状態を保存するチェックボックスを備えた拡張可能なリストビューがあります。私が実装している方法は機能しますが、それだけではないと感じています。クラッシュしても驚かない。基本的に私のメソッドでは、シリアル化を使用してアクティビティ クラスの状態を読み込んで保存します。

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 ステートメントがありません。

4

1 に答える 1

0

ArrayListsにはCAPACITYがあり、インスタンスを宣言/初期化するときに明示的に設定できます。容量を指定しない場合、デフォルトが適用されます。

要素の数が容量を超えると、その容量はリセットされます。私はArrayListsを使用していませんが、それらが同様のコレクションとして動作すると仮定すると、容量を超えて大きくなると、容量が大きくなり、新しい容量にスペースが割り当てられ、「古い」小さい値からすべての値がコピーされます。新しいものへのストレージ。

これがコードの速度を低下させていると思います。容量が増えるたびに、これまで以上に大量のデータが繰り返しコピーされます。

検証(および修正)するには:配列リストを初期化してみてください-初期容量の引数として整数をとるコンストラクターを使用して-最終的に取るサイズ、つまりchildren.lengthを使用します

于 2012-09-30T23:10:58.570 に答える