如果使用Firebase数据库单击,如何获取卡片视图的信息?

时间:2019-05-16 11:43:31

标签: android firebase firebase-realtime-database

Firebase数据库

enter image description here

在此图像中,我有3个数据,分别是苏哈尔片,阿曼片和草莓牛奶。

我在项目中的下一步是获取卡视图标题的信息,并将其存储在文本视图中,稍后将使用。 CardView

问题:在获取信息后,我尝试使用onClick按钮检索单个卡片视图的信息。问题是,每次我单击卡视图(例如,苏哈尔芯片)时,我都会继续直接打开三个窗口,如苏哈尔,阿曼和草莓牛奶。

我只想在单张卡视图上检索信息,而不是全部。

public class ViewHolder extends RecyclerView.Adapter<ViewHolder.ImageViewHolder> {
    private Context mContext;
    private List<Model> mModel;


    public ViewHolder(Context context, List<Model> models)
    {
        mContext=context;
        mModel=models;

    }
    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.row, viewGroup,false);
        return new ImageViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder imageViewHolder, int i) {
        Model mModelcur = mModel.get(i);
        imageViewHolder.img_description.setText(mModelcur.getDescription());
        imageViewHolder.img_title.setText(mModelcur.getTitle());
        Picasso.get().load(mModelcur.getImage())
                    .into(imageViewHolder.image_view, new Callback() {
                        @Override
                        public void onSuccess() {

                        }

                        @Override
                        public void onError(Exception e) {
                            Log.i("log" , e.getMessage());
                        }
                    });


    }

    @Override
    public int getItemCount() {
        return mModel.size();
    }



    public class ImageViewHolder extends RecyclerView.ViewHolder
    {
        public TextView img_description;
        public TextView img_title;
        public ImageView image_view;
        public ImageViewHolder(@NonNull View itemView) {
            super(itemView);
            img_description = itemView.findViewById(R.id.rDescription);
            image_view = itemView.findViewById(R.id.rImageview);
            img_title = itemView.findViewById(R.id.rTextview);
        }
    }

}
public class Ordering extends AppCompatActivity {


    private RecyclerView mRecyclerView;
    private ViewHolder mAdapter;
    private DatabaseReference mDatabaseReference;
    private List<Model> mModel;

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

        mRecyclerView= findViewById(R.id.recyclerview);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mModel = new ArrayList<>();
        mAdapter=new ViewHolder(Ordering.this, mModel);
        mRecyclerView.setAdapter(mAdapter);
        mDatabaseReference=FirebaseDatabase.getInstance().getReference("Data");
        mDatabaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot postSnapshot:dataSnapshot.getChildren())
                {
                    Model model=postSnapshot.getValue(Model.class);
                    mModel.add(model);
                }
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(Ordering.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
            }
        });

    }

    public void onDetails(final View view) {
        final List<Model> mModel = new ArrayList<>();
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Data");
        ref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for(DataSnapshot postSnapshot:dataSnapshot.getChildren())
                {
                    String title = postSnapshot.child("title").getValue().toString();
                    Intent intent = new Intent(view.getContext(),Detailsoforder.class);
                    intent.putExtra("title",title);
                    startActivity(intent);

                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

    }
}

row.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/parentLayout"
    android:onClick="onDetails">
    <TextView
        android:id="@+id/rTextview"
        android:text="Title"
        android:textColor="#000"
        android:textSize="22sp"
        android:textStyle="bold"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/rImageview"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"
        android:background="@drawable/loading"/>
    <TextView
        android:id="@+id/rDescription"
        android:text="This will be the description"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

首先在您的recyclerview Xml文件中给cardview一些特定的ID。

现在,这是您的适配器类。

public class ViewHolder extends RecyclerView.Adapter<ViewHolder.ImageViewHolder> {
private Context mContext;
private List<Model> mModel;


public ViewHolder(Context context, List<Model> models)
{
    mContext=context;
    mModel=models;

}
@NonNull
@Override
public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(mContext).inflate(R.layout.row, viewGroup,false);
    return new ImageViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull ImageViewHolder imageViewHolder, int i) {
    Model mModelcur = mModel.get(i);
    imageViewHolder.img_description.setText(mModelcur.getDescription());
    imageViewHolder.img_title.setText(mModelcur.getTitle());

    imageViewHolder.yourcardview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) { 
            Intent intent = new Intent(mcontext, yourWantedActivity.class);
             intent.putExtra("your key", mMOdel.get(i).getTitle());
            mcontext.startActivity(intent);
        }
    });

   Picasso.get().load(mModelcur.getImage())
                .into(imageViewHolder.image_view, new Callback() {
                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onError(Exception e) {
                        Log.i("log" , e.getMessage());
                    }
                });


}

@Override
public int getItemCount() {
    return mModel.size();
}



public class ImageViewHolder extends RecyclerView.ViewHolder
{
    public TextView img_description;
    public TextView img_title;
    public ImageView image_view;
    public YourCardView yourcardview;

    public ImageViewHolder(@NonNull View itemView) {
        super(itemView);
        img_description = itemView.findViewById(R.id.rDescription);
        image_view = itemView.findViewById(R.id.rImageview);
        img_title = itemView.findViewById(R.id.rTextview);

        yourcardview = item.findViewById(R.id.yourcardviewId);
    }
}

}

我使用cardview及其列表器更新了您的适配器,请结帐,如有任何问题,请在下面评论。