如何从另一个适配器更改一个适配器值?

时间:2018-06-05 05:47:07

标签: java android

我有点问题。在我的活动中,有两个适配器用于颜色选择,另一个用于尺寸选择。虽然我点击其中一个颜色项目,然后最近可用的大小适配器应该更改,但我在尺寸适配器有问题。它只在我点击尺寸项目时才会改变。我研究并试图解决问题,但它对我没有用。

这是我的代码。

AddToCartActivity.java

public class AddToCartActivity extends BaseActivity{
    @Override
    protected int getLayout() {
        return R.layout.activity_add_to_cart;
    }

    @Override
    protected void init() {
        //api called here
    }

     // response of api 
    @Override
    public void productDetail(ProductCommonModel productCommonModel, 
    ArrayList<ProductChildModel> productChildModels, HashMap<Integer, 
    ArrayList<ChildAttributeModel>> childWithAttribute, HashMap<Integer, 
    ArrayList<ChildImageModel>> childWithImages, 
    ArrayList<com.hazesoft.dokan.singleproductdetail.model.ColorModel> 
    colorModels, ArrayList<SizeModel> sizeModels, 
    ArrayList<RelatedProductModel> relatedProductModels) {
        this.productCommonModel = productCommonModel;
        this.productChildModels = productChildModels;
        this.childWithAttribute = childWithAttribute;
        this.childWithImages = childWithImages;
        this.colorModels = colorModels;
        this.sizeModels = sizeModels;
        this.relatedProductModels = relatedProductModels;
        tvProductName.setText(productCommonModel.getName());
        if (productCommonModel.getSpecialPrice() == 0) {
            tvSellingPrice.setText(getString(R.string.rs) + productCommonModel.getSellingPrice());
            tvDiscount.setVisibility(View.GONE);
            tvSpecialPrice.setVisibility(View.GONE);
        } else {
            tvSpecialPrice.setText(getString(R.string.rs) + productCommonModel.getSpecialPrice());
            tvSellingPrice.setText(getString(R.string.rs) + productCommonModel.getSellingPrice());
            tvSellingPrice.setPaintFlags(tvSellingPrice.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            tvDiscount.setText(productCommonModel.getDiscount() + getString(R.string.percentage));
        }
        setChildDetail(childWithAttribute, productChildModels);
        setColorModel(colorModels);
        setSizeModel(sizeModels);
        quantity = Integer.parseInt(tvQuantityCart.getText().toString());
    }

      // setcolor adapter
      private void setColorModel(ArrayList<ColorModel> colorModels) {
        MyColorGridViewAdapter adapter = new MyColorGridViewAdapter(this, colorModels);
        gvColor.setAdapter(adapter);
        gvColor.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setSelectedPostion(position);
                adapter.notifyDataSetChanged();
            }
        });
    }


  // set size adapter
    private void setSizeModel(ArrayList<SizeModel> sizeModels) {
        sizeCustomModels = new ArrayList<>();
        for(int i=0;i<sizeModels.size();i++){
            sizeCustomModels.add(new SizeCustomModel(sizeModels.get(i).getAttName(),0));
        }
        setCustomSizeModelToAdapter(sizeCustomModels);
    }


// this is code when i click color and change the size adapter but size doesnt change recently only changes when i click any item of the size
public void getSelectedC0lor(String color) {
        selectedColor = color;
        selectedSize=null;
        sizeCustomModels = new ArrayList<>();
        availableSize = new ArrayList<>();
        for (int i = 0; i < skuColorSIzeList.size(); i++) {
            if (skuColorSIzeList.get(i).getColor().equals(selectedColor)) {
                availableSize.add(skuColorSIzeList.get(i).getSize());
            }
        }
        for(int i=0;i<sizeModels.size();i++){
            String size = null;
            int status=0;
            for(int j=0;j<availableSize.size();j++){
                if(sizeModels.get(i).getAttName().equals(availableSize.get(j))){
                    size = sizeModels.get(i).getAttName();
                    status = 1;
                    break;
                }else {
                    size = sizeModels.get(i).getAttName();
                    status = 0;
                }
            }
            sizeCustomModels.add(new SizeCustomModel(size,status));
        }
        sizeRecylerAdapter.getNewModel(sizeCustomModels);
        /*sizeRecylerAdapter = new MyCustomSizeRecylerAdapter(sizeCustomModels,this);
        rvSize.setAdapter(sizeRecylerAdapter);
        sizeRecylerAdapter.notifyDataSetChanged();*/
        /*setCustomSizeModelToAdapter(sizeCustomModels);*/

    }

}

MyColorGridViewAdapter.java

public class MyColorGridViewAdapter extends BaseAdapter {

