【Android筆記-異常-4】定義一個臨時的數組變量承接數據,ListView的數據以及通知數據更新要放到同一個線程(主線程)。避免出現異常"The content of the adapter

定義一個臨時的數組變量承接數據,ListView的數據以及通知數據更新要放到同一個線程(主線程)。避免出現異常"The content of the adapter has changed but ListView did not receive a notification"




public class WordFragment extends BaseFragment implements View.OnClickListener, AdapterView.OnItemClickListener, PullToRefreshBase.OnRefreshListener2 {

    private List<Word> dataList = new ArrayList<>();
    private List<Word> dataListTemp = new ArrayList<>(); // 定義一個臨時的數組變量承接數據,ListView的數據以及通知數據更新要放到同一個線程(主線程)。避免出現異常"The content of the adapter has changed but ListView did not receive a notification"。
    private int pageIndex = 1;
    private int pageSize = 150;
    private final int MESSAGE_GET_LIST_DATA = 0;
    private String userID;
    private int type = 0;
    
    
    // 信息接收處理-成功
    @Override
    public void handlerAPIRequestSucceed(Message msg) {
        switch (msg.what) {
            case MESSAGE_GET_LIST_DATA:
                handle_getListData(msg);
                break;
            default:
                break;
        }
    }

    // 事件------------------------------開始

    private void getListData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String response = api_word.list(String.valueOf(pageIndex), String.valueOf(pageSize), String.valueOf(type));
                try {
                    JSONArray ja = new JSONArray(response);
                    Word word;
                    dataListTemp.clear();
                    for (int i = 0; i < ja.length(); i++) {
                        JSONObject jo = ja.getJSONObject(i); // 轉換成JSONObject對象
                        word = new Word(
                                jo.getString("id"),
                                jo.getString("query"),
                                jo.getString("speakUrl"),
                                jo.getString("translation"),
                                jo.getString("phonetic"),
                                jo.getString("ukPhonetic"),
                                jo.getString("usPhonetic"));
                        dataListTemp.add(word);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                // 在子線程中將Message對象發出去
                Message message = new Message();
                message.what = MESSAGE_GET_LIST_DATA;
                message.obj = null; //
                handler.sendMessage(message);
            }
        }).start();
    }
    // 事件------------------------------結束

    // 處理------------------------------開始
    // 返回結果處理-獲取列表數據
    private void handle_getListData(Message msg) {
        // 收起下拉控件的等待圖標
        listView.onRefreshComplete();
        if (1 == pageIndex) {
            // 如果是第一頁(即刷新),則清空原有數據
            dataList.clear();
        }
        dataList.addAll(dataListTemp);
        adapter.notifyDataSetChanged();
    }
    // 處理------------------------------結束
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章