gpt4 book ai didi

java - 为什么我会收到此错误 "FirebaseRecyclerAdapter() in FirebaseRecyclerAdapter cannot be applied to:"

转载 作者:行者123 更新时间:2023-12-02 01:10:48 25 4
gpt4 key购买 nike

我正在开发一个包含帖子的应用,并使用 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

关于java - 为什么我会收到此错误 "FirebaseRecyclerAdapter() in FirebaseRecyclerAdapter cannot be applied to:",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59430285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com