gpt4 book ai didi

android - 如何使用 BaseAdapter 在 ListView 中保存我的 CheckBox 项目的状态

转载 作者:行者123 更新时间:2023-11-30 03:21:30 25 4
gpt4 key购买 nike

我的 CourseFragmentActivity 类扩展了 FragmentActivity,它使用带有 5 个选项卡的 ViewPager。我在每个选项卡中都有一个 ListView,它显示 CheckBox 项目的 ArrayList。在我的 ViewPager 的第 5 个选项卡上,我有一个按钮可以导航到另一个 FragmentActivity 类。问题是当我在 CourseFragmentActivity 类中的一个 ListView 中选择一个 CheckBox 项目,然后单击按钮导航到另一个 FragmentActivity 类时,当我从另一个导航回我的 CourseFragmentActivity 类时,我丢失了 CheckBox 项目的状态 fragment Activity 类。当通过我的 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
}

最佳答案

我在使用 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));

关于android - 如何使用 BaseAdapter 在 ListView 中保存我的 CheckBox 项目的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19053653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com