I'm able to fetch data from firebase and put into an arraylist. I wanted to know how to display this arraylist using recyclerview.
我能够从firebase获取数据并放入arraylist。我想知道如何使用recyclerview显示这个arraylist。
public class HomeFragment extends Fragment {
public List<Product> list = new ArrayList<Product>();
public HomeFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
//firebase db
FirebaseDatabase mFirebaseInstance = FirebaseDatabase.getInstance();
DatabaseReference mFirebaseDatabase = mFirebaseInstance.getReference();
DatabaseReference refproducts = mFirebaseDatabase.child("products");
//listening for value changes
refproducts.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot productSnapshot: snapshot.getChildren()) {
long value = snapshot.getChildrenCount();
Product product = productSnapshot.getValue(Product.class);
list.add(product);
Log.d("product",list.get(0).getProductName() +" "+ value);
}
}
@Override
public void onCancelled(DatabaseError error) {
Log.e("Product", "The read failed. ");
}
});
return view;
}
}
Product.java
I'm able Log product name of specified index but when I tried to populate it in a listview, the products aren't displaying.
我能够记录指定索引的产品名称但是当我尝试在列表视图中填充它时,产品不会显示。
public Product(String name, double available_quantity, double price, String
id, String downloadUrl) {
this.name = name;
this.available_quantity = available_quantity;
this.price=price;
this.id=id;
this.uri=downloadUrl;
}
Recyclerview Adapter I want to know whether I've made any mistakes in code.
Recyclerview适配器我想知道我是否在代码中犯了任何错误。
public class RecyclerAdapter extends
RecyclerView.Adapter<RecyclerAdapter.ProductViewHolder> {
private List<Product> list = new ArrayList<>();
public RecyclerAdapter(List<Product> list) {
this.list = list;
}
public class ProductViewHolder extends RecyclerView.ViewHolder {
View mView;
TextView textView_name;
TextView textView_quantity;
TextView textView_price;
ImageView imageView;
Button Order;
public ProductViewHolder(View itemView) {
super(itemView);
mView = itemView;
textView_name = (TextView) itemView.findViewById(R.id.productname);
textView_price = (TextView) itemView.findViewById(R.id.productprice);
textView_quantity = (TextView) itemView.findViewById(R.id.productQuantity);
imageView = (ImageView) itemView.findViewById(R.id.productimage);
Order = (Button) itemView.findViewById(R.id.orderbutton);
}
public void setName(String title) {
textView_name.setText(title);
}
public void setQunatity(Double quantity) {
textView_quantity.setText("Available Quantity: " + Double.toString(quantity));
}
public void setPrice(Double price) {
textView_price.setText("Price: " + Double.toString(price));
}
public void setImage(String image) {
/*Picasso.with(mView.getContext())
.load(image)
.into(imageView);*/
Uri uri = Uri.parse(image);
imageView.setImageURI(uri);
}
}
@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.product_list_item, parent, false);
return new ProductViewHolder(itemView);
}
@Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
Product product = list.get(position);
holder.setName(product.getProductName());
holder.setQunatity(product.getQuantity());
holder.setPrice(product.getPrice());
holder.setImage(product.getUri());
}
@Override
public int getItemCount() {
return list.size();
}
}
2 个解决方案
#1
0
Add recylcerview inside your XML and initialize like this
在XML中添加recylcerview并像这样初始化
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Initialize your recyclerview like this in your activity
在您的活动中初始化您的recyclerview
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
Add recycler view dependency in your build.gradle
在build.gradle中添加recycleler视图依赖项
com.android.support:recyclerview-v7:23.1.1
Add this code inside your onDataChange
after the for
loop
在for循环之后,在onDataChange中添加此代码
RecyclerAdapter mAdapter = new RecyclerAdapter(list);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
Hope this will help you and solve your problem
希望这会帮助您解决您的问题
#2
0
If you want to show some elements using listview you should use adapter. Example with BaseAdapter
如果要使用listview显示某些元素,则应使用适配器。 BaseAdapter的示例
#1
0
Add recylcerview inside your XML and initialize like this
在XML中添加recylcerview并像这样初始化
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Initialize your recyclerview like this in your activity
在您的活动中初始化您的recyclerview
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
Add recycler view dependency in your build.gradle
在build.gradle中添加recycleler视图依赖项
com.android.support:recyclerview-v7:23.1.1
Add this code inside your onDataChange
after the for
loop
在for循环之后,在onDataChange中添加此代码
RecyclerAdapter mAdapter = new RecyclerAdapter(list);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
Hope this will help you and solve your problem
希望这会帮助您解决您的问题
#2
0
If you want to show some elements using listview you should use adapter. Example with BaseAdapter
如果要使用listview显示某些元素,则应使用适配器。 BaseAdapter的示例