如何在FirebaseRecyclerAdapter中选择值?

时间:2016-09-20 16:26:27

标签: android firebase firebase-realtime-database firebaseui

我想使用FirebaseRecyclerAdapter从Realtime DB获取确定的值。这是我的班级:

public class ProductAvtivity extends AppCompatActivity {


    private final String TAG = "ProductActivity";

    private RecyclerView mRecyclerView;
    private RecyclerView.LayoutManager mLayoutManager;

    private FirebaseDatabase database;
    private DatabaseReference mProductReference;
    private Query mMachineReference;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_avtivity);

        database = FirebaseDatabase.getInstance();
        mProductReference = database.getReference().child("products");


        String idmachine = getIntent().getStringExtra("idmachine");
        mMachineReference = database.getReference().child("machines").child("_id").equalTo(idmachine);
        Log.d(TAG + "idmachine",idmachine);



        mRecyclerView = (RecyclerView) findViewById(R.id.product_recyclerview);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);

        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);



        FirebaseRecyclerAdapter<Product,ProductViewHolder> adapter =
                new FirebaseRecyclerAdapter<Product, ProductViewHolder>(Product.class,R.layout.view_product,ProductViewHolder.class,mProductReference) {
                    @Override
                    protected void populateViewHolder(ProductViewHolder viewHolder, Product model, int position) {
                        viewHolder.productName.setText(model.name);
                    }
                };

        mRecyclerView.setAdapter(adapter);

    }

    public static class ProductViewHolder extends RecyclerView.ViewHolder{

        TextView productName;

        public ProductViewHolder(View itemView) {
            super(itemView);
            productName = (TextView) itemView.findViewById(R.id.product_name);
        }
    }
}

我尝试使用此解决方案但由于活动崩溃而无法正常工作:

公共类ProductAvtivity扩展了AppCompatActivity {

    private final String TAG = "ProductActivity";

    private RecyclerView mRecyclerView;
    private RecyclerView.LayoutManager mLayoutManager;

    private FirebaseDatabase database;
    private DatabaseReference mProductReference;
    private Query mMachineReference;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_avtivity);

        database = FirebaseDatabase.getInstance();
        mProductReference = database.getReference().child("products");


        String idmachine = getIntent().getStringExtra("idmachine");
        mMachineReference = database.getReference().child("machines").child("_id").equalTo(idmachine);

        Log.d(TAG + "idmachine",idmachine);



        mRecyclerView = (RecyclerView) findViewById(R.id.product_recyclerview);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);

        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);



        FirebaseRecyclerAdapter<Product,ProductViewHolder> adapter =
                new FirebaseRecyclerAdapter<Product, ProductViewHolder>(Product.class,R.layout.view_product,ProductViewHolder.class,mProductReference) {
                    @Override
                    protected void populateViewHolder(final ProductViewHolder viewHolder, final Product model, int position) {
                        mMachineReference.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                Machine machine = dataSnapshot.getValue(Machine.class);
                                for (Integer i : machine.id_prod){
                                    if (model._id == i){
                                        viewHolder.productName.setText(model.name);
                                    }
                                }
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });

                    }
                };

        mRecyclerView.setAdapter(adapter);

    }

    public static class ProductViewHolder extends RecyclerView.ViewHolder{

        TextView productName;

        public ProductViewHolder(View itemView) {
            super(itemView);
            productName = (TextView) itemView.findViewById(R.id.product_name);
        }
    }
}

我有一个包含所有计算机ID的产品列表,然后我从另一个用户选择机器的活动中传递了该列表。通过这种选择,我必须在recyclerview中列出与之相关的产品。

0 个答案:

没有答案