Я полагаюсь на видео для создания социальной сети на Android, я не знаю, почему эта ошибка мне кажется, если кто-нибудь знает, как ее решить, я был бы очень признателен. Видео с канала Atif Pervaiz, конкретное видео - приложение для социальных сетей Firebase - 18 Показать и найти сообщения
In Activity HomeFragment enter code here
package com.example.whatsappinacap;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.core.view.MenuItemCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.whatsappinacap.adapters.AdapterPosts;
import com.example.whatsappinacap.models.ModelPost;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class HomeFragment extends Fragment {
FirebaseAuth firebaseAuth;
RecyclerView recyclerView;
List<ModelPost> postList;
AdapterPosts adapterPosts;
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
firebaseAuth=FirebaseAuth.getInstance();
recyclerView=view.findViewById(R.id.postRecyclerview);
LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity());
layoutManager.setStackFromEnd(true);
layoutManager.setReverseLayout(true);
recyclerView.setLayoutManager(layoutManager);
postList=new ArrayList<>();
loadPosts();
return view;
}
private void loadPosts() {
DatabaseReference ref= FirebaseDatabase.getInstance().getReference("Posts");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
postList.clear();
for (DataSnapshot ds: dataSnapshot.getChildren()){
ModelPost modelPost=ds.getValue(ModelPost.class);
postList.add(modelPost);
adapterPosts=new AdapterPosts(getActivity(), postList);
recyclerView.setAdapter(adapterPosts);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getActivity(), ""+databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private void searchPosts(final String searchQuery){
DatabaseReference ref= FirebaseDatabase.getInstance().getReference("Posts");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
postList.clear();
for (DataSnapshot ds: dataSnapshot.getChildren()){
ModelPost modelPost=ds.getValue(ModelPost.class);
if (modelPost.getpTitle().toLowerCase().contains(searchQuery.toLowerCase()) ||
modelPost.getpDescr().toLowerCase().contains(searchQuery.toLowerCase())){
postList.add(modelPost);
}
adapterPosts=new AdapterPosts(getActivity(), postList);
recyclerView.setAdapter(adapterPosts);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getActivity(), ""+databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private void checkUserStatus(){
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null){
}
else {
startActivity(new Intent(getActivity(), MainActivity.class));
getActivity().finish();
}
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
MenuItem item=menu.findItem(R.id.action_search);
SearchView searchView = (SearchView)MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
if (!TextUtils.isEmpty(s)){
searchPosts(s);
}
else {
loadPosts();
}
return false;
}
@Override
public boolean onQueryTextChange(String s) {
if (!TextUtils.isEmpty(s)){
searchPosts(s);
}
else {
loadPosts();
}
return false;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_logout){
firebaseAuth.signOut();
checkUserStatus();
}
if (id == R.id.action_add_post){
startActivity(new Intent(getActivity(), AddPostActivity.class));
}
return super.onOptionsItemSelected(item);
}
enter code here
<!-- begin snippet: js hide: false console: true babel: false -->
And the xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/postRecyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
}
сильный текст