recyclerview无法显示图片~
Posted
技术标签:
【中文标题】recyclerview无法显示图片~【英文标题】:Unable to display images in recyclerview~ 【发布时间】:2018-07-18 18:04:34 【问题描述】:我知道这个问题已经被问过很多次,我查看了所有解决方案并尝试了它们,但我的应用程序仍然无法检索图像。
请帮忙!首先,没有显示图像,其次我的应用程序崩溃了。 我很确定我在这里缺少一些东西,但我无法弄清楚.. 在我的 firebase 数据库中,标题是“类别”“图像”“名称”。
public class home extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
FirebaseDatabase database;
DatabaseReference category;
TextView txtFullName;
RecyclerView recycler_menu;
RecyclerView.LayoutManager layoutManager;
private FirebaseRecyclerAdapter<Category, MenuViewHolder> mFirebaseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Menu");
setSupportActionBar(toolbar);
//Get PassUserFullName from MainActivity main
Intent i = getIntent();
String PassUserFullName = i.getStringExtra("PassUserFullName");
//init Firebase
/*database = FirebaseDatabase.getInstance();
category = database.getReference("Category");*/
category=FirebaseDatabase.getInstance().getReference().child("Category");
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//Show Name of User
View headerView = navigationView.getHeaderView(0);
/* txtFullName = (TextView)headerView.findViewById(R.id.txtFullName);
txtFullName.setText(""+PassUserFullName);
*//*if
Toast.makeText(home.this, "User data field is null",
Toast.LENGTH_SHORT).show();
*/
//Load Menu
recycler_menu=(RecyclerView)findViewById(R.id.recycler_menu);
recycler_menu.setHasFixedSize(true);
layoutManager=new LinearLayoutManager(this);
recycler_menu.setLayoutManager(layoutManager);
loadMenu();
private void loadMenu()
FirebaseRecyclerAdapter<Category,MenuViewHolder> adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(Category.class,R.layout.menu_item,MenuViewHolder.class,category)
@Override
protected void populateViewHolder(MenuViewHolder viewHolder, Category model, int position)
viewHolder.txtMenuName.setText(model.getName());
Picasso.with(getBaseContext()).load(model.getImage()).into(viewHolder.imageView);
final Category clickItem = model;
viewHolder.setItemClickListener(new ItemClickListener()
@Override
public void onClick(View view, int position, boolean isLongClick)
Toast.makeText(home.this,""+clickItem.getName(),Toast.LENGTH_SHORT).show();
);
;
recycler_menu.setAdapter(adapter);
@Override
public void onBackPressed()
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START))
drawer.closeDrawer(GravityCompat.START);
else
super.onBackPressed();
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
return super.onOptionsItemSelected(item);
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item)
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_menu)
// Handle the camera action
else if (id == R.id.nav_cart)
else if (id == R.id.nav_orders)
else if (id == R.id.nav_log_out)
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
public class Category
public String Image;
public String Name;
public Category()
public Category(String Image, String Name)
this.Image = Image;
this.Name = Name;
public String getImage()
return Image;
public String getName()
return Name;
public void setImage(String Image)
this.Image = Image;
public void setName(String Name)
this.Name = Name;
【问题讨论】:
添加您的适配器代码 ...也请添加日志 【参考方案1】:当您使用RecyclerView
时,您并没有将list
数据传递给您的适配器,只有您使用了setAdapter。将列表数据传递给您的 RecyclerAdapter 类,并通过适配器类中的 cosntructor 获取。
private void loadMenu()
FirebaseRecyclerAdapter<Category,MenuViewHolder> adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(Category.class,R.layout.menu_item,MenuViewHolder.class,category)
@Override
protected void populateViewHolder(MenuViewHolder viewHolder, Category model, int position)
viewHolder.txtMenuName.setText(model.getName());
Picasso.with(getBaseContext()).load(model.getImage()).into(viewHolder.imageView);
final Category clickItem = model;
viewHolder.setItemClickListener(new ItemClickListener()
@Override
public void onClick(View view, int position, boolean isLongClick)
Toast.makeText(home.this,""+clickItem.getName(),Toast.LENGTH_SHORT).show();
);
;
mFirebaseAdapter= new FirebaseRecyclerAdapter(home.this, yourList);
recycler_menu.setAdapter(adapter);
有关 RecyclerView 的更多信息,请查看:https://antonioleiva.com/recyclerview/
【讨论】:
以上是关于recyclerview无法显示图片~的主要内容,如果未能解决你的问题,请参考以下文章