如何使用Sqlite数据库单击Recyler视图发送Parcel

时间:2016-06-23 07:03:50

标签: android android-intent android-database

我已经使用 sqlite 输入了我的数据,但是当我想使用意图中的 recyler 视图中使用parcel时我很困惑如何将适配器位置设置为存储到捆绑中并通过意图将其发送到其他活动并将其收集到其他活动中。

这是我的代码,

我的带有parcel的模型类

 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 视图我很困惑使用包裹来收集点击位置并将相应的数据发送到其他活动

 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);
}

} 

1 个答案:

答案 0 :(得分:0)

我通过让内部课程阅读java docs上的校长来得到答案

public class ReadPropertiesWS {
    private static final String filename = "WEBINF/conf/sample.properties";
    @Resource
    private WebServiceContext context;
    private static Properties properties = new Properties();

    public ReadPropertiesWS() throws IOException {
        ServletContext servletContext = (ServletContext) context
                .getMessageContext().get(MessageContext.SERVLET_CONTEXT);
        InputStream in = servletContext
                .getResourceAsStream("/WEB-INF/conf/sample.properties");
        try {
            properties.load(in);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

这是一种逻辑分组仅在一个地方使用的类的方法:如果一个类只对另一个类有用,那么将它嵌入该类并将两者保持在一起是合乎逻辑的。嵌套这样的“辅助类”使得它们的包更加简化。

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

我在传记片段中创建了内部类,这是recyler视图,因为它们都是逻辑连接的。