Android 安卓:内容适配器';不能从UI线程修改

Android 安卓:内容适配器';不能从UI线程修改,android,Android,有时我在下面的活动中收到此错误,有时没有: 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. 但我不确定我在班上的错误在哪里。有人知道吗 public clas

有时我在下面的活动中收到此错误,有时没有:

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. 
但我不确定我在班上的错误在哪里。有人知道吗

public class FavoriteActivity extends SpeakSuperActivity {

        private final static String TAG = FavoriteActivity.class.getSimpleName();

        private Button btn_filter_topic, btn_filter_rating, btn_filter_none;
        private TextView fav_filter_text;
        private static ListView listViewFavorites;
        private static TextView txtNoFavoritesYet;
        private List<Favorite> currentFavorites;
        private ArrayAdapter<Favorite> currentFavoritesArrayAdapter;

        // required for list loading piece by piece
        final int itemsPerLoading = Configuration.LOADED_ITEMS_ON_LIST_AT_ONCE;
        boolean loadingMore = false;
        private List<Long> idList;
        int currentDataLoaded;

        private static int oldBtnViewId = 0;

        // set the start value as same as the loading value
        int maximumDataLoadedYet = Configuration.LOADED_ITEMS_ON_LIST_AT_ONCE;

        // 0 = not sorted, 1 = sorted by topic and minimum number of stars
        private int caseSelection = 0;

        private static View progressView;

