我有一个ListView,其中每个项目都有一个SeekBar。
当我更新Seekbar的进度时,会将其写入数据库,以便在再次使用该应用程序时重新加载。
不幸的是,如果ListView向上或向下滚动以便任何项目从可见屏幕上消失,那么当重绘它们时,它们将使用旧值绘制。
如何在更新SeekBar时使活动“刷新”,以便ListView后面的值与数据库同步,这应该会停止使用陈旧数据重新绘制SeekBar。 / p>
我已经解决了这个问题是由于底层的ArrayList没有被更新,所以当数据库被写入更新进度时,ArrayList处于与最初传递给构造函数时相同的状态。当我更新底层数据库时,如何重新填充ArrayList?
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
try {
if (view == null) {
LayoutInflater vi = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.task_list_item, null);
}
final Task t = mList.get(position);
if (t != null) {
/* assign layout elements */
final SeekBar seekBar = (SeekBar) view.findViewById(R.id.seek);
final TextView textViewDescription = (TextView) view
.findViewById(R.id.task_description_text_view);
final TextView textViewTaskId = (TextView) view
.findViewById(fyp.proj.R.id.task_id_text_view);
final TextView textViewTaskTitle = (TextView) view
.findViewById(R.id.task_title_text_view);
/* populate layout with task details */
textViewTaskId.setText(String.valueOf(t.getTaskId()));
textViewTaskTitle.setText(t.getTaskTitle());
textViewDescription.setText(t.getTaskDesc());
seekBar.setProgress(t.getProgress());
seekBar.setTag(t.getTaskId());
/* attach listener */
attachProgressUpdatedListener(seekBar);
}
} catch (Exception e) {
Log.i(TaskAdapter.class.toString(), Log.getStackTraceString(e));
}
return view;
}
private void attachProgressUpdatedListener(SeekBar seekBar) {
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
int task_id = (Integer) seekBar.getTag();
// task handler instansiated with zero as parent project id is
// not needed here.
TaskHandler taskHandler = new TaskHandler(DBAdapter
.getDBAdapterInstance(getContext()), 0);
taskHandler.updateTaskProgress(task_id, progress);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// empty as onStartTrackingTouch listener not being used in
// current implementation
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// empty as onProgressChanged listener not being used in
// current implementation
}
});
}
答案 0 :(得分:1)
根据slukian的评论,这个案例的答案是将新的进度值传递给底层的mList。这是解决方案。
private void attachProgressUpdatedListener(SeekBar seekBar,
final int position) {
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
int task_id = (Integer) seekBar.getTag();
// task handler instansiated with zero as parent project id is
// not needed here.
TaskHandler taskHandler = new TaskHandler(DBAdapter
.getDBAdapterInstance(getContext()), 0);
taskHandler.updateTaskProgress(task_id, progress);
// update underlying mList
mList.get(position).setProgress(progress);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// empty as onStartTrackingTouch listener not being used in
// current implementation
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// empty as onProgressChanged listener not being used in
// current implementation
}
});
}
为attachProgressUpdatedListener方法添加了位置。 检索传递位置的ArrayList中的项目并更新progress属性。