GreenDao数据库

依赖

compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.squareup.okhttp3:okhttp:3.8.1'
compile 'com.google.code.gson:gson:2.8.1'
compile 'com.squareup.picasso:picasso:2.3.2'
compile 'com.android.support:recyclerview-v7:24.0.0'
compile 'org.greenrobot:greendao:3.2.0'
compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.3'
compile 'com.android.support:design:24.0.0'
compile 'com.youth.banner:banner:1.4.10'  //最新版本
compile 'com.github.bumptech.glide:glide:3.8.0'

popup_xml

<Button
    android:id="@+id/btn_one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="增"
    />
<Button
    android:id="@+id/btn_two"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="删"
    />
<Button
    android:id="@+id/btn_three"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="改"
    />
<Button
    android:id="@+id/btn_four"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="查"
    />
main_xml

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    ></android.support.v7.widget.RecyclerView>
main

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private DaoSession daoSession;
    //    DaoSession session;
    String murl = "http://www.ipanda.com/kehuduan/PAGE14501767715521482/index.json";
    List<InteractiveBean> myLeis = new ArrayList<>();
    public  static MyAdapter myAdapter;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String s = (String) msg.obj;
            Log.e("-----------", s);
            Gson gson = new Gson();
            MyBean myBean = gson.fromJson(s, MyBean.class);
            InteractiveBeanDao interactiveBeanDao = daoSession.getInteractiveBeanDao();
            List<InteractiveBean> interactiveBeen = (List<InteractiveBean>) interactiveBeanDao.loadAll();
            List<InteractiveBean> interactive = myBean.getInteractive();
            for (int i = 0; i < interactive.size(); i++) {
                if(0==interactiveBeen.size()){
                    interactiveBeanDao.insert(interactive.get(i));
                }
            }
            List<InteractiveBean> interactiveBeen1 = daoSession.getInteractiveBeanDao().loadAll();
//            final List<InteractiveBean> interactive = myBean.getInteractive();
            Log.e("aaa",interactiveBeen1.size()+"");
            initAdapter(interactiveBeen1);
            initListener();
        }
    };

    private void initAdapter(List<InteractiveBean> interactiveBeen1) {
        myAdapter = new MyAdapter(MainActivity.this, interactiveBeen1);
        recyclerView.setAdapter(myAdapter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initListener() {
        myAdapter.getDatas(new MyAdapter.getTranstion() {
            @Override
            public void getData(int i) {
                if(0 == i){
                    List<InteractiveBean> interactiveBeen = daoSession.getInteractiveBeanDao().loadAll();
                    initAdapter(interactiveBeen);
                }
            }
        });
    }


    private void initData() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(murl).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Message message = new Message();
                message.obj = response.body().string();
                handler.sendMessage(message);
            }
        });
    }

    private void initView() {

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);

        daoSession = Dataku.getDataku().getDaoSession();
    }

}
datraku

public class Dataku extends Application {
    private static Dataku dataku;
    private DaoSession daoSession;

    public static Dataku getDataku(){
        return dataku;
    };

    @Override
    public void onCreate() {
        super.onCreate();
        dataku = this;
        createData();
    }

    private void createData() {
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(dataku,"aaa");
        DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
        daoSession = daoMaster.newSession();
    }
    public DaoSession getDaoSession(){
        return daoSession;
    };
}
myadapter

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.Myaaa> {
    Context context;
    List<InteractiveBean> myLeis;
    private getTranstion getTranstion;
    private PopupWindow popupWindow;
    private View views;
    private InteractiveBeanDao interactiveBeanDao;
    private DaoSession daoSession;

    public MyAdapter(Context context, List<InteractiveBean> myLeis) {
        this.context = context;
        this.myLeis = myLeis;
    }
    private Onclick mOnclick = null;
    public static interface Onclick{
        void OnclickListener(int position);
    }
    public void setOnClickss(Onclick listener){
        this.mOnclick = listener;
    }
    @Override
    public MyAdapter.Myaaa onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.xml_one,parent,false);
        Myaaa myaaa = new Myaaa(view);

        return myaaa;
    }

    @Override
    public void onBindViewHolder(MyAdapter.Myaaa holder, final int position) {
        holder.textView.setText(myLeis.get(position).getTitle());
        Picasso.with(context).load(myLeis.get(position).getImage()).into(holder.imageView);
        holder.itemView.setOnClickListener(new View.OnClickListener() {

            private Button button4;
            private Button button3;
            private Button button2;
            private Button button1;

            @Override
            public void onClick(View view) {
                views = LayoutInflater.from(context).inflate(R.layout.popup,null);
                popupWindow = new PopupWindow();
                popupWindow.setFocusable(true);
                popupWindow.setBackgroundDrawable(new BitmapDrawable());
                popupWindow.setContentView(views);//获取到的布局
                button1 = (Button) views.findViewById(R.id.btn_one);
                button2 = (Button) views.findViewById(R.id.btn_two);
                button3 = (Button) views.findViewById(R.id.btn_three);
                button4 = (Button) views.findViewById(R.id.btn_four);
                daoSession = Dataku.getDataku().getDaoSession();

                button1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(context, "增", Toast.LENGTH_SHORT).show();
                        DaoSession daoSession = Dataku.getDataku().getDaoSession();
                        interactiveBeanDao = daoSession.getInteractiveBeanDao();
                        InteractiveBean interactiveBean = new InteractiveBean();
                        interactiveBean.setTitle("添加成功");
                        interactiveBean.setImage("http://p1.img.cctvpic.com/photoAlbum/page/performance/img/2017/6/1/1496307155406_40.jpg");
                        interactiveBean.setOrder("asdasdsadsadsad");
                        interactiveBean.setUrl("http://webapp.cctv.com/h5/ipanda/U805QCEA72.html");
                        interactiveBeanDao.insert(interactiveBean);
                        getTranstion.getData(0);
                    }
                });
                button2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(context, "删", Toast.LENGTH_SHORT).show();
                        InteractiveBeanDao interactiveBeanDao = daoSession.getInteractiveBeanDao();
                        interactiveBeanDao.deleteByKey((Long) myLeis.get(position).getID());
                        getTranstion.getData(0);
                    }
                });
                button3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(context, "改", Toast.LENGTH_SHORT).show();
                        InteractiveBeanDao interactiveBeanDao = daoSession.getInteractiveBeanDao();
                        InteractiveBean interactiveBean = new InteractiveBean();
                        interactiveBean.setID(myLeis.get(position).getID());
                        interactiveBean.setTitle("xiugaic");
                        interactiveBean.setImage("http://p1.img.cctvpic.com/photoAlbum/page/performance/img/2017/6/1/1496307155406_40.jpg");
                        interactiveBean.setOrder("asdasdsadsadsad");
                        interactiveBean.setUrl("http://webapp.cctv.com/h5/ipanda/U805QCEA72.html");
                        interactiveBeanDao.update(interactiveBean);
                        getTranstion.getData(0);
                    }
                });
                button4.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
//                        Toast.makeText(MainActivity.this, "查", Toast.LENGTH_SHORT).show();
                        context.startActivity(new Intent(context, ResfasdsaActivity.class));
                    }
                });
                popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

                popupWindow.setBackgroundDrawable(new ColorDrawable());
                popupWindow.setOutsideTouchable(true);
                popupWindow.showAtLocation(views, Gravity.CENTER, 0, 0);
            }
        });
    }
    public interface getTranstion{
        void getData(int i);
    }
    public void getDatas(getTranstion getTranstion){
        this.getTranstion=getTranstion;
    }
    @Override
    public int getItemCount() {
        return myLeis.size();
    }

    public class Myaaa extends RecyclerView.ViewHolder {
        TextView textView;
        ImageView imageView;
        public Myaaa(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.text);
            imageView = (ImageView) itemView.findViewById(R.id.image);
        }
    }
}
 
mybean
public class MyBean {
    private List<InteractiveBean> interactive;

    public List<InteractiveBean> getInteractive() {
        return interactive;
    }

    public void setInteractive(List<InteractiveBean> interactive) {
        this.interactive = interactive;
    }
}

InteractiveBean
@Entity
public class InteractiveBean {
    @Id(autoincrement = true)
    private Long ID;
    private String image;
    private String title;
    private String url;
    private String order;
    @Generated(hash = 1765186461)
    public InteractiveBean(Long ID, String image, String title, String url,
            String order) {
        this.ID = ID;
        this.image = image;
        this.title = title;
        this.url = url;
        this.order = order;
    }
    @Generated(hash = 1588809506)
    public InteractiveBean() {
    }
    public Long getID() {
        return this.ID;
    }
    public void setID(Long ID) {
        this.ID = ID;
    }
    public String getImage() {
        return this.image;
    }
    public void setImage(String image) {
        this.image = image;
    }
    public String getTitle() {
        return this.title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getUrl() {
        return this.url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getOrder() {
        return this.order;
    }
    public void setOrder(String order) {
        this.order = order;
    }
}

 
 
 
 
 
 
 分页加载 

public class ResfasdsaActivity extends AppCompatActivity {

    private RecyclerView recyclerview;
    private SmartRefreshLayout refreshLayout;
    private ArrayList<String> arrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resfasdsa);
        initView();
        initData();
        initAdapter();
        initListener();
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000);
                initData();
                initAdapter();
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(2000);
                ArrayList<String> arrayLists=new ArrayList<String>();
                for (int i = 0; i < 10; i++) {
                    arrayLists.add("tianjiade"+i);
                }
                arrayList.addAll(arrayLists);
                initAdapter();
            }
        });
    }

    private void initAdapter() {
        RecyclerViewResfAdapter recyclerViewResfAdapter = new RecyclerViewResfAdapter(arrayList,ResfasdsaActivity.this);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(ResfasdsaActivity.this);
        recyclerview.setLayoutManager(linearLayoutManager);
        recyclerview.setAdapter(recyclerViewResfAdapter);
    }

    private void initData() {
        arrayList = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            arrayList.add("di"+i+"asdasdsad");
        }
    }

    private void initView() {
        recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        refreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);
    }
}
分页加载适配器

public class RecyclerViewResfAdapter extends RecyclerView.Adapter<RecyclerViewResfAdapter.Holder> {
    ArrayList<String> arrayList;
    Context context;

    public RecyclerViewResfAdapter(ArrayList<String> arrayList, Context context) {
        this.arrayList = arrayList;
        this.context = context;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.layout_recyclerviewitem_resf, parent, false);
        Holder holder = new Holder(inflate);
        return holder;
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        holder.tv_recyclerview_resf.setText(arrayList.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                context.startActivity(new Intent(context, BannerActivity.class));
            }
        });
    }

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

    public class Holder extends RecyclerView.ViewHolder {
        public TextView tv_recyclerview_resf;

        public Holder(View itemView) {
            super(itemView);
            tv_recyclerview_resf = (TextView) itemView.findViewById(R.id.tv_recyclerview_resf);

        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值