        private View footerView;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.favorites);
            Log.d(TAG, "FavoritesScreen onCreate()...");

            // indicator for waiting processes
            progressView = UIUtils.addBlockingProgressIndicatorBlack(this);

            // init Listview
            listViewFavorites = (ListView) findViewById(R.id.fav_listview_favorites);

            // add the footer before adding the adapter, else the footer
            // will not load!
            footerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listviewfooter, null, false);
            listViewFavorites.addFooterView(footerView);

            listViewFavorites = (ListView) findViewById(R.id.fav_listview_favorites);

            fav_filter_text = (TextView) findViewById(R.id.fav_filter_text);

            btn_filter_none = (Button) findViewById(R.id.btn_fav_filter_none);
            btn_filter_topic = (Button) findViewById(R.id.btn_fav_filter_topic);
            btn_filter_rating = (Button) findViewById(R.id.btn_fav_filter_rating);

            toggleButtonStates(R.id.btn_fav_filter_none);

            LoadDataTask ldTask = new LoadDataTask();
            ldTask.execute();

            // no favorites yet?
            txtNoFavoritesYet = (TextView) findViewById(R.id.fav_no_favorites_yet);

            updateUI();

        }

        /**
         * An asynchronous Task (doesn't block the UI Thread) for loading the Data in background.
         * 
         * @author Jonas Soukup
         */
        private class LoadDataTask extends AsyncTask<Void, Void, LoudmouthException> {

            private final String TAG = LoadDataTask.class.getName();

            protected void onPreExecute() {
                super.onPreExecute();

                if (FavoriteProvider.getInstance().getNumOfFavorites() != 0)
                    progressView.setVisibility(View.VISIBLE);
                else
                    progressView.setVisibility(View.GONE);

                listViewFavorites.setVisibility(View.GONE);

                fav_filter_text.setVisibility(View.GONE);

                btn_filter_none.setVisibility(View.GONE);
                btn_filter_topic.setVisibility(View.GONE);
                btn_filter_rating.setVisibility(View.GONE);
            }

            protected LoudmouthException doInBackground(Void... params) {
                LoudmouthException exception = null;

                Log.d(TAG, "loading data..");
                switch (caseSelection) {
                case 0:
                    // Get FavoriteList without sorting
                    idList = FavoriteProvider.getInstance().getFavoritesByDate();
                    break;
                case 1:
                    // Get FavoriteList sorted by
                    // Topics + amount of stars
                    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

                    float minRating = prefs.getFloat(getResources().getString(R.string.rating_filter_star_amount), 0);

                    idList = FavoriteProvider.getInstance().getFavoritesByTopicAndMinRating(minRating);
                    break;
                default:
                    Log.e(TAG, "No Case with number: " + caseSelection);

                }

                // reset data loaded, so it loads till maximumDataLoadedYet on a
                // refresh of the list
                currentDataLoaded = 0;

                // reset List on Data change
                currentFavorites = new ArrayList<Favorite>();

                Log.d(TAG, "..loading data finished");

                return exception;
            }

            protected void onPostExecute(LoudmouthException result) {

                try {
                    Log.d(TAG, "LoadDataTask.onPostExecute()");
                    super.onPostExecute(result);
                    progressView.setVisibility(View.GONE);
                    if (result != null) {
                        // Error ocurred during loading
                        android.content.DialogInterface.OnClickListener retryClickListener = new android.content.DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int which) {
                                new LoadDataTask().execute();

                            }
                        };
                        UIUtils.showRetryCancelAlertDialog(getApplicationContext(), result, retryClickListener, null);
                    } else {
                        // Everythings fine, data loaded

                        // showing & hiding

                        if (FavoriteProvider.getInstance().getNumOfFavorites() == 0) {

                            fav_filter_text.setVisibility(View.GONE);
                            btn_filter_none.setVisibility(View.GONE);
                            btn_filter_topic.setVisibility(View.GONE);
                            btn_filter_rating.setVisibility(View.GONE);
                        } else {

                            fav_filter_text.setVisibility(View.VISIBLE);
                            btn_filter_none.setVisibility(View.VISIBLE);
                            btn_filter_topic.setVisibility(View.VISIBLE);
                            btn_filter_rating.setVisibility(View.VISIBLE);
                        }

                        if (idList.size() == 0) {
                            txtNoFavoritesYet.setVisibility(View.VISIBLE);
                            listViewFavorites.setVisibility(View.GONE);
                        } else {
                            txtNoFavoritesYet.setVisibility(View.GONE);
                            listViewFavorites.setVisibility(View.VISIBLE);

                            runOnUiThread(new Runnable() {
                                public void run() {
                                    btn_filter_none.setOnClickListener(new OnClickListener() {
                                        public void onClick(View v) {
                                            caseSelection = 0;
                                            FavoriteProvider.getInstance().setCurrentFavoriteListStateDirty(true);
                                            toggleButtonStates(v.getId());
                                            updateUI();
                                        }
                                    });

                                    btn_filter_topic.setOnClickListener(new OnClickListener() {
                                        public void onClick(View v) {
                                            caseSelection = 1;
                                            TopicFilterFavDialog tfFavDialog = new TopicFilterFavDialog(FavoriteActivity.this, FavoriteActivity.this, v
                                                    .getId());
                                            tfFavDialog.show();
                                        }
                                    });

                                    btn_filter_rating.setOnClickListener(new OnClickListener() {
                                        public void onClick(View v) {
                                            caseSelection = 1;
                                            RatingFilterFavDialog ratDialog = new RatingFilterFavDialog(FavoriteActivity.this, FavoriteActivity.this, v
                                                    .getId());
                                            ratDialog.show();
                                        }
                                    });
                                }
                            });
                        }
                    }

                    // init listview displaying with data loaded step by step
                    currentFavoritesArrayAdapter = new FavoriteArrayAdapter(FavoriteActivity.this, FavoriteActivity.this, R.layout.favorite_list_entry,
                            currentFavorites);
                    listViewFavorites.setAdapter(currentFavoritesArrayAdapter);
                    currentFavoritesArrayAdapter.notifyDataSetChanged();


                } catch (Exception exception) {
                    // silent catch because activity could be closed meanwhile
                    Log.i(TAG, "silent exception catch in onPostExecute: " + exception.getMessage());
                }
            }
        }

        /**
         * Update UI
         */
        public void updateUI() {
            LoadDataTask ldTask = new LoadDataTask();
            ldTask.execute();
            if (currentFavoritesArrayAdapter != null)
                currentFavoritesArrayAdapter.notifyDataSetChanged();
        }

        @Override
        protected void onResume() {
            super.onResume();
            updateUI();
        }

        private class ListMoreItemsTask extends AsyncTask<Void, Void, LoudmouthException> {
            @Override
            protected LoudmouthException doInBackground(Void... arg0) {
                LoudmouthException exception = null;

                loadingMore = true;

                // reset loading values if adapter was reseted
                if (currentFavoritesArrayAdapter.getCount() == 0)
                    maximumDataLoadedYet = Configuration.LOADED_ITEMS_ON_LIST_AT_ONCE;

                // Get value of Configuration.LOADEDITEMSONLISTATONCE new listitems
                for (; currentDataLoaded < maximumDataLoadedYet && currentDataLoaded < idList.size(); currentDataLoaded++) {

                    // Fill the list with new information
                    currentFavorites.add(FavoriteProvider.getInstance().getFavorite(idList.get(currentDataLoaded)));
                }
                maximumDataLoadedYet += itemsPerLoading;

                // Done loading more.
                loadingMore = false;

                return exception;
            }

            protected void onPostExecute(LoudmouthException result) {
                if (result == null) {
                    // Tell to the adapter that changes have been made, this will
                    // cause
                    // the list to refresh
                    currentFavoritesArrayAdapter.notifyDataSetChanged();

                    // remove loading view when maximum data is reached
                    if (currentFavorites.size() == idList.size()) {
                        listViewFavorites.removeFooterView(footerView);
                    }
                }
            }
        }

        public void toggleButtonStates(int viewId) {

            // set clicked button as selected
            if (viewId != 0) {
                switch (viewId) {
                case R.id.btn_fav_filter_none:
                    btn_filter_none.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.filter_neuste_selected), null, null,
                            null);
                    btn_filter_none.setTextColor(getResources().getColor(color.black));
                    break;
                case R.id.btn_fav_filter_topic:
                    btn_filter_topic.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.filter_themen_selected), null, null,
                            null);
                    btn_filter_topic.setTextColor(getResources().getColor(color.black));
                    break;
                case R.id.btn_fav_filter_rating:
                    btn_filter_rating.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.filter_rating_selected), null, null,
                            null);
                    btn_filter_rating.setTextColor(getResources().getColor(color.black));
                    break;
                default:
                    Log.d("TAG", "No View with id: " + viewId);
                }
            }

            // if previews Button exists and wasn't the same button set the old
            // one
            // to selected false
            if (oldBtnViewId != 0 && oldBtnViewId != viewId) {
                // set clicked button as not selected
                switch (oldBtnViewId) {
                case R.id.btn_fav_filter_none:
                    btn_filter_none.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.filter_neuste), null, null, null);
                    btn_filter_none.setTextColor(getResources().getColor(R.color.font_grey));
                    break;
                case R.id.btn_fav_filter_topic:
                    btn_filter_topic.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.filter_themen), null, null, null);
                    btn_filter_topic.setTextColor(getResources().getColor(R.color.font_grey));
                    break;
                case R.id.btn_fav_filter_rating:
                    btn_filter_rating.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.filter_rating), null, null, null);
                    btn_filter_rating.setTextColor(getResources().getColor(R.color.font_grey));
                    break;
                default:
                    Log.d("TAG", "No View with id: " + viewId);
                }
            }
            oldBtnViewId = viewId;
        }

    }
