Is it possible to get reference to ListView from Adapter in Android?
Asked Answered
H

3

16

Is it possible to get reference to ListView from Adapter in Android without passing it as an argument to constructor?

Holst answered 28/11, 2012 at 9:59 Comment(4)
No, but reverse is possible.Shroud
You're supposed to do it the other way... Adapter should not know about the UI component which it is being used. It is simply a provider, and does not care about who is receiving the data. Although the ListView knows and cares about which adapter is feeding the data, and provides a way to change it if necessary. I'm sorry to say, but something is up-side-down in your architecture...Charlton
@Charlton thanks for answer. I use ExpandableListView, and I need to collapse all the groups when expand another. So I override onExpandGroup in adapter. So I need reference to listviewHolst
I assume you have an implementation of ExpandableListView.OnGroupExpandListener . Can you pass the reference to the list view to its constructor ?. (I assume you set it using setOnGroupExpandListener at the constructor of your activity, which 'knows' the List View)Charlton
L
12

It is definitely possible. Should it be done or not (Yes there are cases).

// See this method of your adapter
// The parent is the view you are looking for
public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
{
    ListView view = (ListView)parent;
}
Labiodental answered 26/6, 2013 at 1:14 Comment(5)
Right, working :) although i'm dealing with expandableListView but got the idea from you +1.Court
Wonderful! So simple.Monophyletic
That's fine, but is it a good practice or should it be avoided?Blessington
It is a harmful method. Practice depends on who is using this and howLabiodental
I meant harmless. SorryLabiodental
B
0
public class ItemListAdapter extends RecyclerView.Adapter<IItemListAdapter.ViewHolder> {
    Context context;
    ArrayList<ProductModel> designerCollection;
    ArrayList<ProductModel> designerCollectionforreward;
    ArrayAdapter<String> adapter;
    ListItemClickListener listItemClickListener;
    boolean isProducthide;
    boolean isEditable;
    ProductModel myListDataforReward;


    public ItemListAdapter(Context context, ArrayList<ProductModel> designerCollection, ArrayList<ProductModel> designerCollectionforreward, boolean isProducthide, boolean isEditable, ListItemClickListener listItemClickListener) {
        this.context = context;
        this.designerCollection = designerCollection;
        this.designerCollectionforreward = designerCollectionforreward;
        this.listItemClickListener = listItemClickListener;
        this.isProducthide = isProducthide;
        this.isEditable = isEditable;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View listItem = layoutInflater.inflate(R.layout.new_layout_cart_product, parent, false);
        ViewHolder viewHolder = new ViewHolder(listItem);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final ProductModel myListData = designerCollection.get(position);
        if (designerCollectionforreward != null) {
            myListDataforReward = designerCollectionforreward.get(position);
        }
        Glide.with(holder.itemView)
                .load(myListData.getProductImage())
                .placeholder(R.drawable.noimage)
                .into(holder.iv_product_image);
        if (isProducthide) {
            holder.iv_product_image.setVisibility(View.VISIBLE);
        } else {
            holder.iv_product_image.setVisibility(View.GONE);
        }
        holder.tv_product_price.setText(myListData.getProductPrice());
        holder.imgDelete.setVisibility(View.VISIBLE);

        holder.imgDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                notifyDataSetChanged();
                listItemClickListener.onItemClick(position, myListData.getItemQuantity(), myListData.getShoppingId());
            }
        });

        holder.tv_product_name.setText(myListData.getProductName());
        holder.txt_quantity.setText(myListData.getItemQuantity());
        holder.tv_product_qty.setText(myListData.getItemQuantity());

        if (myListData.isRemoveDisable()) {
            holder.imgDelete.setVisibility(View.GONE);
        } else {
            holder.imgDelete.setVisibility(View.VISIBLE);
        }

