SearchActivity定制建议:适配器不刷新

时间:2016-10-26 05:43:10

标签: android android-adapter simplecursoradapter android-search searchactivity

我有一个使用基本Android功能的简单SearchActivity

但是,我已经定制了以前的搜索'建议显示在ListView上的mail_layout

问题

删除和添加值不会实时显示在UI上。它只会在重新创建活动后显示。

我尝试了什么:

  1. 再次取出光标并将其放在适配器上。

  2. adapter.notifyDatasetChanged()

  3. adapter.swapCursor(光标)

  4. 不知何故,这些似乎都不起作用。欢迎任何建议:

    SearchActivity类:

    package com.example.deep_kulshreshtha.expandablelistnavdrawer;
    
    import android.app.SearchManager;
    import android.content.Context;
    import android.content.Intent;
    import android.database.ContentObserver;
    import android.database.Cursor;
    import android.net.Uri;
    import android.provider.SearchRecentSuggestions;
    import android.support.v4.app.NavUtils;
    import android.support.v4.widget.CursorAdapter;
    import android.support.v4.widget.ResourceCursorAdapter;
    import android.support.v4.widget.SimpleCursorAdapter;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.SearchView;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.LinearLayout;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import java.lang.reflect.Array;
    import java.util.Arrays;
    
    public class SearchActivity extends AppCompatActivity {
    
        static Uri uri = Uri.parse("content://" + MyContentProvider.AUTHORITY + "/suggestions");
        Cursor cursor;
        SimpleCursorAdapter simpleCursorAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_search);
    
            handleIntent(getIntent());
    
            this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            this.getSupportActionBar().setDisplayShowHomeEnabled(true);
    
            cursor = getContentResolver().query(uri, new String[]{"query"}, null, null, null);
    
            ListView listView = (ListView) findViewById(R.id.listView);
            instantiateAdapter();
            listView.setAdapter(simpleCursorAdapter);
    
        }
    
        private void instantiateAdapter(){
    
            simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.previous_search, cursor,
                    new String[]{ "query" }, new int[] { R.id.previousSearchTextView },
                    CursorAdapter.NO_SELECTION);
        }
    
        @Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);
            handleIntent(intent);
        }
    
        private void handleIntent(Intent intent){
    
            if(Intent.ACTION_SEARCH.equals(intent.getAction())){
                String queryString = intent.getStringExtra(SearchManager.QUERY);
    
                SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
                        MyContentProvider.AUTHORITY, MyContentProvider.MODE);
                suggestions.saveRecentQuery(queryString, null);
            }
    
            cursor = getContentResolver().query(uri, new String[]{"query"}, null, null, null);
            instantiateAdapter();
            simpleCursorAdapter.swapCursor(cursor);
    
        }
    
        @Override
        public void onBackPressed() {
            super.onBackPressed();
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
    
            getMenuInflater().inflate(R.menu.search_menu, menu);
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    
            SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
            searchView.setIconified(false);
            searchView.setQueryHint(getString(R.string.search_activity_text));
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    
            return true;
        }
    
        public void removeItem(View view){
    
            TextView textView = (TextView) ((LinearLayout)view.getParent()).findViewById(R.id.previousSearchTextView);
            String queryString = textView.getText().toString();
            int deleted = getContentResolver().delete(uri, "query = ?", new String[]{ queryString });
        }
    
    }
    

1 个答案:

答案 0 :(得分:0)

发现问题:

一个。更新了活动launchMode =“singleTop”

湾注册了ContentObserver并在onChange方法中执行了此操作:

        handler.post(new Runnable() {
            @Override
            public void run() {
                Log.v("Deep", "Inside ContentObserver : NofityDataSetChange Called ");
                cursor = getContentResolver().query(uri, new String[]{"query"}, null, null, null);
                simpleCursorAdapter.swapCursor(cursor);
            }
        });