问题描述
当我尝试删除或复制项目时,我的列表视图没有正确更新。 数据正确存储在数据库中,但未正确显示在列表视图上,我认为这是由于回收视图存在问题。
例如,我删除了一个 id 为 87 的项目,然后我复制了一个不同的项目,它在列表视图中显示为 87,即使它应该是 88(并且在数据库中正确存储为 88)。
我尝试查看其他一些带有视图持有者和自定义适配器的代码示例,但我似乎无法弄清楚问题所在。 我是否将 ViewHolder 概念正确应用于我的自定义适配器?
public class WorkoutAdapter extends ArrayAdapter<ExerciseSetModel> {
private Context context;
private ArrayList<ExerciseSetModel> exerciseSetList;
private JSONParser jParser = new JSONParser();
private String isComplete;
private String exerciseId;
public WorkoutAdapter(Context context, ArrayList<ExerciseSetModel> objects) {
super(context, R.layout.exercise_set_item, objects);
this.context = context;
this.exerciseSetList = objects;
isComplete = "";
}
private class ViewHolder{
TextView tvExerciseSetName;
TextView tvExerciseSetDetails;
CheckBox cbIsComplete;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
RelativeLayout view = (RelativeLayout) convertView;
ViewHolder holder = null;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (RelativeLayout) inflater.inflate(R.layout.exercise_set_item, parent, false);
holder = new ViewHolder();
holder.tvExerciseSetName = (TextView) view.findViewById(R.id.exerciseSetName);
holder.tvExerciseSetDetails = (TextView) view.findViewById(R.id.exerciseSetDetails);
holder.cbIsComplete = (CheckBox) view.findViewById(R.id.cbIsComplete);
if (exerciseSetList.get(position).getIsComplete() == 1) {
holder.cbIsComplete.setChecked(true);
isComplete = "1";
}
else {
holder.cbIsComplete.setChecked(false);
isComplete = "0";
}
holder.cbIsComplete.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
exerciseSetList.get(position).setIsComplete(1);
exerciseId = exerciseSetList.get(position).getExerciseId() + "";
isComplete = "1";
} else {
exerciseSetList.get(position).setIsComplete(0);
exerciseId = exerciseSetList.get(position).getExerciseId() + "";
isComplete = "0";
}
new UpdateExerciseComplete().execute();
}
});
holder.tvExerciseSetName.setText(exerciseSetList.get(position).getExerciseName());
holder.tvExerciseSetDetails.setText(exerciseSetList.get(position).getExerciseDetails() + " id: " + exerciseSetList.get(position).getExerciseId());
}
else{
holder = (ViewHolder) view.getTag();
}
return view;
}
class UpdateExerciseComplete extends AsyncTask<String, String, String> {
private String updateIsCompleteUrl;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... args) {
updateIsCompleteUrl = "localhost/android_connect/updateIsComplete.php";
LinkedHashMap<String, String> params = new LinkedHashMap<String, String>();
params.put("is_complete", isComplete);
params.put("exercise_id", exerciseId);
jParser.makePostRequest(updateIsCompleteUrl, params);
return null;
}
protected void onPostExecute(String file_url) {
}
}
}
1楼
用于在onclick()
函数中获得正确的位置。
您必须在视图上标记位置holder.cbIsComplete.setTag(position)
然后使用int position=(Integer)buttonView.getTag()
在onCLick()
获取此标记
2楼
您实施了错误的ViewHolder
模式。
替换修改后的getView
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
RelativeLayout view = (RelativeLayout) convertView;
ViewHolder holder = null;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (RelativeLayout) inflater.inflate(R.layout.exercise_set_item, parent, false);
holder = new ViewHolder();
holder.tvExerciseSetName = (TextView) view.findViewById(R.id.exerciseSetName);
holder.tvExerciseSetDetails = (TextView) view.findViewById(R.id.exerciseSetDetails);
holder.cbIsComplete = (CheckBox) view.findViewById(R.id.cbIsComplete);
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
if (exerciseSetList.get(position).getIsComplete() == 1) {
holder.cbIsComplete.setChecked(true);
isComplete = "1";
}
else {
holder.cbIsComplete.setChecked(false);
isComplete = "0";
}
holder.cbIsComplete.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
exerciseSetList.get(position).setIsComplete(1);
exerciseId = exerciseSetList.get(position).getExerciseId() + "";
isComplete = "1";
} else {
exerciseSetList.get(position).setIsComplete(0);
exerciseId = exerciseSetList.get(position).getExerciseId() + "";
isComplete = "0";
}
new UpdateExerciseComplete().execute();
}
});
holder.tvExerciseSetName.setText(exerciseSetList.get(position).getExerciseName());
holder.tvExerciseSetDetails.setText(exerciseSetList.get(position).getExerciseDetails() + " id: " + exerciseSetList.get(position).getExerciseId());
return view;
}