在listview中滚动时切换按钮更改状态

时间:2016-02-29 00:56:23

标签: android listview

我是Android开发的初学者。当我在此列表视图中垂直滚动时会发生此问题。切换按钮自动更改状态。我该如何解决这个问题?代码如下所示:

public class TrainerSessionAdapterView extends BaseAdapter {
public TrainerSessionAdapterView(Context context) {
    this.context = context;
    inflater = LayoutInflater.from(this.context);
    workoutList = new ArrayList<>();
    newSessionList = new ArrayList<>();
    type = context.getResources().getString(R.string.type);
    other = context.getResources().getString(R.string.other);
    objectID = context.getResources().getString(R.string.object_id);
}

   @Override
    public int getCount() {
    if (workoutList.isEmpty()) {
        return 0;
    } else {
        return workoutList.size();
    }

}


@Override
public ParseObject getItem(int position) {
    return workoutList.get(position);
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    removeSession = new ArrayList<String>();
    trainerWorkoutList =  (ArrayList<String>)user.get("trainer_workout_type");
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.session_listview, parent, false);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    if (!workoutList.isEmpty() && position < workoutList.size() && change)  {
            ParseObject workoutObject = workoutList.get(position);
            if (!TextUtils.isEmpty(workoutObject.get(type).toString()));
            holder.sessionTypeView.setText(workoutObject.get(type).toString());
            if(trainerWorkoutList!=null && workoutObject.getObjectId()!=null){
                if (trainerWorkoutList.contains(workoutObject.getObjectId())){
                    holder.sessionSwitch.setChecked(true);
                }
            }
            else{
                holder.sessionSwitch.setChecked(false);
            }
        }  
    return convertView;
}
}

2 个答案:

答案 0 :(得分:1)

在你的getView方法中添加这一行。

  holder.switch.setChecked(ArrayList.get(position).isStudentPresent());

答案 1 :(得分:0)

你可以试试这个,

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        removeSession = new ArrayList<String>();
        trainerWorkoutList =  (ArrayList<String>)user.get("trainer_workout_type");
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.session_listview, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.sessionSwitch.setChecked(false);

        if (!workoutList.isEmpty() && position < workoutList.size() && change)  {
                ParseObject workoutObject = workoutList.get(position);
                if (!TextUtils.isEmpty(workoutObject.get(type).toString()));

                holder.sessionTypeView.setText(workoutObject.get(type).toString());

                if(trainerWorkoutList!=null && workoutObject.getObjectId()!=null){
                    if (trainerWorkoutList.contains(workoutObject.getObjectId())){
                        holder.sessionSwitch.setChecked(true);
                    }
                }
            }  
        return convertView;
    }
}