在我的应用程序中,我有一个ListView
包含不同时区的时间。使用 a seekbar
,用户可以更改时区的当前时间,并且其他时区也应该更新它们的时间。
在我的onCreate()
方法中,我ListView
启动了 Time 变量并将其设置为我之前选择的位置的当前时间。另外我设置了OnSeekBarChangeListener
我的seekbar
.
private ArrayList<Welt> welts;
private SimpleArrayAdapter arrayAdapter;
public void onCreate(Bundle savedInstanceState) {
...
arrayAdapter = new SimpleArrayAdapter(this, R.layout.listwelt, welts);
showwelt();
...
now.setToNow();
now.set(now.toMillis(false));
pos = extras.getInt("weltcompare");
now.switchTimezone(welts.get(pos).zone);
...
seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
offset = seekBar.getProgress()*5 - start;
arrayAdapter.notifyDataSetChanged();
//showwelt();
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
offset = seekBar.getProgress()*5 - start;
arrayAdapter.notifyDataSetChanged();
//showwelt();
}
});
...
}
public void showwelt() {
ListView listTest = (ListView)findViewById(R.id.mylistwelt);
listTest.setAdapter(arrayAdapter);
listTest.setDivider(null);
listTest.setDividerHeight(0);
}
最后我的 ArrayAdapter 类:
public class SimpleArrayAdapter extends ArrayAdapter<Welt> {
private final int resource;
public SimpleArrayAdapter(Context context, int resource, ArrayList<Welt> arrays) {
super(context, resource, arrays);
this.resource = resource;
}
public View getView(int position, View convertView, ViewGroup parent) {
Welt cobject = getItem(position);
RelativeLayout listView;
if(convertView==null) {
listView = new RelativeLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi;
vi = (LayoutInflater)getContext().getSystemService(inflater);
vi.inflate(resource, listView, true);
}else{
listView = (RelativeLayout) convertView;
}
TextView tv1 = (TextView)listView.findViewById(R.id.listwelt1);
tv1.setText(cobject.ort);
TextView tv2 = (TextView)listView.findViewById(R.id.listwelt2);
tv2.setText(cobject.land);
TextView tv3 = (TextView)listView.findViewById(R.id.listwelt3);
now.set(base);
now.minute = now.minute + offset;
now.normalize(false);
now.set(now.toMillis(false));
now.switchTimezone(cobject.zone);
tv3.setText(TimeMateActivity.formatDateTime(0, 0, 0, now.hour, now.minute));
TextView tv4 = (TextView)listView.findViewById(R.id.listwelt4);
tv4.setText(TimeMateActivity.formatDateTime(now.monthDay, now.month, now.year, 24, 0));
if(position == pos) {
tv1.setTypeface(null, Typeface.BOLD);
tv2.setTypeface(null, Typeface.BOLD);
tv3.setTypeface(null, Typeface.BOLD);
}
return listView;
}
}
因此,活动运行时唯一更改的数据是变量偏移量。我认为这就是为什么arrayAdapter.notifyDataSetChanged()
不工作的原因,虽然在工作中调用 showwelt() OnSeekBarChangeListener
,但将其重置ListView
为顶部。
我读了几篇文章,但没有一个有帮助。我试图在调用之前清除适配器并再次添加所有项目notifyDataSetChanged()
。我尝试保存列表的当前滚动位置并恢复它。
我应该如何解决我的问题?