AsyncTask中的IllegalStateException。适配器的内容已更改,但listview未收到通知

时间:2013-11-04 07:35:48

标签: android listview android-listview android-asynctask illegalstateexception

我正在研究TCP套接字。我从服务器每1秒收到一次数据,我需要在ListView的屏幕上显示它。 为此,我使用了AsyncTask。

但我经常收到IllegalStateException错误

enter image description here

我的代码:

Handler handler = new Handler();
Timer timer = new Timer();

TimerTask doAsynchronousTask = new TimerTask() {

    @Override
    public void run() {
        finalizer = new Runnable() {
            public void run() {
                try {
                    if (navBool) {
                        runOnUiThread(new Runnable() {
                            public void run() {
                                new RetriveStock().execute(); // AsyncTask.
                            }
                        });

                    }
                } catch (Exception e) {
                }
            }
        };
        handler.post(finalizer);
    }
}; 
timer.schedule(doAsynchronousTask, 0, 1000);

// AsyncTask类

public class RetriveStock extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        message = client.clientReceive(1); // Here I receive data from server and stores it in "message" string variable.
        printJson(); // FUNCTION WHICH UPDATE VALUES IN 'obj' OBJECT
       runOnUiThread(new Runnable() {

            @Override
            public void run() {
                updateList();// FUNCTION WHICH UPDATE THE LISTVIEW UI.
                adb.notifyDataSetChanged();
            }
        });

        return null;
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    @Override
    protected void onPostExecute(Void result) {

                if (adb != null) {
                    lv.invalidateViews();
                    lv.setAdapter(adb);
                    adb.notifyDataSetChanged();
                    lv.requestLayout();

                }


        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

}

//以JSON更新值的函数。

    public void printJson() {
    try {
        JSONArray jsonArray = new JSONArray(message);

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject json = jsonArray.getJSONObject(i);


            String symbol = json.getString("Symbol_En");
            User obj = new User();
            boolean checkSymbol = false;
            for (int j = 0; j < list.size(); j++) {
                obj = list.get(j);
                if (obj.getSymbol().equalsIgnoreCase(symbol)) {
                    checkSymbol = true;
                    break;
                }
            }
            if (!checkSymbol) {
                obj = new User();
                obj.Symbol_En = json.getString("Symbol_En");
                obj.Symbol_Ar = json.getString("Symbol_Ar");
                obj.AskPrice = json.getString("Ask");
                obj.BidPrice = json.getString("Bid");
                obj.AskQuantity = json.getString("AskQuantity");
                obj.High = json.getString("High");
                obj.Low = json.getString("Low");
                obj.Open = json.getString("Open");
                obj.Close = json.getString("Close");
                obj.PerChange = json.getString("PerChange");
                obj.NetChange = json.getString("NetChange");
                obj.Volume = json.getString("Volume");
                obj.Ltp = json.getString("LTP");
                obj.TimeStamp = json.getString("TimeStamp");
                obj.symbolId = json.getString("Id");

                    list.add(obj);
            } else {

                obj.Symbol_En = json.getString("Symbol_En");
                obj.AskPrice = json.getString("Ask");
                obj.BidPrice = json.getString("Bid");
                obj.High = high + "";
                obj.Low = low + "";
                obj.Open = json.getString("Open");
                obj.Close = json.getString("Close");
                obj.PerChange = json.getString("PerChange");
                obj.NetChange = json.getString("NetChange");
                obj.Volume = json.getString("Volume");
                obj.Ltp = json.getString("LTP");
                obj.TimeStamp = json.getString("TimeStamp");
                obj.symbolId = json.getString("Id");

            }

        }
    } catch (JSONException e1) {
        e1.printStackTrace();
    }
}

//更新LISTVIEW UI的功能。

public void updateList() {
        adb = new ArrayAdapter<User>(DefaultMarketWatch.this,
                R.layout.rssitemview, list) {

            @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) {
                        final TextView title = (TextView) view
                                .findViewById(R.id.symbol);
                        final TextView persend = (TextView) view
                                .findViewById(R.id.persent);
                        final TextView ltp = (TextView) view
                                .findViewById(R.id.ltp);
                        final TextView high = (TextView) view
                                .findViewById(R.id.high);
                        final TextView low = (TextView) view
                                .findViewById(R.id.low);
                        final TextView persendBold = (TextView) view
                                .findViewById(R.id.persent_bold);
                        final TextView persendSup = (TextView) view
                                .findViewById(R.id.persent_sup);

                                ltp.setText(u.getLtp());
                                title.setText(u.getSymbol());
                                high.setText(u.getHigh());
                                low.setText(u.getLow());
                                    persend.setText(u.getPerChange());

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return view;

            }

        };
}

1 个答案:

答案 0 :(得分:3)

你的日志说

The content of the adapter is changed but listview did not receive notification. make sure content of your adapter is not modified from background thread but only from ui thread.

updateList() // FUNCTION WHICH UPDATE THE LISTVIEW UI中有doInBackground。在后台线程上调用doInbackground。你需要在Ui线程上更新ui。

runOnUiThread中使用doInbackground作为活动方法或返回结果,并在onPostExecute更新列表视图