首页 > listview异步刷新问题

listview异步刷新问题

网络获取数据通过listview显示,几轮刷新之后就报异常!总之我写的这个listview有点不稳定,另listview有实现横向和纵向滑动的事件处理,感觉很不流畅,望各位指教,急~~~在线等!!!
public void onCreate(Bundle icicle) {

    // long start = System.currentTimeMillis();
    super.onCreate(icicle);
    setContentView(R.layout.main);
    initTask2();
    handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {

                myDataAdapter.notifyDataSetChanged();
                handledMsg = true;
            }
        }
    };
    refreshTask();
}

private void initTask2() {
    mListView = (HVListView) findViewById(android.R.id.list);
    myDataAdapter = new DataAdapter();
    mListView.setAdapter(myDataAdapter);
}

private void refreshTask() {

    final TimerTask task = new TimerTask() {
        @Override
        public void run() {
            if (!Util.isConnect(mcontext)) {
                Util.showNetExceptionDialog("网络异常", mcontext);
            } else {
                if (handledMsg) {
                    if (thisSortFlag == 0 || changeMarket)
                        GetData.getDataFromWeb(mcontext, thisSortFlag,
                                Configurations.thisSelectedMarketPre,
                                false, false);
                    else
                        GetData.getDataFromWeb(mcontext, thisSortFlag,
                                Configurations.thisSelectedMarketPre,
                                false, true);
                    handledMsg = false;
                    handler.sendEmptyMessage(0); // 向Handler发送消息,更新UI
                }
            }
        }
    };
    java.util.Date time = new java.util.Date();
    timer.schedule(task, time,2000);
}

private class DataAdapter extends BaseAdapter {

    public int getCount() {
        if (GetData.stockInfo != null && GetData.stockInfo.size() > 0) {
            return (GetData.stockInfo.size() + 1);
        } else
            return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.itemlistview, null);
        }
        TextView nameText = ((TextView) convertView
                .findViewById(R.id.item01));
        TextView symbolText = ((TextView) convertView
                .findViewById(R.id.item02));
        TextView newPriceText = ((TextView) convertView
                .findViewById(R.id.item2));
        StockInfo stock = GetData.stockInfo.get(position);
            if (Float.parseFloat(stock.getPriceChangeRatio()) > 0) {
                newPriceText.setTextColor(Color.RED);
        
            } else if (Float.parseFloat(stock.getPriceChangeRatio()) < 0) {
                newPriceText.setTextColor(Color.GREEN);
                
            }
        nameText.setText(stock.getName());
        symbolText.setText(stock.getSymbol());
        newPriceText.setText(stock.getNew_price());        
        // 校正(处理同时上下和左右滚动出现错位情况)
        View child = ((ViewGroup) convertView).getChildAt(1);
        int head = mListView.getHeadScrollX();
        if (child.getScrollX() != head) {
            child.scrollTo(mListView.getHeadScrollX(), 0);
        }
        return convertView;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


找不到你更新数据的代码。看Log,原因是你在非UI Thread对数据源进行了操作。而你的 handleMessage() 只看到了 notifyDataSetChanged()。那么在你更新数据源和 notifyDataSetChanged() 之间,就留出了空隙引发错误。

不妨再检查一下你更新数据源的代码是否运行在UI Thread。如果可以,把更新操作放到 handleMessage(),完成更新操作后立刻调用notifyDataSetChanged()。


参考一下: https://github.com/LuckyJayce/MVCHelper

MVC结构的带下拉刷新上拉加载的一个库。

挺方便。

【热门文章】
【热门文章】