您的位置:首页 > 编程语言 > Java开发

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

2016-08-12 16:13 393 查看
今天在做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 Serializ
4000
able,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);

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: