【问题标题】:For loop in listview列表视图中的for循环
【发布时间】:2016-01-01 03:57:17
【问题描述】:

我有一个使用自定义适配器的 ListView。每行包含 2 个复选框,我想检查它们是否被勾选。

我将如何遍历每一行来检查这一点,然后,如果它们被选中,将行位置的布尔数组更改为 true,然后使用 SharedPreferences 保存这些数组?

在再次打开活动后,我将如何通过查看该位置的数组是真还是假来设置每个 CheckBox? 谢谢。

列表适配器

public class LockerAdapter extends ArrayAdapter<LockerData> {

private Context mContext;
private int mLayoutResourceId;
LockerData[] mData = null;
CheckBox checkBoxHwk;
CheckBox checkBoxLkr;

//trying to change these arrays if boxes are ticked
public boolean[] homeworkCheck = new boolean[6];
public boolean[] lockerCheck = new boolean[6];

public LockerAdapter(Context context, int resource, LockerData[] objects) {
    super(context, resource, objects);
    this.mContext = context;
    this.mLayoutResourceId = resource;
    this.mData = objects;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    LockerHolder holder = null;

    if(row == null){

        LayoutInflater inflater = LayoutInflater.from(mContext);
        row = inflater.inflate(mLayoutResourceId,parent,false);
        holder = new LockerHolder();

        holder.theLessonName = (TextView) row.findViewById(R.id.generalLessonName);
        holder.hwkCheck = (CheckBox) row.findViewById(R.id.homeworkCheck);
        holder.lkrCheck = (CheckBox) row.findViewById(R.id.lockerCheck);

        row.setTag(holder);
    }else {
        holder = (LockerHolder) row.getTag();
    }
    LockerData lesson = mData[position];
    holder.theLessonName.setText(lesson.lessonsName);

    return row;

}

public class LockerHolder{
    TextView theLessonName;
    CheckBox hwkCheck;
    CheckBox lkrCheck;
}

}

活动

public class HomeworkLockerActivity extends Activity {

private ListView hwkListview;
private LockerAdapter lockerAdapter;
private CheckBox checkBoxChem;
private CheckBox checkBoxPhys;
private CheckBox checkBoxMech;
private CheckBox checkBoxFP;
private CheckBox checkBoxCore;
private CheckBox checkBoxStats;

/*LockerData is just a class that takes in a single string in the
constructor to store the data*/
public LockerData[] lessons = {
        new LockerData("Chemistry"),
        new LockerData("Physics"),
        new LockerData("Mechanics"),
        new LockerData("Further Pure"),
        new LockerData("Core 1"),
        new LockerData("Statistics"),
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_homework_locker);

    hwkListview = (ListView) findViewById(R.id.homeworkListview);
    lockerAdapter = new LockerAdapter(getApplicationContext(),R.layout.list,lessons);
    hwkListview.setAdapter(lockerAdapter);

}

}

【问题讨论】:

    标签: android arrays listview for-loop


    【解决方案1】:

    我建议编辑您的 LockerData 类以接受三个输入:姓名(例如:化学)、是否检查作业以及是否检查储物柜。然后,您可以在适配器中编辑 getView() 以覆盖 LockerData 类实例中的布尔值,相应地更新您的复选框。使用这种方法,您的两个列表 homeworkCheck 和 lockerCheck 就不需要了。

    至于共享首选项,我相信您只能存储原始值(例如:布尔值、整数、字符串等)而不能存储列表。每次更新复选框时,我都会使用 onCheckChanged 侦听器来更新您的共享首选项;所以像:

    holder.hwkCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             SharedPreferences.Editor editor = settings.edit();
             //check what kind of button this is, in this case its chemistry
             editor.putBoolean("chemistryCheck", isChecked);
           }
       }
    );    
    

    我会在您的 Activity 的 onCreate 类中检索这些共享偏好,并相应地初始化您的 LockerData[] 课程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-27
      • 1970-01-01
      • 2020-12-25
      • 1970-01-01
      • 1970-01-01
      • 2014-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多