安卓中碰到的JAVA引用传递问题
2016-08-12 16:13
393 查看
今天在做i项目的时候碰到了引用传递,每次在adapte中对所需要的数据修改后所有引用该数据的内容都被修改
因此我使用了克隆的方法。先让实体类实现Cloneable的接口
再在使用中进行克隆
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); } } }
相关文章推荐
- 源码分析-java-Vector、Stack-与ArrayList的相关性
- Java中OutOfMemoryError
- 《Pro Java 7 NIO.2 》 The Asychronous Channel API 部分翻译
- spring security @PreAuthorize在controller中配置失效解决方法
- Spring常用注解
- java几种经典排序
- zz java反射性能低?如何优化
- Eclipse 直接开发HANA U…
- Java AIO 认识
- HANA Studio (Eclipse…
- Spring mvc / 和 /*的区别
- java死锁的一个例子
- java Gson java代码转字符串、对象使用
- eclipse中ant打包,报异常解决办法
- PAT乙级(Basic Level)1016(Java)
- Struts2与Spring、Hibernate三者整合的过程示例
- myeclipse8.5配置SSH环境
- 关于eclipse运行和调试结果不同的问题
- Java实现邮件发送【带附件】
- ssh三大框架整合后struts2返回json数据出现懒加载异常