我想让Arraylist显示在列表视图中。我该如何制作?

时间:2017-09-16 15:20:40

标签: android android-layout listview android-recyclerview android-arrayadapter

我可以从firebase获取数据并放入arraylist。我想知道如何使用arraylist显示此recyclerview

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

我可以使用指定的索引记录产品,但是当我尝试在列表视图中填充它时,产品没有显示。

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适配器 我想知道我是否在代码中犯了任何错误。

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 个答案:

答案 0 :(得分:0)

如果要使用listview显示某些元素,则应使用适配器。 Example with BaseAdapter

答案 1 :(得分:0)

在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"/>

在您的活动

中初始化您的recyclerview
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

build.gradle

中添加回收站视图相关性
com.android.support:recyclerview-v7:23.1.1

onDataChange循环

之后的for内添加此代码
RecyclerAdapter mAdapter = new RecyclerAdapter(list);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);

希望这会帮助你并解决你的问题