    Context context;
    List<ColorModel> colorModelList;
    String select_color;
    boolean ch =false;
    int checkPosition = -1;

    public MyColorGridViewAdapter(Context context, List<ColorModel> colorModelList) {
        this.context = context;
        this.colorModelList = colorModelList;
    }

    public void setSelectedPostion(int postion){
        this.checkPosition = postion;

    }

    @Override
    public int getCount() {
        return colorModelList.size();
    }

    @Override
    public Object getItem(int position) {
        return colorModelList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.custom_color_list_item,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        Picasso.with(context).load(colorModelList.get(position).getImage()).into(holder.ivImage);
        holder.tvColorName.setText(colorModelList.get(position).getAttName());
        if(checkPosition==position){
            holder.ivChecked.setVisibility(View.VISIBLE);
            select_color = colorModelList.get(position).getAttName();
            if( context instanceof AddToCartActivity){
                ((AddToCartActivity) context).getSelectedC0lor(select_color);
            }
        }else {
            holder.ivChecked.setVisibility(View.GONE);
        }

        if(colorModelList.size()==1){
            holder.ivChecked.setVisibility(View.VISIBLE);
            select_color = colorModelList.get(position).getAttName();
            if( context instanceof AddToCartActivity){
                ((AddToCartActivity) context).getSelectedC0lor(select_color);
            }
        }
        return convertView;
    }

    class ViewHolder{
        @BindView(R.id.view)
        LinearLayout view;
        @BindView(R.id.tv_color_name)
        TextViewHelper tvColorName;
        @BindView(R.id.iv_image)
        ImageView ivImage;
        @BindView(R.id.iv_checked)
        ImageView ivChecked;

        public ViewHolder(View view) {
            ButterKnife.bind(this,view);
        }
    }
}

MyCustomSizeRecylerAdapter.java

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

    ArrayList<SizeCustomModel> sizeModels;
    Context context;
    int checkPosition = -1;
    String selectedSize;


    public MyCustomSizeRecylerAdapter(ArrayList<SizeCustomModel> sizeModels, Context context) {
        this.sizeModels = sizeModels;
        this.context = context;
    }

    public void getNewModel(ArrayList<SizeCustomModel> customModels) {
        sizeModels.clear();
        this.sizeModels = customModels;
        selectedSize = null;
        Log.d("sizemodel", "getNewModel: " + new Gson().toJson(sizeModels));
        notifyDataSetChanged();
    }

    public void getSelectedPosition(int position) {
        checkPosition = position;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.size_adapter, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        holder.tv_sizeName.setText(sizeModels.get(position).getSize());
        holder.ll_sizeAdapter.setBackgroundResource(R.drawable.ellipse_register);
        if (sizeModels.get(position).getStock_Status() == 0) {
            holder.ll_mainview.setClickable(false);
            holder.ll_sizeAdapter.setBackgroundResource(R.color.blue_700);
        } else if (sizeModels.get(position).getStock_Status() == 1) {
            holder.ll_sizeAdapter.setBackgroundResource(R.drawable.ellipse_register);
            if (checkPosition == position) {
                holder.ll_sizeAdapter.setBackgroundResource(R.drawable.ellipse_green);
                holder.tv_sizeName.setTextColor(context.getResources().getColor(R.color.white));
                selectedSize = sizeModels.get(position).getSize();
                if (context instanceof AddToCartActivity) {
                    ((AddToCartActivity) context).getSelectSize(selectedSize);
                }
            } else {
                holder.ll_sizeAdapter.setBackgroundResource(R.drawable.ellipse_register);
                holder.tv_sizeName.setTextColor(context.getResources().getColor(R.color.tv_black));
            }

            if (sizeModels.size() == 1) {
                holder.ll_sizeAdapter.setBackgroundResource(R.drawable.ellipse_green);
                holder.tv_sizeName.setTextColor(context.getResources().getColor(R.color.white));
                selectedSize = sizeModels.get(position).getSize();
                if (context instanceof AddToCartActivity) {
                    ((AddToCartActivity) context).getSelectSize(selectedSize);
                }
            }


        }


    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_sizeName)
        TextView tv_sizeName;

        @BindView(R.id.ll_sizeAdapter)
        LinearLayout ll_sizeAdapter;

        @BindView(R.id.main_view)
        LinearLayout ll_mainview;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}

首先,两个适配器都将设置数据,并且在单击颜色项之后,必须更改大小适配器,但只有在我单击任何项​​目时它才会更改。 adapter.notifyDataSetChanged()在这里对我不起作用。

Both Adapter set

When i click color item but doesnt change size adapter

when i click size item only change size adapter

1 个答案:

答案 0 :(得分:0)

使用Interface与两个适配器桥接并相互通信。