0

我把头撞在墙上,试图解决这个问题一段时间。我不是一个有经验的 Android 人,所以它的解决方案可能非常简单。话虽如此,我有以下代码:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.adminusers);
    super.onCreate(savedInstanceState);

    // set user_type for Viewall
    mUserType = 0;

    lv_adminusersListContent.addFooterView(footerView);
    mUsersAdapter = new AdminUsersAdapter(mUsersList, AdminUsers.this);
    lv_adminusersListContent.setAdapter(mUsersAdapter);

    getUsersList();

}

private void getUsersList() {

    if (UsefulFcts.isOnline(AdminUsers.this)) {

        if (mThread != null && mThread.isAlive()) {
            mThread.interrupt();
        }

        mThread = new Thread() {
            @Override
            public void run() {

                mHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        pDialog = new ProgressDialog(AdminUsers.this);
                        pDialog.setMessage("Loading.. Please wait!");
                        pDialog.setCancelable(false);
                        pDialog.show();
                    }
                });

                mUsersList.clear();
                mUsersList.addAll(mServiceManager.getUsersList(mUserType,
                        0 + "", AdminServiceManager.sHowManyToLoad));
                mArrayLength = mUsersList.size();

                Log.d("arrayLength", String.valueOf(mArrayLength));
                Log.d("total_results",
                        String.valueOf(ParserJSON.total_results));

                mHandler.post(new Runnable() {

                    @Override
                    public void run() {

                        // refresh listview

                        mUsersAdapter.notifyDataSetChanged();

                        if (mArrayLength < ParserJSON.total_results) {

                            lv_adminusersListContent
                                    .addFooterView(footerView);

                        } else if (mArrayLength >= ParserJSON.total_results) {

                            lv_adminusersListContent
                                    .removeFooterView(footerView);
                        }

                        if (0 == mArrayLength) {

                            Toast.makeText(AdminUsers.this,
                                    "No username found!",
                                    Toast.LENGTH_SHORT).show();
                        }

                        pDialog.dismiss();
                    }
                });
            }
        };
        mThread.start();
    } else {
        Toast.makeText(AdminUsers.this, "Internet connection required!",
                Toast.LENGTH_LONG).show();
    }
}

@Override
protected void linkUI() {

    // begin load_more view
    footerView = AdminUsers.this.getLayoutInflater().inflate(
            R.layout.loadmore, null);

    btn_loadmore = (Button) footerView.findViewById(R.id.btn_loadmore);
    // end load_more view

    btn_adminusersViewall = (Button) findViewById(R.id.btn_adminusersViewall);
    btn_adminusersAdmins = (Button) findViewById(R.id.btn_adminusersAdmins);
    btn_adminusersClients = (Button) findViewById(R.id.btn_adminusersClients);
    btn_adminusersDevs = (Button) findViewById(R.id.btn_adminusersDevs);

    btn_adminAdd = (ImageButton) findViewById(R.id.btn_adminAdd);

    lv_adminusersListContent = (ListView) findViewById(R.id.lv_adminusersListContent);
}

@Override
protected void setAction() {

    // begin Buttons UnderHeader

    btn_adminusersViewall.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            UsefulFcts.setButtonClicked(btn_adminusersViewall,
                    btn_adminusersClients, btn_adminusersAdmins,
                    btn_adminusersDevs);

            mUserType = 0;

            mArrayLength = -1;

            lv_adminusersListContent.removeFooterView(footerView);

            getUsersList();

        }
    });

    btn_adminusersClients.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            UsefulFcts.setButtonClicked(btn_adminusersClients,
                    btn_adminusersDevs, btn_adminusersAdmins,
                    btn_adminusersViewall);

            mUserType = 1;

            mArrayLength = -1;

            // view.setVisibility(View.GONE);

            getUsersList();
        }
    });

    btn_adminusersDevs.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            UsefulFcts.setButtonClicked(btn_adminusersDevs,
                    btn_adminusersClients, btn_adminusersAdmins,
                    btn_adminusersViewall);

            mUserType = 2;

            mArrayLength = -1;

            lv_adminusersListContent.removeFooterView(footerView);

            getUsersList();
        }
    });

    btn_adminusersAdmins.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            mUserType = 3;

            mArrayLength = -1;

            lv_adminusersListContent.removeFooterView(footerView);

            getUsersList();
        }
    });

    // end Buttons UnderHeader

    btn_loadmore.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mThread2 = new Thread() {
                @Override
                public void run() {
                    mHandler.post(new Runnable() {

                        @Override
                        public void run() {

                            progressBar = new ProgressDialog(
                                    AdminUsers.this);
                            progressBar
                                    .setMessage("Loading more projects.. ");
                            progressBar.setCancelable(true);
                            progressBar.show();
                        }
                    });

                    mUsersList.addAll(mServiceManager.getUsersList(
                            mUserType, String.valueOf(mArrayLength),
                            AdminServiceManager.sHowManyToLoad));

                    mArrayLength = mUsersList.size();

                    mHandler.post(new Runnable() {

                        @Override
                        public void run() {

                            mUsersAdapter.notifyDataSetChanged();

                            if (mArrayLength >= ParserJSON.total_results) {
                                lv_adminusersListContent.removeFooterView(footerView);
                            }
                            progressBar.dismiss();
                        }
                    });

                };
            };
            mThread2.start();
        }
    });

注意:我基本上有一个列表视图,根据我正在按下的按钮(看起来像一个选项卡)(mUserType = 0/1/2/3)从服务器请求内容。我还使用了加载更多按钮(footerView = 带有按钮的简单布局)。一切似乎都工作得很好,但是在“玩”了几次按钮(随机数!)之后,它崩溃了,并引发了以下异常:

02-21 11:00:49.252: E/AndroidRuntime(11728): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131296461, class android.widget.ListView) with Adapter(class android.widget.HeaderViewListAdapter)]

因此,我回顾了代码,但看不到任何“不在 UI 线程上”的列表视图通知。

1.问题可能是什么?

2.是否有必要使用 2 个线程(mThread 和 mThread2),或者我可以使用同一个线程,因为一个人的工作应该在另一个结束时开始(或者不是吗?)我应该将它们声明为静态吗?

还指出我将来可以使用的任何“良好做法”技巧,或我所做的“错误做法”将不胜感激!

PS。linkUI() 和 setAction() 在 super().onCreate() 中调用。

4

1 回答 1

2

错误消息似乎可以理解:您正在从 mThread 修改适配器:

mUsersList.clear();
mUsersList.addAll(mServiceManager.getUsersList(mUserType,
                    0 + "", AdminServiceManager.sHowManyToLoad));

你可以尝试这样的事情:

            final List newUserList = mServiceManager.getUsersList(mUserType,
                    0 + "", AdminServiceManager.sHowManyToLoad);

            mArrayLength = newUserList.size();

            Log.d("arrayLength", String.valueOf(mArrayLength));
            Log.d("total_results",
                    String.valueOf(ParserJSON.total_results));

            mHandler.post(new Runnable() {

                @Override
                public void run() {
                    mUsersList.clear();
                    mUsersList.addAll(newUserList);
                    // refresh listview

                    mUsersAdapter.notifyDataSetChanged();
于 2013-02-21T09:30:21.687 回答