Новый студент студии Android здесь.Я пытаюсь поместить Edittext выше моего recyclerView, который находится на странице фрагмента, и мне трудно это сделать.приложение запускается нормально, но оно продолжает падать, как только я запускаю приложение.Вот мой код.Я попытался поместить текст редактирования в скобку onCreate.не могли бы вы мне помочь.Я приложил фрагмент кода Java и Адаптер ресивера Ciew.Спасибо
Фрагмент 1
public class Tab1Fragment extends Fragment {
View v;
private RecyclerView rRecyclerView;
private List<RestaurantItem>lstRestaurant;
private RecyclerViewAdapter rAdapter;
public Tab1Fragment(){
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment1_layout,container,false);
rRecyclerView = (RecyclerView) v.findViewById( R.id.restaurant_recyclerView );
RecyclerViewAdapter recyclerAdapter = new RecyclerViewAdapter( getContext(),lstRestaurant );
rRecyclerView.setLayoutManager( new LinearLayoutManager( getActivity() ) );
rRecyclerView.setAdapter( recyclerAdapter );
EditText rEditText = v.findViewById( R.id.edit_text );
rEditText.addTextChangedListener( new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
filter(s.toString());
}
} );
return v;
}
private void filter(String text){
ArrayList<RestaurantItem> filteredList = new ArrayList<>( );
for (RestaurantItem item : lstRestaurant){
if (item.getrAddress().toLowerCase().contains( text.toLowerCase() )){
filteredList.add(item);
}
}
rAdapter.filterList( filteredList );
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
lstRestaurant=new ArrayList<>( );
lstRestaurant.add( new RestaurantItem( " Restaurant1","AAAAAAAAAAAAAAAA ", "5 Miles Away", "10:00 am - 10:00 pm",R.drawable.beatesub ) );
RecyclerviewAdapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
Context rcontext;
List<RestaurantItem> mData;
public RecyclerViewAdapter(Context rcontext, List<RestaurantItem> mData) {
this.rcontext = rcontext;
this.mData = mData;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v;
v= LayoutInflater.from(rcontext).inflate( R.layout. item_restaurant,parent,false);
MyViewHolder vHolder = new MyViewHolder(v);
return vHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.tv_name.setText( mData.get(position).getrName() );
holder.tv_address.setText( mData.get( position ).getrAddress() );
holder.tv_distance.setText( mData.get( position ).getrDistance() );
holder.tv_hours.setText( mData.get( position ).getrHours() );
holder.img.setImageResource( mData.get( position ).getrImage() );
}
@Override
public int getItemCount() { return mData.size();
}
public void filterList(ArrayList<RestaurantItem> filteredList){
mData=filteredList;
notifyDataSetChanged();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
private TextView tv_name;
private TextView tv_address;
private TextView tv_distance;
private TextView tv_hours;
private ImageView img;
public MyViewHolder(@NonNull View itemView) {
super( itemView );
tv_name = (TextView) itemView.findViewById( R.id.name_restaurant );
tv_address = (TextView) itemView.findViewById( R.id.address_restaurant );
tv_distance = (TextView) itemView.findViewById( R.id.distance_restaurant );
tv_hours = (TextView) itemView.findViewById( R.id.hours_restaurant );
img = (ImageView) itemView.findViewById( R.id.image_restaurant );
}
}
}