0

我正在研究TCP socketListView其中有 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);
}
4

2 回答 2

1

问题是:你喜欢什么样的行为?如果列表项完全改变,显示第一个元素是合理的。但是,如果您只想使用新数据获得与以前相同的位置,则可以执行以下操作:

protected void onPostExecute(ArrayList<User> result) {
    int currentPos = lv.getFirstVisiblePosition();
    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;
        }

    };

     //previous null check completely unnessesary, because you initialized it just before
    lv.setAdapter(adb);
    adb.notifyDataSetChanged();

    lv.setSelection(currentPos);

    super.onPostExecute(result);
}
于 2013-11-07T11:00:32.903 回答
0

我认为问题在于你adapter每次都为你的listView. 我的解决方案是实现一个update在运行时支持列表的自定义数组适配器。

步骤 1) 定义自定义ArrayAdapter如下:

public class MyArrayAdapter extends ArrayAdapter<User>
{
    ArrayList<User> list;

    public MyArrayAdapter (Context context, int textViewResourceId, ArrayList<User> result) {
       super (context, textViewResourceId, objects);
       this.list = result;
    }

    public void updateList(ArrayList<User> updatedList) {
        this.list = updatedList;
    }

    @Override
    public View getView (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;
    }
}

初始化adb如下(在onCreate或开始之前的任何地方AsyncTask

adb = new MyArrayAdapter(DefaultMarketWatch.this, R.layout.rssitemview, new ArrayList<User>());
lv.setAdapter(adb);
adb.notifyDataSetChanged();

您的帖子执行现在只会告诉适配器列表已更新,并调用通知数据集已更改。

protected void onPostExecute(ArrayList<User> result) {
    adb.updateList(result);
    adb.notifyDataSetChanged();
    super.onPostExecute(result);
}
于 2013-11-07T11:42:31.280 回答