Parcelable改变原始数据

时间:2018-01-28 11:55:25

标签: java android

我正在实施RecycleView,这将显示这些技能通过Parcelable使用RecycleViewParcelable上显示的技能中删除项目时返回的另一个片段所传递的技能列表该项目的前一个片段也从原始的arraylist中删除了

这是public class Skills implements Parcelable { private ArrayList<String> skills; public ArrayList<String> getVacancySkills() { return skills; } public void setSkills(ArrayList<String> skills) { this.skills = skills; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeStringList(this.skills); } public Skills() { } protected Skills(Parcel in) { this.skills = in.createStringArrayList(); } public static final Parcelable.Creator<Skills> CREATOR = new Parcelable.Creator<Skills>() { @Override public Skills createFromParcel(Parcel source) { return new Skills(source); } @Override public Skills[] newArray(int size) { return new Skills[size]; } }; } 对象

      @OnClick(R.id.card_add_skill)
public void showAddSkills() {
    Bundle data = new Bundle();

    Skills skills = new Skills();
    skills.setSkills(skillsLit);
    data.putParcelable(ExtraKeys.VACANCY_SKILLS, skills);

    VacancyEditSkillsFragment vacancyEditSkillsFragment = new VacancyEditSkillsFragment();

    FragmentUtils.addFragment(
            getActivity().getSupportFragmentManager(),
            vacancyEditSkillsFragment
            ,
            true,
            App.getContext().getString(R.string.skills), data

    );

}

这是传递数据

  public class VacancyEditSkillsFragment extends Fragment {


@BindView(R.id.rc_edit_skill_recycle)
RecyclerView editSkillsRecycle;

private Skills mSkills;

private EditVacancySkillsAdapter mEditVacancySkillsAdapter;

public VacancyEditSkillsFragment() {
    // Required empty public constructor
}


@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (getArguments() != null) {
        mSkills = getArguments().getParcelable(ExtraKeys.VACANCY_SKILLS);
    }

    mEditVacancySkillsAdapter = new EditVacancySkillsAdapter(mSkills.getVacancySkills());
    editSkillsRecycle.setLayoutManager(new LinearLayoutManager(getActivity()));
    editSkillsRecycle.setAdapter(mEditVacancySkillsAdapter);


}

public static VacancyEditSkillsFragment newInstance() {
    VacancyEditSkillsFragment fragment = new VacancyEditSkillsFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_vacancy_edit_skills, container, false);
    ButterKnife.bind(this, view);
    return view;
}

  }

这里是重现Parcable字符串arraylist的片段

     public class EditVacancySkillsAdapter extends RecyclerView.Adapter<EditVacancySkillsAdapter.EditSkillViewHolder> {

private List<String> skills;


public class EditSkillViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    @BindView(R.id.tv_skill_name)
    TextView skillTitle;
    @BindView(R.id.iv_reomve_skill)
    ImageView deleteSkill;


    public EditSkillViewHolder(View view) {
        super(view);
        ButterKnife.bind(this, view);
        deleteSkill.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        if(view.getId()== R.id.iv_reomve_skill)
        {
            skills.remove(getAdapterPosition());
            notifyDataSetChanged();
        }
    }
}

public EditVacancySkillsAdapter(ArrayList<String> sections) {
    this.skills = sections;
    if (skills == null) {
        skills = new ArrayList<>();
    }
}

@Override
public EditSkillViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_edit_vacancy_skill, parent, false);

    return new EditSkillViewHolder(itemView);
}

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


    holder.skillTitle.setText(skills.get(position));
}

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


   }

这是适配器代码

for v in enumerate (["20170106", "20170113", "20170127", "20170203", "20170210", "20170224", "20170303", "20170310", "20170324", "20170331"])
    Year17 = "/Users/Name/Desktop/Datas/" + v
    csvFiles = glob.glob (os.path.join (Year17, "*.csv"))
    df = (pd.read_csv(i) for i in csvFiles)
    df = pd.concat (df, ignore_index=True)

1 个答案:

答案 0 :(得分:0)

这是预期的。

简单来说,parcellable是什么: - 它允许您跨不同组件发送对象。您在两个组件中引用相同的对象。 Parcellable已经在对象上实现了。因此,如果组件中的任何位置发生任何更改,则会反映回使用相同对象的组件的其余部分。

我希望你能理解。

您应该做的是发送对象的副本而不是原始对象。

如何克隆parcellable对象是这样的: -

Foo foo1 = new Foo("a", "b", "c");
Parcel p = Parcel.obtain();
p.writeValue(foo1);
p.setDataPosition(0);
Foo foo2 = (Foo)p.readValue(Foo.class.getClassLoader());
p.recycle();

我希望这会有所帮助!