从firebase检索图像以在片段中的回收器视图时出错

Posted

技术标签:

【中文标题】从firebase检索图像以在片段中的回收器视图时出错【英文标题】:Error to retrieve image from firebase for recycler view in fragment 【发布时间】:2021-10-28 04:39:05 【问题描述】:

我想在片段中为回收站视图检索图像

MovieActivity 代码:

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) 
        Intent intent = new Intent(this, LoadingActivity.class);
        startActivity(intent);
        setupViewFragment();
    
    setupToolbar();
    setupBottomNavigation();


private void setupViewFragment() 
    // show discover movies fragment by default
    DiscoverMoviesFragment discoverMoviesFragment = DiscoverMoviesFragment.newInstance();
    ActivityUtils.replaceFragmentInActivity(
            getSupportFragmentManager(), discoverMoviesFragment, R.id.fragment_container);


private void setupBottomNavigation() 
    BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
    bottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() 
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) 
            switch (menuItem.getItemId()) 
                case R.id.action_home:
                    ActivityUtils.replaceFragmentInActivity(
                            getSupportFragmentManager(), HomeFragment.newInstance(),
                            R.id.fragment_container);
                    //startActivity(new Intent(getApplicationContext()
                            //,HomeActivity.class));
                    return true;
                case R.id.action_discover:
                    ActivityUtils.replaceFragmentInActivity(
                            getSupportFragmentManager(), DiscoverMoviesFragment.newInstance(),
                            R.id.fragment_container);
                    return true;
                case R.id.action_favorites:
                    ActivityUtils.replaceFragmentInActivity(
                            getSupportFragmentManager(), FavoritesFragment.newInstance(),
                            R.id.fragment_container);
                    return true;
            
            return false;
        
    );


private void setupToolbar() 
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

`

HomeFragment 的代码:

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) 
        Intent intent = new Intent(this, LoadingActivity.class);
        startActivity(intent);
        setupViewFragment();
    
    setupToolbar();
    setupBottomNavigation();


private void setupViewFragment() 
    // show discover movies fragment by default
    DiscoverMoviesFragment discoverMoviesFragment = DiscoverMoviesFragment.newInstance();
    ActivityUtils.replaceFragmentInActivity(
            getSupportFragmentManager(), discoverMoviesFragment, R.id.fragment_container);


private void setupBottomNavigation() 
    BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
    bottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() 
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) 
            switch (menuItem.getItemId()) 
                case R.id.action_home:
                    ActivityUtils.replaceFragmentInActivity(
                            getSupportFragmentManager(), HomeFragment.newInstance(),
                            R.id.fragment_container);
                    //startActivity(new Intent(getApplicationContext()
                            //,HomeActivity.class));
                    return true;
                case R.id.action_discover:
                    ActivityUtils.replaceFragmentInActivity(
                            getSupportFragmentManager(), DiscoverMoviesFragment.newInstance(),
                            R.id.fragment_container);
                    return true;
                case R.id.action_favorites:
                    ActivityUtils.replaceFragmentInActivity(
                            getSupportFragmentManager(), FavoritesFragment.newInstance(),
                            R.id.fragment_container);
                    return true;
            
            return false;
        
    );


private void setupToolbar() 
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

HomeAdapter 的代码:

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ImageViewHolder> 
private Context mContext;
private List<Upload> mUploads;
private OnItemClickListener mListener;

public HomeAdapter(Context context, List<Upload> uploads) 
    mContext = context;
    mUploads = uploads;


@Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
    View v = LayoutInflater.from(mContext).inflate(R.layout.image_item, parent, false);
    return new ImageViewHolder(v);


@Override
public void onBindViewHolder(ImageViewHolder holder, int position) 
    Upload uploadCurrent = mUploads.get(position);
    holder.textViewName.setText(uploadCurrent.getName());
    Picasso.get()
            .load(uploadCurrent.getImageUrl())
            .placeholder(R.mipmap.ic_launcher)
            .centerInside()
            .fit()
            .into(holder.imageView);


@Override
public int getItemCount() 
    return mUploads.size();


public class ImageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,
        View.OnCreateContextMenuListener, MenuItem.OnMenuItemClickListener 
    public TextView textViewName;
    public ImageView imageView;

    public ImageViewHolder(View itemView) 
        super(itemView);
        textViewName = itemView.findViewById(R.id.text_view_name);
        imageView = itemView.findViewById(R.id.image_view_upload);
        itemView.setOnClickListener(this);
        itemView.setOnCreateContextMenuListener(this);
    

    @Override
    public void onClick(View v) 
        if (mListener != null) 
            int position = getAdapterPosition();
            if (position != RecyclerView.NO_POSITION) 
                mListener.onItemClick(position);
            
        
    

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) 
        menu.setHeaderTitle("Select Action");
        MenuItem doWhatever = menu.add(Menu.NONE, 1, 1, "Do whatever");
        MenuItem delete = menu.add(Menu.NONE, 2, 2, "Delete");
        doWhatever.setOnMenuItemClickListener(this);
        delete.setOnMenuItemClickListener(this);
    

    @Override
    public boolean onMenuItemClick(MenuItem item) 
        if (mListener != null) 
            int position = getAdapterPosition();
            if (position != RecyclerView.NO_POSITION) 
                switch (item.getItemId()) 
                    case 1:
                        mListener.onWhatEverClick(position);
                        return true;
                    case 2:
                        mListener.onDeleteClick(position);
                        return true;
                
            
        
        return false;
    


public interface OnItemClickListener 
    void onItemClick(int position);

    void onWhatEverClick(int position);

    void onDeleteClick(int position);


public void setOnItemClickListener(OnItemClickListener listener) 
    mListener = listener;

这是错误:

`C:\Users\User\Desktop\PopularMovies\app\src\main\java\com\ajdi\yassin\popularmovies\ui\movieslist\home\HomeFragment.java:65:错误:不兼容的类型:HomeFragment 不能转换为上下文

    mAdapter = new HomeAdapter(HomeFragment.this, mUploads);`

【问题讨论】:

【参考方案1】:

请使用 getApplicationContext() ,因为 HomeFragment.this 被视为 Activity 并且在您的 HomeAdapter 构造函数中您已传递 Context

替换:

 mAdapter = new HomeAdapter(HomeFragment.this, mUploads);

收件人:

 mAdapter = new HomeAdapter(getApplicationContext(), mUploads);

【讨论】:

以上是关于从firebase检索图像以在片段中的回收器视图时出错的主要内容,如果未能解决你的问题,请参考以下文章

如何使用随机键从 Firebase 数据库中检索子项到 Android 的回收站视图中?

Swift:从 Firebase 检索数据并在表格视图中显示

无法从 firebase 回收器视图中的 firebase 实时数据库中获取嵌套数据

使用 OnClick 删除回收站视图中的项目。 Firebase 安卓

如何使用 kotlin 实现分页

在recyclerview kotlin中插入firebase快照