当我尝试删除或复制项目时,我的列表视图无法正确更新。数据正确存储在数据库中,但在listview上无法正确显示,我认为这是由于回收视图的问题。
例如,我删除了一个id为87的项目,然后我复制了一个不同的项目,它在listview中显示为id为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) {
}
}
}
答案 0 :(得分:0)
在onclick()
函数中获取正确的位置。您必须在视图holder.cbIsComplete.setTag(position)
上标记位置,然后使用onCLick()
int position=(Integer)buttonView.getTag()
内获取此标记
答案 1 :(得分:0)
您实施了错误的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;
}