Search code examples
javaandroidandroid-studioandroid-recyclerviewfirebaseui

How to retrieve data from an item that was clicked from an onclick listener


i have a recycler view that has many shops in my app. each app when clicked opens a new activity that should display all the details of that specific shop.

now i created the onclick listener and everything is working fine and i am even going to the next activity which is detailsActivity.

My problem is that how can i pass the information of each individual shop into the detailsActivity after it has been clicked in the HomeActivity.

here is my Shop Class:

private String name;
private String country;
private String address;
private String location;
private String ShopHeaderImg;
private String ShopProfileImg;
private String shopPID;

//constructor
public Shop(){

}

//constructor with parameters
public Shop(String name, String country, String address, String location, String shopHeaderImg, String shopProfileImg, String shopPID) {
    this.name = name;
    this.country = country;
    this.address = address;
    this.location = location;
    this.ShopHeaderImg = shopHeaderImg;
    ShopProfileImg = shopProfileImg;

    this.shopPID = shopPID;
}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}


public String getShopHeaderImg() {
    return ShopHeaderImg;
}

public void setShopHeaderImg(String shopHeaderImg) {
    ShopHeaderImg = shopHeaderImg;
}


public String getShopProfileImg() {
    return ShopProfileImg;
}

public void setShopProfileImg(String shopProfileImg) {
    ShopProfileImg = shopProfileImg;
}


public String getShopPID() {
    return shopPID;
}

public void setShopPID(String shopPID) {
    this.shopPID = shopPID;
}
 }

and here is my HomeActivity:

private FirebaseFirestore firebaseFirestore;
    private RecyclerView FirestoreList;
    private FirestoreRecyclerAdapter adapter;

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);


        FirestoreList = findViewById(R.id.recycler_view);
        firebaseFirestore = FirebaseFirestore.getInstance();

  Query q = firebaseFirestore.collection("Shops");

    //recycle options
    FirestoreRecyclerOptions<Shop> options = new FirestoreRecyclerOptions.Builder<Shop>()
            .setQuery(q, Shop.class)
            .build();

    adapter = new FirestoreRecyclerAdapter<Shop, ShopViewHolder>(options) {
        @NonNull
        @Override
        public ShopViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.shop_item,parent,false);
            //onclick of cardview
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Context context = v.getContext();
                    Intent intent = new Intent(context, DetailsActivity.class);
                    context.startActivity(intent);

                }
            });
            return new ShopViewHolder(view);
        }

        @RequiresApi(api = Build.VERSION_CODES.M)
        @Override
        protected void onBindViewHolder(@NonNull ShopViewHolder holder, int position, @NonNull Shop model) {
            holder.list_name.setText(model.getName());
            holder.list_location.setText(model.getLocation());
            holder.list_uid.setText(model.getShopPID());
            holder.setHeaderImage(getApplicationContext(),model.getShopHeaderImg());
            holder.list_profileImage(getApplicationContext(),model.getShopProfileImg());

    }
    };

    FirestoreList.setHasFixedSize(true);
    FirestoreList.setLayoutManager(new LinearLayoutManager(this));
    FirestoreList.setAdapter(adapter);

}

private class ShopViewHolder extends RecyclerView.ViewHolder {
    private TextView list_name;
    private TextView list_location;
    private TextView list_uid;
    private ImageView header_img;
    private ImageView list_profileImage;




    public ShopViewHolder(@NonNull View itemView) {
        super(itemView);



        list_name = itemView.findViewById(R.id.list_name);
        list_location = itemView.findViewById(R.id.list_location);
        list_uid = itemView.findViewById(R.id.list_uid);
        header_img = itemView.findViewById(R.id.header_img);
        list_profileImage = itemView.findViewById(R.id.list_profileImage);

    }

    public void setHeaderImage(final Context c , final String Image) {
        final ImageView headerImg = (ImageView)itemView.findViewById(R.id.header_img);
        Picasso.get().load(Image).into(headerImg);

    }
    public void list_profileImage(final Context c , final String image) {
        final ImageView profileImg = (ImageView)itemView.findViewById(R.id.list_profileImage);
        Picasso.get().load(image).into(profileImg);

    }
}

@Override
public void onStart() {
    super.onStart();
    adapter.startListening();
}

@Override
public void onStop() {
    super.onStop();
    adapter.stopListening();
}

this is my DetailsActivity:

public class DetailsActivity extends AppCompatActivity {


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


    }
}

now when i click on any of the shops cards or images it opens up the DetailsActivity. my problem is with how to get that specific shops info into the new detailsActivity from the DB such as name,location,address etc.


Solution

  • Nidhessh is right to ask you to move the onCLickListener to the onBindViewHolder, but I would do it this way:

        @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            protected void onBindViewHolder(@NonNull ShopViewHolder holder, int position, @NonNull Shop model) {
                holder.list_name.setText(model.getName());
                holder.list_location.setText(model.getLocation());
                holder.list_uid.setText(model.getShopPID());
                holder.setHeaderImage(getApplicationContext(),model.getShopHeaderImg());
                holder.list_profileImage(getApplicationContext(),model.getShopProfileImg());
    
    //add this lines
    holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                       
                        Intent intent = new Intent(context, DetailsActivity.class);
                        intent.putExtra("shopModel", model);
                        context.startActivity(intent);
    
                    }
                });
        
    

    Make your Shop class implement Serializable, like this:

        public class Shop implements Serializable { 
    ...
    //all remains the same in that class
     }
    

    After in your DetailsActivity write this to get the selected shop model:

    Shop shopModel = getIntent().getSerializableExtra("shopModel");