即使有视图持有者,ListView 项目也不 deleting/copying 正确
ListView item not deleting/copying correctly even with view holder
当我尝试删除或复制项目时,我的列表视图没有正确更新。数据在数据库中正确存储,但在列表视图中显示不正确,我认为这是由于回收视图的问题。
For example, I deleted an item with id 87, then I copied a different item and it showed up in the listview as having an id of 87 even though it should have been 88 (and was correctly stored in the db as 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) {
}
}
}
为了在 onclick()
函数中获得正确的位置。您必须在视图 holder.cbIsComplete.setTag(position)
上标记位置,然后使用 int position=(Integer)buttonView.getTag()
在 onCLick()
中获取此标记
您实施了错误的 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;
}
当我尝试删除或复制项目时,我的列表视图没有正确更新。数据在数据库中正确存储,但在列表视图中显示不正确,我认为这是由于回收视图的问题。
For example, I deleted an item with id 87, then I copied a different item and it showed up in the listview as having an id of 87 even though it should have been 88 (and was correctly stored in the db as 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) {
}
}
}
为了在 onclick()
函数中获得正确的位置。您必须在视图 holder.cbIsComplete.setTag(position)
上标记位置,然后使用 int position=(Integer)buttonView.getTag()
onCLick()
中获取此标记
您实施了错误的 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;
}