Firebase Database

enter image description here

In this Image I have 3 data's which are sohar chips,oman chips and strawberry milk.

My next step in the project is to get the information which is the title of the card view and store them in a textview which will be used later. CardView

Problem: On getting the information I have tried using onClick button to retrieve the information of a single card view. The problem is that every time I clicked a card view for example sohar chips, I keep on getting three windows open directly like sohar,oman and strawberry milk.

I only want to retrieve the information on single card view not all of them.

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 Answers

0
BlackBlind On Best Solutions

First give cardview some specific id in your recyclerview Xml file.

Now, This is your adapter class.

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);
    }
}

}

I update your adapter with cardview and its listner Please checkout and any problem please comment below.