【发布时间】:2013-11-07 10:44:05
【问题描述】:
我正在研究 TCP socket 和 ListView,它们有 20 个列表项。
我每 5 秒从服务器收到一组数据,我需要在 listview 中显示这些数据。
我的问题是。
当我滚动我的列表并从服务器收到第二组数据时,我需要更新我的列表项,然后我的listview 自动移动到顶部。我需要阻止它。
我的代码正在更新我的列表视图。此代码在 AsyncTask 的 onPostExecute 中。
protected void onPostExecute(ArrayList<User> result) {
adb = new ArrayAdapter<User>(DefaultMarketWatch.this, R.layout.rssitemview, result) {
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
try {
if (null == view) {
LayoutInflater vi = (LayoutInflater) DefaultMarketWatch.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.rssitemview, null);
}
final User u = list.get(position);
if (null != u) {
TextView title = (TextView) view.findViewById(R.id.symbol);
TextView ltp = (TextView) view.findViewById(R.id.ltp);
TextView high = (TextView) view.findViewById(R.id.high);
TextView low = (TextView) view.findViewById(R.id.low);
TextView persendBold = (TextView) view.findViewById(R.id.persent_bold);
persendBold.setText(u.getAskPrice());
ltp.setText(u.getLtp());
title.setText(u.getSymbol());
high.setText(u.getHigh());
low.setText(u.getLow());
}
}
catch (Exception e) {
e.printStackTrace();
}
return view;
}
};
if (adb != null) {
lv.setAdapter(adb);
adb.notifyDataSetChanged();
int currentPos = lv.getFirstVisiblePosition();
lv.setSelection(currentPos);
}
super.onPostExecute(result);
}
【问题讨论】:
-
发布代码如何更新列表视图..
-
发生这种情况是因为每 5 秒更新一次列表......这会重置滚动位置。发布您的代码,以便我们为您提供解决方案..
-
您是否在代码中使用了 setSelection?
-
我已经更新了我的代码。请看。我用 setSelection() 没用。
标签: android listview android-listview timer android-asynctask