安卓中碰到的JAVA引用传递问题

今天在做i项目的时候碰到了引用传递,每次在adapte中对所需要的数据修改后所有引用该数据的内容都被修改
package sunrun.com.mall.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import sunrun.com.mall.R;
import sunrun.com.mall.bean.GoodsSpecValueListDTO;
import sunrun.com.mall.bean.SpecInfo;
import sunrun.com.mall.event.PopFirstEvent;
import sunrun.com.mall.event.PopRefreshEvent;
import sunrun.com.mall.httputils.JsonUtils;
import sunrun.com.mall.utils.LogI;

/**
 * Created by 25002 on 2016-08-03.
 */

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

    private LayoutInflater inflater;
    private List<GoodsSpecValueListDTO> mGoodsSpecValueListDTO = new ArrayList<>();
    private int old_select = 99999;
    private List<SpecInfo> specInfoList = new ArrayList<SpecInfo>();
    private String specinfos;
    private int current_select;
    Activity mActivity;


    public PopupSecondAdapter(Context context, List<GoodsSpecValueListDTO> list, int i, String specInfos) {
        this.mGoodsSpecValueListDTO = list;
        this.inflater = inflater.from(context);
        this.mActivity = (Activity) context;
        this.current_select = i;
        this.specinfos = specInfos;
        specInfoList = JsonUtils.parseJsonToSpecInfolist(specinfos);
    }


    @Override
    public PopupSecondAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        PopupSecondAdapter.MyViewHolder holder = new PopupSecondAdapter.MyViewHolder(inflater.inflate(R.layout.item_popup_type_second, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(PopupSecondAdapter.MyViewHolder holder, final int position) {
        holder.value.setText(mGoodsSpecValueListDTO.get(position).getSpValue());
        if(old_select!=99999) {
            LogI.w("old==" + old_select + "==" + mGoodsSpecValueListDTO.get(old_select).isChecked());
        }
        LogI.w("now=="+position+"=="+mGoodsSpecValueListDTO.get(position).isChecked());
        if (mGoodsSpecValueListDTO.get(position).isChecked()) {
            holder.value.setBackgroundResource(R.mipmap.param_child_selected);
            old_select = position;
        } else {
            holder.value.setBackgroundResource(R.mipmap.paramchild_bg);
        }
        holder.value.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(old_select!=99999){
                    mGoodsSpecValueListDTO.get(old_select).setChecked(false);
                }

                if (mGoodsSpecValueListDTO.get(position).isChecked()) {
                    mGoodsSpecValueListDTO.get(position).setChecked(false);
                   // notifyItemChanged(position);
                } else {
                    mGoodsSpecValueListDTO.get(position).setChecked(true);
                    //notifyItemChanged(position);
                }
                old_select = position;
                SpecInfo specInfo = new SpecInfo(mGoodsSpecValueListDTO.get(position).getSpValue(), mGoodsSpecValueListDTO.get(position).getSpValueId());
                specInfoList.set(current_select, specInfo);
                notifyDataSetChanged();
                EventBus.getDefault().post(new PopFirstEvent(specInfoList));//将选择的内容传递给popwindow进行图片与价格的刷新
                //EventBus.getDefault().post(new PopRefreshEvent());//
            }
        });


    }

    @Override
    public int getItemCount() {

        if(mGoodsSpecValueListDTO==null){
            return 0;
        }else{
            return mGoodsSpecValueListDTO.size();
        }
    }

    public void setmGoodsSpecValueListDTO(List<GoodsSpecValueListDTO> mGoodsSpecValueListDTO) {
        this.mGoodsSpecValueListDTO = mGoodsSpecValueListDTO;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView value;

        public MyViewHolder(View view) {
            super(view);
            value = (TextView) view.findViewById(R.id.tv_value);

        }
    }
}


因此我使用了克隆的方法。先让实体类实现Cloneable的接口


package sunrun.com.mall.bean;

import java.io.Serializable;

/**
 * Created by 25002 on 2016-08-02.
 */