//        holder.tv_require.setText(myListData.getRequireText());
        if (myListDataforReward != null) {
            if (myListDataforReward.getProductRewardPoint() != null && !myListDataforReward.getProductRewardPoint().isEmpty()) {
                holder.ll_product_reward_point.setVisibility(View.VISIBLE);
                if (Constants.hashMap.containsKey("rewardpoint.plugin.rewardpoints.willearn")) {
                    holder.tv_reward_title.setText(Constants.hashMap.get("rewardpoint.plugin.rewardpoints.willearn"));
                } else {
                    holder.tv_reward_title.setText("You will earn : ");
                }
                holder.tv_reward_point.setText(myListDataforReward.getProductRewardPoint() + " "
                        + Constants.hashMap.get("admin.customers.customers.rewardpoints.fields.points"));
            } else {
                holder.ll_product_reward_point.setVisibility(View.GONE);
            }
        } else {
            holder.ll_product_reward_point.setVisibility(View.GONE);
        }
        if (myListData.getAttributeString() != null && !myListData.getAttributeString().equals("")) {
            holder.btnEdit.setVisibility(View.VISIBLE);
            holder.txtAttribute.setVisibility(View.VISIBLE);
            holder.txtAttribute.setText(myListData.getAttributeString());
        } else {
            holder.txtAttribute.setVisibility(View.GONE);
            holder.btnEdit.setVisibility(View.GONE);
        }
        /*if (myListData.getAttributeString().equals("")) {
            holder.ll_editProduct.setVisibility(View.GONE);
            holder.btnEdit.setVisibility(View.GONE);
        }*/
        /*if (isEditable) {
            holder.ll_editProduct.setVisibility(View.VISIBLE);
            holder.btnEdit.setVisibility(View.VISIBLE);
        } else {
            holder.btnEdit.setVisibility(View.GONE);
        }*/

        holder.tv_main_price.setText(myListData.getItemsubtotal());
        int qty = Integer.parseInt(designerCollection.get(position).getItemQuantity());
        int maxQty = designerCollection.get(position).getOrder_maximum_quantity();

            holder.plus_quantity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    if (qty < maxQty) {
                    int qty = Integer.parseInt(designerCollection.get(position).getItemQuantity());
                    holder.txt_quantity.setText(String.valueOf(qty + 1));
                    designerCollection.get(position).setItemQuantity(String.valueOf(qty + 1));
                    notifyDataSetChanged();
                    listItemClickListener.onItemClick(position, String.valueOf(1), "quantity");

//                    } else {
//                        Toast.makeText(context.getApplicationContext(), "", Toast.LENGTH_SHORT).show();
//                    }
                }
            });
        holder.minus_quantity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int qty = Integer.parseInt(designerCollection.get(position).getItemQuantity());
                if (qty > 1) {
                    if (qty == Integer.parseInt(designerCollection.get(position).getOrder_minimum_quantity())) {
                        Toast.makeText(context.getApplicationContext(), "minimum quantity must be " + Integer.parseInt(designerCollection.get(position).getOrder_minimum_quantity()), Toast.LENGTH_SHORT).show();
                    } else {
                        holder.txt_quantity.setText(String.valueOf(qty - 1));
                        designerCollection.get(position).setItemQuantity(String.valueOf(qty - 1));
                        listItemClickListener.onItemClick(position, String.valueOf(-1), "quantity");
                    }
                }
            }
        });

        holder.btnEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, ProductDetailActivity.class)
                        .putExtra(Constants.INTENT_PRODUCT_ID, myListData.getProductId())
                        .putExtra(Constants.productAttribute, (Serializable) myListData.getProductAttributeValueModelList())
                        .putExtra(Constants.isFromCartScreen, true));
                ((Activity) context).finish();
            }
        });
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView plus_quantity, minus_quantity;
        CardView cart;
        public ImageView iv_product_image, img_dropDown;
        TextView tv_product_name, tv_product_price, txt_quantity, tv_product_qty, txtAttribute, tv_main_price;
        CustomSpinner ll_spinner;
        LinearLayout ll_click, ll_editProduct, ll_product_reward_point;
        CircleImageView imgDelete;

        TextView txtPrice, txtQuant, txtTotal, tv_reward_title, tv_reward_point;
        CircleImageView btnEdit;

        public ViewHolder(View itemView) {
            super(itemView);
            this.setIsRecyclable(false);
            iv_product_image = (ImageView) itemView.findViewById(R.id.iv_product_image);
            tv_product_name = itemView.findViewById(R.id.tv_product_name);
            ll_product_reward_point = itemView.findViewById(R.id.ll_product_reward_point);
//            tv_product_names = itemView.findViewById(R.id.tv_product_names);
            imgDelete = itemView.findViewById(R.id.imgDelete);
//            cart = itemView.findViewById(R.id.cart);

            btnEdit = itemView.findViewById(R.id.btnEdit);
//            product_tags = itemView.findViewById(R.id.product_tags);

            tv_reward_title = itemView.findViewById(R.id.tv_reward_title);
            tv_reward_point = itemView.findViewById(R.id.tv_reward_point);
//            tv_require = itemView.findViewById(R.id.tv_require);
            txtPrice = itemView.findViewById(R.id.txtPrice);
            txtQuant = itemView.findViewById(R.id.txtQty);
            txtTotal = (TextView) itemView.findViewById(R.id.txtTotal);

            plus_quantity = (TextView) itemView.findViewById(R.id.plus_quantity);
            minus_quantity = (TextView) itemView.findViewById(R.id.minus_quantity);
//            text_quantity = (TextView) itemView.findViewById(R.id.txt_quantity);
            tv_product_name = (TextView) itemView.findViewById(R.id.tv_product_name);
            tv_product_price = (TextView) itemView.findViewById(R.id.tv_product_price);
            txt_quantity = (TextView) itemView.findViewById(R.id.txt_quantity);
            tv_product_qty = (TextView) itemView.findViewById(R.id.tv_product_qty);
            txtAttribute = (TextView) itemView.findViewById(R.id.txtAttributeName);
            tv_main_price = (TextView) itemView.findViewById(R.id.tv_main_price);
            ll_click = itemView.findViewById(R.id.ll_click);
            ll_editProduct = itemView.findViewById(R.id.ll_editProduct);
            if (Constants.hashMap.containsKey("admin.catalog.products.fields.price"))
                txtPrice.setText(Constants.hashMap.get("admin.catalog.products.fields.price"));
            if (Constants.hashMap.containsKey("shoppingcart.quantity"))
                txtQuant.setText(Constants.hashMap.get("shoppingcart.quantity"));
            if (Constants.hashMap.containsKey("admin.currentcarts.total"))
                txtTotal.setText(Constants.hashMap.get("admin.currentcarts.total"));
        }
    }

    public void removeItem(int position) {
        designerCollection.remove(position);
        notifyItemRemoved(position);
    }

    public void restoreItem(ProductModel item, int position) {
        designerCollection.add(position, item);
        notifyItemInserted(position);
    }

    public ArrayList<ProductModel> getData() {
        return designerCollection;
    }
}
Burns answered 5/10, 2021 at 8:28 Comment(0)
T
-5

No it is not possible, layout should be known to adapter.

Tyronetyrosinase answered 28/11, 2012 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.