This question already has answers here:
firebaserecycleradapter() cannot be applied to FirebaseRecyclerAdapter
(5个答案)
4个月前关闭。
我正在开发带有帖子的应用程序,并将Firebase用作后端平台。
接下来,我尝试添加FirebaseRecyclerAdapter,按照给出的说明进行操作,但出现此错误,即视频中的那个人不在,并且我无法修复它。
这是我的代码:
我收到我在问题中键入的错误。
没有关于StackOverflow的答案对我有用,这就是为什么我要发表一篇新文章。
变成这个:
在新版本的Firebaseui中,
检查文档:
https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md#firebaseui-for-realtime-database
(5个答案)
4个月前关闭。
我正在开发带有帖子的应用程序,并将Firebase用作后端平台。
接下来,我尝试添加FirebaseRecyclerAdapter,按照给出的说明进行操作,但出现此错误,即视频中的那个人不在,并且我无法修复它。
这是我的代码:
import android.annotation.SuppressLint;
import android.content.Intent;
import android.gesture.GestureOverlayView;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toolbar;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.multidex.MultiDex;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.shrinkio.Fragments.HomeFragment;
import com.example.shrinkio.Fragments.MessagesFragment;
import com.example.shrinkio.Fragments.NotFragment;
import com.example.shrinkio.Fragments.PeopleFragment;
import com.example.shrinkio.R;
import com.example.shrinkio.SecondaryActivities.DrawerLayout;
import com.example.shrinkio.SecondaryActivities.PostActivity;
import com.example.shrinkio.SecondaryActivities.Posts;
import com.example.shrinkio.SecondaryActivities.ProfileActivity;
import com.example.shrinkio.SecondaryActivities.SettingsActivity;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationView;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.storage.StorageReference;
import java.util.Objects;
import static com.example.shrinkio.R.menu.main_menu;
public class BottomActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
RadioGroup radioGroup;
RadioButton Rd1, Rd2, Rd3, Rd4;
RecyclerView rv;
private ActionBar actionBar;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private StorageReference StorageRef;
private Toolbar toolbar;
private DatabaseReference mDataBase;
@SuppressLint({"RtlHardcoded", "ClickableViewAccessibility"})
@Override
protected void onCreate(Bundle savedInstanceState) {
MultiDex.install(this);
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_bottom );
new GestureOverlayView( this );
//Action bar configurations
Objects.requireNonNull( getSupportActionBar() ).setDisplayOptions( ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView( R.layout.abs_layout_home );
//Navigation view
NavigationView nav_view;
nav_view = findViewById(R.id.nav_view); //select nav_view from activity_drawer_layout
nav_view.setNavigationItemSelectedListener(this);
View nav_header = nav_view.getHeaderView(0);
rv = findViewById(R.id.recyclerView);
rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(this));
mDataBase = FirebaseDatabase.getInstance().getReference().child("Posts");
//Navigation Drawer header menu items on click actions
LinearLayout header = nav_header.findViewById( R.id.nav_header);
header.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity( new Intent( BottomActivity.this, ProfileActivity.class ) );
}
} );
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
//I added this if statement to keep the selected fragment when rotating the device
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
}
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<Posts> options = new FirebaseRecyclerOptions.Builder<Posts>()
.setQuery(mDataBase, Posts.class)
.build();
FirebaseRecyclerAdapter firebaseRecyclerAdapter = new FirebaseRecyclerAdapter <Posts, PostViewHolder> (
Posts.class,
R.layout.posts,
PostViewHolder.class,
mDataBase
) {
@Override
protected void onBindViewHolder(@NonNull PostViewHolder holder, int position, Posts model) {
holder.setPost(model.getPost());
}
@NonNull
@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.posts, parent, false);
return new PostViewHolder(view);
}
};
rv.setAdapter(firebaseRecyclerAdapter);
}
public static class PostViewHolder extends RecyclerView.ViewHolder{
View mview;
public PostViewHolder(@NonNull View itemView) {
super(itemView);
mview = itemView;
}
public void setPost(String post) {
TextView post_title = mview.findViewById(R.id.post);
post_title.setText(post);
}
}
//Define the options menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
{
if (item.getItemId() == R.id.add){
startActivity(new Intent(this, PostActivity.class));
}
}
return super.onOptionsItemSelected(item);
}
// Bottom Navigation View, when the fragment is selected
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
selectedFragment = new HomeFragment();
break;
case R.id.navigation_dashboard:
selectedFragment = new NotFragment();
break;
case R.id.navigation_people:
selectedFragment = new PeopleFragment();
break;
case R.id.navigation_messages:
selectedFragment = new MessagesFragment();
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
};
// Navigation menu items on click action
@Override
public boolean onNavigationItemSelected (@NonNull MenuItem item){
switch (item.getItemId()) {
case R.id.nav_profile:
startActivity( new Intent( BottomActivity.this, ProfileActivity.class ) );
overridePendingTransition( 0, 0 );
return true;
case R.id.nav_settings:
startActivity( new Intent( BottomActivity.this, SettingsActivity.class ) );
overridePendingTransition( 0, 0 );
return true;
default:
return super.onOptionsItemSelected( item );
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
}
我收到我在问题中键入的错误。
没有关于StackOverflow的答案对我有用,这就是为什么我要发表一篇新文章。
最佳答案
更改此:
FirebaseRecyclerAdapter firebaseRecyclerAdapter = new FirebaseRecyclerAdapter <Posts, PostViewHolder> (
Posts.class,
R.layout.posts,
PostViewHolder.class,
mDataBase
)
变成这个:
FirebaseRecyclerAdapter firebaseRecyclerAdapter = new FirebaseRecyclerAdapter <Posts, PostViewHolder> (options)
在新版本的Firebaseui中,
FirebaseRecyclerAdapter
的构造函数采用您已经初始化的类型为FirebaseRecyclerOption
的参数。检查文档:
https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md#firebaseui-for-realtime-database
09-17 17:40