1

我的 CourseFragmentActivity 类扩展了使用带有 5 个选项卡的 ViewPager 的 FragmentActivity。我在每个选项卡中都有一个 ListView,显示 CheckBox 项的 ArrayList。在 ViewPager 的第 5 个选项卡上,我有一个按钮可以导航到另一个 FragmentActivity 类。问题是当我在 CourseFragmentActivity 类中的一个 ListView 中选择一个 CheckBox 项,然后单击按钮导航到另一个 FragmentActivity 类时,当我从另一个导航回我的 CourseFragmentActivity 类时,我失去了 CheckBox 项的状态片段活动类。通过 BaseAdapter 在 2 个 FragmentActivity 之间切换时,如何在 ListView 中保存 CheckBox 项的状态?

下面是我的 BaseAdapter 用于设置我的 ListView 的 CheckBox 项:

package com.usjr.sss.adapter;

import java.util.ArrayList;
import com.usjr.sss.MyConstants;
import com.usjr.sss.R;
import com.usjr.sss.activity.CourseFragmentActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class InfoTechAdapter extends BaseAdapter {

private ArrayList<String> subjectArrayList;
private CourseFragmentActivity activity;
private String subject;

public InfoTechAdapter(CourseFragmentActivity activity,
        ArrayList<String> subjectArrayList) {
    this.activity = activity;
    this.subjectArrayList = subjectArrayList;
}

@Override
public int getCount() {
    if (subjectArrayList != null) {
        return subjectArrayList.size();
    } else {
        return 0;
    }
}

@Override
public String getItem(int position) {
    if (subjectArrayList != null) {
        return subjectArrayList.get(position);
    } else {
        return null;
    }
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.subject_row, null);
        viewHolder = new ViewHolder();
        viewHolder.checkBoxToSend = (CheckBox) convertView
                .findViewById(R.id.subject);
        viewHolder.checkBoxToSend.setText(getItem(position));
        subject = viewHolder.checkBoxToSend.getText().toString();
        /**
         * Missing set checked state of CheckBox item
         */

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }// end if-else (convertView == null)

    viewHolder.ref = position;
    viewHolder.checkBoxToSend.setClickable(true);

    viewHolder.checkBoxToSend.setOnClickListener(new MyClickListener(
            viewHolder));
    viewHolder.checkBoxToSend
            .setOnCheckedChangeListener(new MyCheckChangeListener(
                    viewHolder));

    return convertView;
}// end getView

class ViewHolder {
    private CheckBox checkBoxToSend;
    int ref;
}// end ViewHolder

/**
 * Handle onClick
 */
class MyClickListener implements OnClickListener {

    ViewHolder viewHolder = null;

    public MyClickListener(ViewHolder viHolder) {
        viewHolder = viHolder;
    }

    @Override
    public void onClick(View v) {
        ArrayList<String> arrayListSelectedSubject = MyConstants.ARRAYLIST_SELECTED_SUBJECT;
        String stringNull = String.valueOf(arrayListSelectedSubject);
        String subject = ((CheckBox) v).getText().toString();

        if (((CheckBox) v).isChecked()) {
            /**
             * Check if ARRAYLIST_SELECTED_SUBJECT is null
             */
            if (!stringNull.equals("null")) {
                /**
                 * if ARRAYLIST_SELECTED_SUBJECT is not null, check if
                 * ArrayList does not contain duplicate subject
                 */
                if (!arrayListSelectedSubject.contains(subject)) {
                    arrayListSelectedSubject.add(subject);
                }// end if
                    // (!arrayListSelectedSubject.contains(subject))
            } else {
                /**
                 * if ARRAYLIST_SELECTED_SUBJECT is null, automatically add
                 * subject to ArrayList
                 */
                arrayListSelectedSubject = new ArrayList<String>();
                arrayListSelectedSubject.add(subject);
            }// end if-else (!stringNull.equals("null"))
        }// end if (((CheckBox) v).isChecked())

        if (!((CheckBox) v).isChecked()) {
            arrayListSelectedSubject.remove(arrayListSelectedSubject
                    .indexOf(subject));
        }// end if (!((CheckBox) v).isChecked())

        MyConstants.ARRAYLIST_SELECTED_SUBJECT = arrayListSelectedSubject;
        Log.i("ARRAYLIST_SELECTED_SUBJECT",
                String.valueOf(MyConstants.ARRAYLIST_SELECTED_SUBJECT));

    }// end onClick
}// end MyClickListener

/**
 * Handle CheckBox state
 */
class MyCheckChangeListener implements OnCheckedChangeListener {

    ViewHolder viewHolder = null;

    public MyCheckChangeListener(ViewHolder viHolder) {
        viewHolder = viHolder;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {

        if (viewHolder.checkBoxToSend.equals(buttonView)) {
            if (!isChecked) {
                /**
                 * Missing syntax for saving false state of CheckBox
                 */
                Log.i("onCheckedChanged", "false");
            } else {
                /**
                 * Missing syntax for saving true state of CheckBox
                 */
                Log.i("onCheckedChanged", "true");
            }// end if-else (!isChecked)

        }// end if (viewHolder.checkBoxToSend.equals(buttonView))

    }// end onCheckedChanged
}// end OncheckchangeListner
}
4

1 回答 1

1

我让它与 SharedPreferences 一起工作。

以下是我处理 CheckBox 状态更改的更新代码

class MyCheckChangeListener implements OnCheckedChangeListener {

    ViewHolder viewHolder = null;

    public MyCheckChangeListener(ViewHolder viHolder) {
        viewHolder = viHolder;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {

        String subject = buttonView.getText().toString();

        if (viewHolder.checkBoxToSend.equals(buttonView)) {
            if (!isChecked) {
                /**
                 * Missing syntax for saving false state of CheckBox
                 */
                editor.putBoolean(subject, false);
                editor.commit();
            } else {
                /**
                 * Missing syntax for saving true state of CheckBox
                 */
                editor.putBoolean(subject, true);
                editor.commit();
            }// end if-else (!isChecked)
        }// end if (viewHolder.checkBoxToSend.equals(buttonView))
    }// end onCheckedChanged
}// end OncheckchangeListner

然后我从 BaseAdapter 的 getView() 方法设置 CheckBox 项的状态

String subject = viewHolder.checkBoxToSend.getText().toString();
viewHolder.checkBoxToSend.setChecked(sharedPreferences.getBoolean(
                subject, false));
于 2013-09-28T07:32:42.460 回答