如何使用SQLite数据库在单击“ Recyler”视图上发送包裹



我已经使用 sqlite 输入了数据我如何将适配器位置存储到捆绑中,并通过意图将其发送给其他活动并在其他活动中收集。

这是我的代码,

我的模型类带包裹

 public class FamousPeople implements Parcelable {
private static final String FAMOUS_NAME = "famous name";
private static final String FAMOUS_PHOTO="photo";
public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}
private long id;
private String name;
private String photo;
private String details;


public String getDetails() {
    return details;
}

public void setDetails(String details) {
    this.details = details;
}

public String getPhoto() {
    return photo;
}

public void setPhoto(String photo) {
    this.photo = photo;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Override
public int describeContents() {
    return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeLong(this.id);
    dest.writeString(this.name);
    dest.writeString(this.photo);
    dest.writeString(this.details);
}
public FamousPeople() {
}
protected FamousPeople(Parcel in) {
    this.id = in.readLong();
    this.name = in.readString();
    this.photo = in.readString();
    this.details = in.readString();
}
public static final Parcelable.Creator<FamousPeople> CREATOR = new Parcelable.Creator<FamousPeople>() {
    @Override
    public FamousPeople createFromParcel(Parcel source) {
        return new FamousPeople(source);
    }
    @Override
    public FamousPeople[] newArray(int size) {
        return new FamousPeople[size];
    }
};
}

我正在使用 sqlite数据库的片段

public class BiographyFragment extends Fragment {
StaggeredGridLayoutManager staggeredGridLayoutManager;
List<FamousPeople> famousPeoples;
BiographyDataSource biographyDataSource;
public BiographyFragment() {
    // Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    biographyDataSource=new BiographyDataSource(getActivity());
    biographyDataSource.open();
    Log.v("data","database created");
    View view=inflater.inflate(R.layout.fragment_biography, container, false);
    RecyclerView recyclerView= (RecyclerView)view.findViewById(R.id.my_recycler_view);
    recyclerView.setHasFixedSize(true);
    staggeredGridLayoutManager=new
            StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(staggeredGridLayoutManager);
    List<FamousPeople> famousPeoples=biographyDataSource.findRecords();
    List<FamousPeople> famousPeopleArrayList=biographyDataSource.findRecords();
    if(famousPeoples.size()==0){
        BioData bioData=new BioData();
        bioData.createdata();
        famousPeoples=biographyDataSource.findRecords();

    }
    RecyclerViewAdapter recyclerViewAdapter=
            new RecyclerViewAdapter(getActivity(),famousPeoples);
    recyclerView.setAdapter(recyclerViewAdapter);

    return view;
}

 }

**我正在收集数据的数据列表**

  public class BioData 
    {
    List<FamousPeople> famousPeoples;
    BiographyDataSource biographyDataSource;
   public List<FamousPeople> createdata(){
    FamousPeople famousPeople=new FamousPeople();
    famousPeople.setName("rahul");
    famousPeople.setPhoto("amitabh");
    famousPeople.setDetails("hello this is amitabh");
    biographyDataSource.create(famousPeople);
    Log.v("data", "inserted value 1");
    famousPeople=new FamousPeople();
    famousPeople.setName("cvraman");
    famousPeople.setPhoto("cvraman");
    famousPeople.setDetails("hello this is amitabh");
    biographyDataSource.create(famousPeople);
    Log.v("data", "inserted value 2");
    famousPeople=new FamousPeople();
    famousPeople.setName("indira");
    famousPeople.setPhoto("indira");
    famousPeople.setDetails("hello this is amitabh");
    biographyDataSource.create(famousPeople);
    Log.v("data", "inserted value 3");
    famousPeople=new FamousPeople();
    famousPeople.setName("modi");
    famousPeople.setPhoto("modi");
    famousPeople.setDetails("hello this is amitabh");
    biographyDataSource.create(famousPeople);
    Log.v("data", "inserted value 4");
    return famousPeoples;
}
}

我的 recyler 查看我很困惑的使用包裹,以收集单击的位置,然后将适当的dat发送到其他活动

 class BiographyViewHolder extends RecyclerView.ViewHolder
    implements View.OnClickListener {
 public TextView textView;
  public ImageView imageView;
  BiographyDataSource biographyDataSource;
List<FamousPeople> famousPeoples;

public BiographyViewHolder(View itemView)
{
    super(itemView);
    itemView.setOnClickListener(this);
    textView= (TextView) itemView.findViewById(R.id.country_name);
    imageView= (ImageView) itemView.findViewById(R.id.country_photo);

}
@Override
public void onClick(View view) {
    List<FamousPeople> famousPeopleArrayList=biographyDataSource.findRecords();
    if(famousPeoples.size()==0){
        BioData bioData=new BioData();
        bioData.createdata();
        famousPeoples=biographyDataSource.findRecords();

    }
    Bundle bundle=new Bundle();
   // bundle.putParcelable("test",famousPeopleArrayList.get(getAdapterPosition()));
    Intent intent=new Intent(view.getContext(),DetailActivity.class);
    intent.putExtras(bundle);
    view.getContext().startActivity(intent);
}
} 

我通过让内部类我读了java docs上的校长

来得到答案。
**java principal of inner class** 

这是一种仅在一个地方使用的逻辑分组的方式:如果一个类仅对另一个类有用,那么将其嵌入该类并将两者保持在一起是合乎逻辑的。嵌套这样的"辅助类"使他们的包裹更加精简。

它增加了封装:考虑两个顶级类A和B,B需要访问A的成员,否则将被声明为私人。通过将B类隐藏在A类中,可以将A的成员声明为私人,B可以访问它们。此外,B本身可以隐藏在外界。

我使传记片段中的内部类是回收器视图beacuse两者都在逻辑上连接。

最新更新