我知道这个问题已被多次询问,我已经查看了所有解决方案并尝试了它们但我的应用程序仍然无法检索图像。
请帮忙!首先,没有显示图像,第二个我的应用程序崩溃。 我很确定我在这里缺少一些东西,但我无法弄明白...... 在我的firebase数据库中,标题是"类别" "图像" "名称&#34 ;.
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;
}
}
答案 0 :(得分:0)
在使用RecyclerView
时,只有使用了setAdapter,才会将list
数据传递给适配器。将列表数据传递到RecyclerAdapter类,并通过Adapter类中的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/