Я создал listview
, в котором много информации в одном списке. Я устал реализовывать представление поиска в адаптере listview
, но список не фильтруется.
Ниже мой исходный код для этого действия.
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. Может ли кто-нибудь подсказать, как это сделать?