public class GoodsSpecValueListDTO implements Serializable,Cloneable{
    private int spValueId;//二级目录id
    private String spValue; //二级目录名
    private boolean checked=false; //是否被选择

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return "GoodsSpecValueListDTO{" +
                "spValueId=" + spValueId +
                ", spValue='" + spValue + '\'' +
                ", checked=" + checked +
                '}';
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public int getSpValueId() {
        return spValueId;
    }

    public void setSpValueId(int spValueId) {
        this.spValueId = spValueId;
    }

    public String getSpValue() {
        return spValue;
    }

    public void setSpValue(String spValue) {
        this.spValue = spValue;
    }

}


再在使用中进行克隆
package sunrun.com.mall.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import sunrun.com.mall.R;
import sunrun.com.mall.bean.GoodsSpecValueListDTO;
import sunrun.com.mall.bean.SpecInfo;
import sunrun.com.mall.event.PopFirstEvent;
import sunrun.com.mall.event.PopRefreshEvent;
import sunrun.com.mall.httputils.JsonUtils;
import sunrun.com.mall.utils.LogI;

/**
 * Created by 25002 on 2016-08-03.
 */

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

    private LayoutInflater inflater;
    private List<GoodsSpecValueListDTO> mGoodsSpecValueListDTO = new ArrayList<>();
    private int old_select = 99999;
    private List<SpecInfo> specInfoList = new ArrayList<SpecInfo>();
    private String specinfos;
    private int current_select;
    Activity mActivity;


    public PopupSecondAdapter(Context context, List<GoodsSpecValueListDTO> list, int i, String specInfos) {
        for (int i1=0;i1<list.size();i1++) {
            try {
                this.mGoodsSpecValueListDTO.add((GoodsSpecValueListDTO) list.get(i1).clone());
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
        }
        this.inflater = inflater.from(context);
        this.mActivity = (Activity) context;
        this.current_select = i;
        this.specinfos = specInfos;
        specInfoList = JsonUtils.parseJsonToSpecInfolist(specinfos);
    }


    @Override
    public PopupSecondAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        PopupSecondAdapter.MyViewHolder holder = new PopupSecondAdapter.MyViewHolder(inflater.inflate(R.layout.item_popup_type_second, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(PopupSecondAdapter.MyViewHolder holder, final int position) {
        holder.value.setText(mGoodsSpecValueListDTO.get(position).getSpValue());
        if(old_select!=99999) {
            LogI.w("old==" + old_select + "==" + mGoodsSpecValueListDTO.get(old_select).isChecked());
        }
        LogI.w("now=="+position+"=="+mGoodsSpecValueListDTO.get(position).isChecked());
        if (mGoodsSpecValueListDTO.get(position).isChecked()) {
            holder.value.setBackgroundResource(R.mipmap.param_child_selected);
            old_select = position;
        } else {
            holder.value.setBackgroundResource(R.mipmap.paramchild_bg);
        }
        holder.value.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(old_select!=99999){
                    mGoodsSpecValueListDTO.get(old_select).setChecked(false);
                }

                if (mGoodsSpecValueListDTO.get(position).isChecked()) {
                    mGoodsSpecValueListDTO.get(position).setChecked(false);
                   // notifyItemChanged(position);
                } else {
                    mGoodsSpecValueListDTO.get(position).setChecked(true);
                    //notifyItemChanged(position);
                }
                old_select = position;
                SpecInfo specInfo = new SpecInfo(mGoodsSpecValueListDTO.get(position).getSpValue(), mGoodsSpecValueListDTO.get(position).getSpValueId());
                specInfoList.set(current_select, specInfo);
                notifyDataSetChanged();
                EventBus.getDefault().post(new PopFirstEvent(specInfoList));//将选择的内容传递给popwindow进行图片与价格的刷新
                //EventBus.getDefault().post(new PopRefreshEvent());//
            }
        });


    }

    @Override
    public int getItemCount() {

        if(mGoodsSpecValueListDTO==null){
            return 0;
        }else{
            return mGoodsSpecValueListDTO.size();
        }
    }

    public void setmGoodsSpecValueListDTO(List<GoodsSpecValueListDTO> mGoodsSpecValueListDTO) {
        this.mGoodsSpecValueListDTO = mGoodsSpecValueListDTO;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView value;

        public MyViewHolder(View view) {
            super(view);
            value = (TextView) view.findViewById(R.id.tv_value);

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值