第一种list
第一步在主工程下导入依赖
第二步在main下的项目下导入依赖
第三步写增删改查
public class NewsBeanUntils {
private static final NewsBeanUntils ourInstance = new NewsBeanUntils();
private NewsBeanDao newsDao;
public static NewsBeanUntils getInstance() {
return ourInstance;
}
private NewsBeanUntils() {
}
//初始化
public void init(Context context){
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "news");
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster mDaoMaster = new DaoMaster(db);
newsDao = mDaoMaster.newSession().getNewsBeanDao();
}
//增
public void insert(NewsBean newsBean){
newsDao.insert(newsBean);
}
//查询全部
public List<NewsBean> queryAll(){
return newsDao.loadAll();
}
//查询单个
public NewsBean query(String key){
return newsDao.load(Long.parseLong(key));
}
//删除全部
public void dellAll(){
newsDao.deleteAll();
}
//删除单个
public void dell(String key){
newsDao.deleteByKey(Long.parseLong(key));
}
//修改
public void udate(NewsBean newsBean){
newsDao.update(newsBean);
}
}
第四部写Bean,再写一个接口的Bean
@Entity
public class NewsBean {
@Id(autoincrement = true)
private Long id;
private String title;
private String image;
private String desc;
@Generated(hash = 1446835668)
public NewsBean(Long id, String title, String image, String desc) {
this.id = id;
this.title = title;
this.image = image;
this.desc = desc;
}
@Generated(hash = 1662878226)
public NewsBean() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImage() {
return this.image;
}
public void setImage(String image) {
this.image = image;
}
public String getDesc() {
return this.desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
第五部写App
public class App extends Application{
@Override
public void onCreate() {
super.onCreate();
NewsBeanUntils.getInstance().init(this);
Fresco.initialize(this);
}
}
主页面
public class MainActivity extends AppCompatActivity {
private String url="https://code.aliyun.com/598254259/FristProject/raw/master/bw_test.txt";
private RecyclerView recyclerView;
private List<NewsBean> items=new ArrayList<>();
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=(RecyclerView)findViewById(R.id.recyclerView);
//布局管理器
//(线性布局)
//LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//(瀑布流)
StaggeredGridLayoutManager staggeredGridLayoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
myAdapter = new MyAdapter(this);
recyclerView.setAdapter(myAdapter);
if (NetworkUtils.isConnected(this)){
//联网工具
doHttp();
}else {
//无网络情况下
items= NewsBeanUntils.getInstance().queryAll();
myAdapter.setList(items);
}
}
private void doHttp() {
new DoHttp1().get(url).result(new HttpLister() {
@Override
public void success(String data) {
items = new Gson().fromJson(data, NewsListBean.class).getItems();
NewsBeanUntils.getInstance().dellAll();
for(int i=0;i<items.size();i++){
NewsBean newsBean = items.get(i);
NewsBeanUntils.getInstance().insert(newsBean);
}
myAdapter.setList(items);
}
});
}
}
第二种list中还有一个list
1.在ChildAdapter里写一个接口
//点击事件
holder.mSimpleDraweeView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
lister.success();
}
});
//传递接口
private HttpLister lister;
public void result(HttpLister lister){
this.lister=lister;
}
public interface HttpLister{
void success();
}
2.在主Adapter中
//点击事件
myChildAdapter.result(new MyChildAdapter.HttpLister() {
@Override
public void success() {
String title = listAll.get(position).getTitle();
String subhead = listAll.get(position).getSubhead();
String images = listAll.get(position).getImages();
lister.success(title,subhead,images);
}
});
//传递接口
private HttpLister lister;
public void result(HttpLister lister){
this.lister=lister;
}
public interface HttpLister{
void success(String title,String subhead,String images);
}
3.在FramentOne中添加
myAdapter.result(new MyAdapter.HttpLister() {
@Override
public void success(String title, String subhead, String images) {
Bean bean=new Bean();
bean.setTitle(title);
bean.setSubhead(subhead);
bean.setImages(images);
Untils.getInstance().insert(bean);
}
});
4.在FramentTwo中查询出来
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
List<Bean> list = Untils.getInstance().query();
Toast.makeText(getActivity(),list+"",Toast.LENGTH_LONG).show();
HistoryAdapter historyAdapter = new HistoryAdapter(getActivity(),list);
StaggeredGridLayoutManager staggeredGridLayoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
recyc.setLayoutManager(staggeredGridLayoutManager);
recyc.setAdapter(historyAdapter);
}
5.写一个展示的适配器
public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.MyViewHolde>{
private Context context;
private List<Bean> list=new ArrayList<>();
public HistoryAdapter(Context context, List<Bean> list) {
this.context=context;
this.list=list;
}
@Override
public HistoryAdapter.MyViewHolde onCreateViewHolder(ViewGroup parent, int viewType) {
View view=View.inflate(context,R.layout.layout_dd,null);
MyViewHolde myViewHolde = new MyViewHolde(view);
return myViewHolde;
}
@Override
public void onBindViewHolder(HistoryAdapter.MyViewHolde holder, final int position) {
holder.txt_02.setText(list.get(position).getTitle());
holder.txt_03.setText(list.get(position).getSubhead());
holder.mSimpleDraweeView.setImageURI(list.get(position).getImages().split("\\|")[0]);
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolde extends RecyclerView.ViewHolder {
private LinearLayout linear_layout;
private TextView txt_02,txt_03;
private SimpleDraweeView mSimpleDraweeView;
public MyViewHolde(View itemView) {
super(itemView);
mSimpleDraweeView=(SimpleDraweeView)itemView.findViewById(R.id.mSimpleDraweeView);
txt_02=(TextView)itemView.findViewById(R.id.txt_02);
txt_03=(TextView)itemView.findViewById(R.id.txt_03);
}
}
}