公共类FavoriteActivity扩展了SpeakSuperActivity{
private final static String TAG=FavoriteActivity.class.getSimpleName();
专用按钮btn\u过滤器\u主题,btn\u过滤器\u评级,btn\u过滤器\u无;
私有文本查看fav_过滤器_文本;
私有静态ListView listViewFavorites;
私有静态文本视图txtNoFavoritesYet;
私人收藏清单;
专用阵列适配器currentFavoritesArrayAdapter;
//需要逐件列出装载清单
final int itemsPerLoading=Configuration.LOADED_ITEMS_ON_LIST_一次性;
boolean loadingMore=false;
私人名单上的游手好闲者;
int currentDataLoaded;
私有静态int oldBtnViewId=0;
//将开始值设置为与加载值相同
int maximumDataLoadedYet=Configuration.LOADED\u列表上的\u项\u一次性;
//0=未排序,1=按主题和最小星星数排序
私有int caseSelection=0;
私有静态视图;
私有视图;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.favorites);
Log.d(标记“FavoriteScreen onCreate()…”;
//等待进程指标
progressView=UIUtils.addBlockingProgressIndicatorBlack(此);
//初始化列表视图
listViewFavorites=(ListView)findViewById(R.id.fav\u ListView\u favorites);
//在添加适配器之前添加页脚,否则添加页脚
//不会加载!
footerView=((LayoutFlater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE)).inflate(R.LAYOUT.listviewfooter,null,false);
listViewFavorites.addFooterView(footerView);
listViewFavorites=(ListView)findViewById(R.id.fav\u ListView\u favorites);
fav_filter_text=(TextView)findViewById(R.id.fav_filter_text);
btn_filter_none=(按钮)findViewById(R.id.btn_fav_filter_none);
btn_filter_topic=(按钮)findviewbyd(R.id.btn_fav_filter_topic);
btn\u过滤器等级=(按钮)findViewById(R.id.btn\u fav\u过滤器等级);
切换按钮状态(R.id.btn\u fav\u filter\u none);
LoadDataTask ldTask=新建LoadDataTask();
ldTask.execute();
//还没有最喜欢的吗?
txtNoFavoritesYet=(TextView)findViewById(R.id.fav\u还没有收藏);
updateUI();
}
/**
*用于在后台加载数据的异步任务(不阻止UI线程)。
* 
*@作者乔纳斯·苏库普
*/
私有类LoadDataTask扩展了AsyncTask{
private final String TAG=LoadDataTask.class.getName();
受保护的void onPreExecute(){
super.onPreExecute();
if(FavoriteProvider.getInstance().GetNumoFavorites()!=0)
progressView.setVisibility(View.VISIBLE);
其他的
progressView.setVisibility(View.GONE);
listViewFavorites.setVisibility(View.GONE);
fav_filter_text.setVisibility(View.GONE);
btn_filter_none.setVisibility(View.GONE);
btn_filter_topic.setVisibility(View.GONE);
btn_filter_rating.setVisibility(View.GONE);
}
受保护的异常doInBackground(无效…参数){
loudmoutexception=null;
Log.d(标签“加载数据…”);
开关(案例选择){
案例0:
//获取FavoriteList而不进行排序
idList=FavoriteProvider.getInstance().getFavoritesByDate();
打破
案例1:
//获取按排序的收藏夹列表
//话题+明星数量
SharedReferences prefs=PreferenceManager.GetDefaultSharedReferences(getApplicationContext());
float minRating=prefs.getFloat(getResources().getString(R.string.rating\u filter\u star\u amount),0;
idList=FavoriteProvider.getInstance().getFavoritesByTopicAndMinRating(minRating);
打破
违约:
Log.e(标签“无案例编号:”+案例选择);
}
//重置加载的数据,使其加载到
//刷新列表
currentDataLoaded=0;
//数据更改时重置列表
currentFavorites=新的ArrayList();
Log.d(标记“…加载数据完成”);
返回异常;
}
受保护的void onPostExecute(异常结果){
试一试{
Log.d(标记“LoadDataTask.onPostExecute()”);
super.onPostExecute(结果);
progressView.setVisibility(View.GONE);
如果(结果!=null){
//加载过程中出现错误
android.content.DialogInterface.OnClickListener retryClickListener=新建android.content.DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
新建LoadDataTask().execute();
}
};
showRetryCancelAlertDialog(getApplicationContext(),结果,retryClickListener,null);
}否则{
//一切都很好,数据
private class ListMoreItemsTask extends AsyncTask<Void, Void, LoudmouthException> {
private class ListMoreItemsTask extends AsyncTask<Void, Favorite, LoudmouthException> {
currentFavorites.add(FavoriteProvider.getInstance().getFavorite(idList.get(currentDataLoaded)));
publishProgress(FavoriteProvider.getInstance().getFavorite(idList.get(currentDataLoaded));
onProgressUpdate(Favorite... values) {
    currentFavorites.add(values[0]);
    currentFavoritesArrayAdapter.notifyDataSetChanged();
}