В настоящее время я пытаюсь добавить представление поиска в мое представление переработчика, но оно не добавляется. Когда я запускаю свое приложение без представления поиска, оно работает полностью нормально, но когда я добавляю представление поиска в свое приложение, оно просто показывает пустой экран после нажатия на значок поиска.
MainActivity. java
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import androidx.appcompat.widget.SearchView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Modalclass> item_list;
private MyDataAdapter myDataAdapter;
private LinearLayoutManager linearLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
item_list = new ArrayList<>();
myDataAdapter = new MyDataAdapter(this,item_list);
linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setAdapter(myDataAdapter);
recyclerView.setLayoutManager(linearLayoutManager);
item_list.add(new Modalclass("123456789",R.drawable.pic1));
item_list.add(new Modalclass("12345678",R.drawable.pic2));
item_list.add(new Modalclass("Shivamn",R.drawable.pic1));
item_list.add(new Modalclass("mmnksksd",R.drawable.pic2));
item_list.add(new Modalclass("jdsvkjd",R.drawable.pic1));
item_list.add(new Modalclass("sdvfdv",R.drawable.pic2));
item_list.add(new Modalclass("retw",R.drawable.pic1));
item_list.add(new Modalclass("xcvsdsfv",R.drawable.pic2));
item_list.add(new Modalclass("dfgsd",R.drawable.pic1));
item_list.add(new Modalclass("dgsdg",R.drawable.pic2));
item_list.add(new Modalclass("dsgdfgsd",R.drawable.pic1));
item_list.add(new Modalclass("ewete",R.drawable.pic2));
item_list.add(new Modalclass("vferthe",R.drawable.pic1));
item_list.add(new Modalclass("gfdnhytj",R.drawable.pic2));
item_list.add(new Modalclass("nfhjtyj",R.drawable.pic1));
item_list.add(new Modalclass("gfnthtr",R.drawable.pic2));
item_list.add(new Modalclass("dsffgrtg",R.drawable.pic1));
item_list.add(new Modalclass("sdfgrtg",R.drawable.pic2));
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.search_view,menu);
MenuItem searchItem=menu.findItem(R.id.search_view);
SearchView searchView= (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
myDataAdapter.getFilter().filter(newText);
return false;
}
});
return true;
}
}
Activity_main. 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=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MyDataAdapter. java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class MyDataAdapter extends RecyclerView.Adapter<MyDataAdapter.MyViewHolder> implements Filterable {
private Context context;
private List<Modalclass> item_list;
private List<Modalclass> item_list_full;
public MyDataAdapter(Context context, List<Modalclass> item_list) {
this.context = context;
this.item_list = item_list;
this.item_list_full = new ArrayList<>(item_list);
}
@NonNull
@Override
public MyDataAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyDataAdapter.MyViewHolder holder, int position) {
final Modalclass modalclass = item_list.get(position);
holder.textView.setText(item_list.get(position).getName());
holder.imageView.setImageResource(item_list.get(position).getImgresource());
}
@Override
public int getItemCount() {
return item_list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView textView;
ImageView imageView;
View mView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.t1);
imageView = itemView.findViewById(R.id.i1);
mView=itemView;
}
}
@Override
public Filter getFilter() {
return DataFilter;
}
private Filter DataFilter= new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<Modalclass> filteredList = new ArrayList<>();
if (constraint==null || constraint.length()==0 ){
filteredList.addAll(item_list_full);
}
else {
String filter=constraint.toString().toLowerCase().trim();
for (Modalclass dataitem:item_list_full){
if (dataitem.getName().toLowerCase().contains(filter)){
filteredList.add(dataitem);
}
}
}
FilterResults results=new FilterResults();
results.values=filteredList;
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
item_list.clear();
item_list.addAll((Collection<? extends Modalclass>) results.values);
notifyDataSetChanged();
}
};
}
Modalclass. java
class Modalclass {
String Name;
int imgresource;
public Modalclass(String name, int imgresource) {
Name = name;
this.imgresource = imgresource;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getImgresource() {
return imgresource;
}
public void setImgresource(int imgresource) {
this.imgresource = imgresource;
}
}
search_view. 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/search_view"
android:icon="@drawable/ic_search_black_24dp"
android:title="Search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="androidx.appcompat.widget.SearchView"/>
</menu>
recycler_item. xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginBottom="8dp">
<TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:height="70dp"
android:fontFamily="casual"
android:gravity="center"
android:text="TextView"
android:textColor="#3D3636"
android:textSize="20sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/i1"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_weight="2"
android:scaleType="fitXY"
app:srcCompat="@mipmap/ic_launcher" />
</LinearLayout>