SearchView不能用ListView过滤结果

问题描述 投票:0回答:1

我已经创建了一个 listview 在一个列表里有很多信息。我厌倦了在 listview 适配器,但列表没有被过滤。

下面是我的那个Activity的源码。

DictionaryAdapter.java

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.CursorAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.personaldictionary.R;
import com.example.personaldictionary.database.DictionaryContract;

public class DictionaryAdapter extends CursorAdapter {

    private static final String LOG_TAG = "DictionaryAdapter";

    public DictionaryAdapter(Context context, Cursor c) {
        super(context, c);
    }


    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        Log.d(LOG_TAG,"newView called cursor: "+cursor+" context: "+context+" parent: "+parent);
        return LayoutInflater.from(context).inflate(R.layout.word_list, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        Log.d(LOG_TAG,"bindView called cursor: "+cursor+" context: "+context+" view: "+view);


        view.findViewById(R.id.container).setAnimation(AnimationUtils.loadAnimation(context,R.anim.fade_scale_animation));


        TextView wordTextView = (TextView)  view.findViewById(R.id.word_text_list);
        TextView wordDefTextView = (TextView) view.findViewById(R.id.word_definition_list);
        TextView wordEtyloTextView = (TextView) view.findViewById(R.id.word_etymologies_list);

        int wordColumnIndex = cursor.getColumnIndex(DictionaryContract.DictionaryEntry.COLUMN_WORD);
        int defColumnIndex = cursor.getColumnIndex(DictionaryContract.DictionaryEntry.COLUMN_WORD_DEFINITION);
        int etyloColumnIndex = cursor.getColumnIndex(DictionaryContract.DictionaryEntry.COLUMN_WORD_ETYMOLOGIES);


        String word = cursor.getString(wordColumnIndex);
        String wordDef = cursor.getString(defColumnIndex);
        String wordEtylo = cursor.getString(etyloColumnIndex);

        wordTextView.setText(word);
        wordDefTextView.setText(wordDef);
        wordEtyloTextView.setText(wordEtylo);


    }
}

WordListActivity.java

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.MenuItemCompat;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.CursorLoader;
import androidx.loader.content.Loader;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import androidx.appcompat.widget.SearchView;
import android.widget.Toast;

import com.example.personaldictionary.adapter.DictionaryAdapter;
import com.example.personaldictionary.database.DictionaryContract;
import com.example.personaldictionary.database.DictionaryDbHelper;

public class WordListActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>  {

    private static final String LOG_TAG = "WordListActivity";

    DictionaryAdapter mAdapter;
    DictionaryDbHelper mDbHelper;


    private static final int WORD_LOADER = 0;

    private String wordString;
    private String wordDefString;
    private String wordEtyloString;

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

        ListView wordListView = (ListView) findViewById(R.id.word_list);




        View emptyView = findViewById(R.id.empty_view);
        wordListView.setEmptyView(emptyView);
        Log.d(LOG_TAG,"productListView.setEmptyView(emptyView)");

        Intent intent = getIntent();
        Bundle extras = intent.getExtras();

        if(extras!=null){
            wordString = intent.getStringExtra("word");
            wordDefString = intent.getStringExtra("wordDefinition");
            wordEtyloString = intent.getStringExtra("wordEtylo");

            Log.d("WordListActivity","wordString: "+wordString+" wordDefString: "+wordDefString+" wordEtyloString: "+wordEtyloString);

            insertWord(wordString,wordDefString,wordEtyloString);

        }


        mAdapter = new DictionaryAdapter(this,null);
        wordListView.setAdapter(mAdapter);

