'How to identify the source of a context object in android java?

Overview

I am working on android application in java. I have Products which can be viewed as a list by customers and vendors.

Problem

I want to be able to utilize the context object that is passed to the AdapterClass class as an argument when the adapter is initialized to identify if the adapter is being used to view products by a customer-specific activity or a vendor-specific activity.

This would eliminate the need for two separate adapter classes for displaying products to customers and vendors and also for implementing separate functionalities for both.

For this purpose, inside the adapter, I need to identify the origin of the received context.

How do I do this?

public class AdapterClass extends RecyclerView.Adapter<AdapterClass.MyViewHolder> {

Context context;
List<String> productList;

public AdapterClass(Context context, List<String> productList){
    this.context=context;
    this.productList = productList;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    //these if-else conditions are dummy code
    if(context is from ActivityVendor){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.vendor_list_item_layout, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }
    else if(context is from ActivityCustomer){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.customer_list_item_layout, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    if(context is from ActivityVendor){
        //code for vendor
    }
    else if(context is from ActivityCustomer){
        //code for customer
    }
}

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

public class MyViewHolder extends RecyclerView.ViewHolder {
    ImageView imageView;
    TextView textView;
    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        if(context is from ActivityVendor){
            imageView=itemView.findViewById(R.id.image);
            textView=itemView.findViewById(R.id.text);
        }
        else if(context is from ActivityCustomer){
            imageView=itemView.findViewById(R.id.image2);
            textView=itemView.findViewById(R.id.text2);
        }
    }
}


Solution 1:[1]

To determine what code shall do, based up the context it runs within commonly is sub-optimal (it's not that one couldn't do it - but within one single Activity, this is pointless). Most commonly one might want to data-bind eg. a groupId and then display either view-type ID of a CardView layout, depending on the current user group. Meanwhile with MAD, it is rather the situation, that as soon a user has a different role/group, one can simply provide them with their own navigation graph ...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1