0

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

但是,我已经自定义了“以前的搜索”建议以显示ListViewmail_layout.

问题

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

我试过的:

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

  2. 适配器.notifyDatasetChanged()

  3. 适配器.swapCursor(光标)

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

搜索活动类:

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 });
    }

}
4

1 回答 1

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);
            }
        });
于 2016-10-26T07:55:47.863 回答