        getSupportLoaderManager().initLoader(WORD_LOADER, null,this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.search,menu);
        MenuItem search_item = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) search_item.getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {

                WordListActivity.this.mAdapter.getFilter().filter(newText);
                return false;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }


    public void insertWord(String word, String word_def, String word_etylo){


        Uri newUri = null;
        Log.d(LOG_TAG,"insertWord() word: "+word+" word_def: "+word_def+" word_et: "+word_etylo);

        ContentValues values = new ContentValues();
        values.put(DictionaryContract.DictionaryEntry.COLUMN_WORD,word);
        values.put(DictionaryContract.DictionaryEntry.COLUMN_WORD_DEFINITION,word_def);
        values.put(DictionaryContract.DictionaryEntry.COLUMN_WORD_ETYMOLOGIES,word_etylo);

        Log.d(LOG_TAG,"DictionaryContract.DictionaryEntry.CONTENT_URI: "+DictionaryContract.DictionaryEntry.CONTENT_URI);
        Log.d(LOG_TAG,"values: "+values);

        newUri = getContentResolver().insert(DictionaryContract.DictionaryEntry.CONTENT_URI, values);
        //mAdapter.swapCursor(getAllItems());

        //mAdapter.swapCursor(getAllItems());

        if (newUri == null) {
            Toast.makeText(WordListActivity.this, "Word insert error ", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(WordListActivity.this, "Word insert Success", Toast.LENGTH_SHORT).show();
        }

    }

    private Cursor getAllItems(){
        Log.d(LOG_TAG,"getAllItems inside");
         String[] projection = {DictionaryContract.DictionaryEntry._ID,
                 DictionaryContract.DictionaryEntry.COLUMN_WORD,
                 DictionaryContract.DictionaryEntry.COLUMN_WORD_DEFINITION,
                 DictionaryContract.DictionaryEntry.COLUMN_WORD_ETYMOLOGIES};

         Cursor cursor  = getContentResolver().query(DictionaryContract.DictionaryEntry.CONTENT_URI,projection,null,null,null);
         Log.d(LOG_TAG,"getAllItems: "+cursor);
         return cursor;
    }

    @NonNull
    @Override
    public Loader<Cursor> onCreateLoader(int id, @Nullable Bundle args) {
        Log.d(LOG_TAG,"onCreateLoader called");

        String[] projection = {DictionaryContract.DictionaryEntry._ID,
                DictionaryContract.DictionaryEntry.COLUMN_WORD,
                DictionaryContract.DictionaryEntry.COLUMN_WORD_DEFINITION,
                DictionaryContract.DictionaryEntry.COLUMN_WORD_ETYMOLOGIES,};

        return new CursorLoader(this, DictionaryContract.DictionaryEntry.CONTENT_URI,projection,null,null,null);
    }

    @Override
    public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor data) {
        Log.d(LOG_TAG,"onLoadFinished called data: "+data);
        mAdapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(@NonNull Loader<Cursor> loader) {
        mAdapter.swapCursor(null);
    }
}

activity_word_list.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".WordListActivity"
    android:background="@color/white"
    android:id="@+id/root_layout">

    <ListView
        android:id="@+id/word_list"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search_input" />


    <RelativeLayout
        android:id="@+id/empty_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/empty_title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:fontFamily="sans-serif-medium"
        android:paddingTop="16dp"
        android:text="@string/empty_view_title_text"
        android:textAppearance="?android:textAppearanceMedium"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>


    <TextView
        android:id="@+id/empty_subtitle_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/empty_title_text"
        android:layout_centerHorizontal="true"
        android:fontFamily="sans-serif"
        android:paddingTop="8dp"
        android:text="@string/empty_view_subtitle_text"
        android:textAppearance="?android:textAppearanceSmall"
        android:textColor="#A2AAB0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.528"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.548" />
    </RelativeLayout>




</androidx.constraintlayout.widget.ConstraintLayout>

word_list.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:background="#fff">


    <RelativeLayout
        android:id="@+id/container"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="20dp"
        android:layout_marginBottom="20dp"
        android:background="@drawable/card_bg"
        android:paddingBottom="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <TextView

            android:id="@+id/word_text_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="40dp"
            android:layout_marginLeft="40dp"
            android:layout_marginTop="20dp"
            android:textColor="@color/title_text_color"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/word_definition_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/word_text_list"
            android:layout_alignStart="@+id/word_text_list"
            android:layout_alignLeft="@id/word_text_list"
            android:layout_marginTop="8dp"
            android:layout_marginRight="16dp"
            android:lineSpacingExtra="8dp"
            android:textColor="@color/content_text_color" />
        <TextView
            android:id="@+id/word_etymologies_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/word_definition_list"
            android:layout_alignStart="@+id/word_definition_list"
            android:layout_alignLeft="@id/word_definition_list"
            android:layout_marginTop="8dp"
            android:layout_marginRight="16dp"
            android:lineSpacingExtra="8dp"
            android:textColor="@color/content_text_color" />

    </RelativeLayout>

    <ImageView
        android:id="@+id/word_audio"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:src="@drawable/ic_audio"
        app:layout_constraintEnd_toStartOf="@+id/container"
        app:layout_constraintStart_toStartOf="@+id/container"
        app:layout_constraintTop_toTopOf="@+id/container"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

搜索.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_search_gray_24dp"
        android:title="Search"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        app:showAsAction="always"/>
</menu>

视图是这样的。我想通过searchview过滤蓝色的文字。有谁能给我建议怎么做?

View of the list

android android-listview searchview
1个回答
0
投票

你应该过滤列表并在适配器中调用notifyDataSetChanged。

例如,你有一个列表ListA,其中包含ListA = {A,B,C}。

你过滤listA(你搜索的项目B) FilteredList = {B}。

AdapterSource = FilteredList

适配器.notifyDataSetChanged()

如果你清除搜索视图

AdapterSource = ListA

© www.soinside.com 2019 - 2024. All rights reserved.