【发布时间】:2017-11-21 09:23:45
【问题描述】:
我有一个带有适配器的自定义列表视图的活动。我通过选中复选框来选择许多名称。当按下 OK 按钮时,我将选择的值返回到上一个活动。重新打开活动时,复选框中已选择的值不会再次被重新选中。如何解决这个问题? 代码
public class MultiselectAdapter extends ArrayAdapter<String> implements CompoundButton.OnCheckedChangeListener{
private final LayoutInflater mInflater;
private final Context mContext;
private final List<TeamModel> items = null;
private final int mResource;
SparseBooleanArray mCheckStates;
public String[] teamIDValues;
ArrayList<Object> ListObject;
public MultiselectAdapter(@NonNull Context context, @LayoutRes int resource,
@NonNull ArrayList objects ){
super(context, resource, 0, objects);
mContext = context;
mInflater = LayoutInflater.from(context);
mResource = resource;
ListObject = objects;
mCheckStates = new SparseBooleanArray(ListObject.size());
System.out.println("teh size vaooo --"+mCheckStates.size());
System.out.println("teh objects vaooo --"+objects.size());
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
mCheckStates.put((Integer) compoundButton.getTag(), isChecked);
}
static class ViewHolder {
protected TextView name;
protected TextView id;
protected CheckBox check;
}
@Override
public @NonNull View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(mResource, null);
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.id = (TextView) convertView.findViewById(R.id.idval);
holder.check = (CheckBox) convertView.findViewById(R.id.choseboxes);
convertView.setTag(holder);
convertView.setTag(R.id.name, holder.name);
convertView.setTag(R.id.idval, holder.id);
convertView.setTag(R.id.choseboxes, holder.check);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.check.setTag(position);
holder.check.setChecked(mCheckStates.get(position, false));
holder.check.setOnCheckedChangeListener(this);
if(ListObject.get(position) instanceof TeamModel.Data)
{
// System.out.println("teh valus are--"+((TeamModel.Data) ListObject.get(position)).getDescription());
holder.name.setText(((TeamModel.Data) ListObject.get(position)).getDescription());
holder.id.setText(Integer.toString(((TeamModel.Data) ListObject.get(position)).getId()));
}
else if(ListObject.get(position) instanceof TeamModel.GroupData)
{
// System.out.println("teh valus are-geroup name--"+((TeamModel.GroupData) ListObject.get(position)).getCceTeamName());
holder.name.setText(((TeamModel.GroupData) ListObject.get(position)).getCceTeamName());
holder.id.setText(Integer.toString(((TeamModel.GroupData) ListObject.get(position)).getId()));
}
// holder.check.setChecked(items.get(position).isSelected());
return convertView;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
}
【问题讨论】:
-
您必须保存实例。试试这个例子:gist.github.com/curioustechizen/6ed0981b013f63236f0b
-
首先您必须通过调用
startActivityForResult()来调用选择活动,然后在按下确定按钮后将所有选定的值传递给setResult()中的前一个活动。下次将数据传入 Intent 以启动选择活动。 -
您需要管理保存实例或存储您的状态,以便在 onCreate() 或 onResume() 中都可以调用它们!检查此链接:developer.android.com/guide/components/activities/…
-
@DheerubhaiBansal 好的。如何在列表中传递选定的值。现在我已经得到了在列表视图中填充的列表。如何在整体加载列表中传递选定的值并重新检查一些值?请看我的代码
-
@MarcGV Intent intent = new Intent(PreScheduleActivity.this,TeamSelectActivity.class); startActivityForResult(intent, 1);我可以在这里传递选定的值。但我不知道如何在加载列表中传递选定的值以在适配器中重新检查它。请帮帮我
标签: android android-arrayadapter android-adapter android-recyclerview