GreenDao增减改查

原创 2018年04月15日 19:54:37

MainAction层

package com.example.dell.a0412rikao_greendao;

 

import android.content.SharedPreferences;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.support.v7.app.AppCompatActivity;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ListView;

import android.widget.Toast;

 

import com.google.gson.Gson;

 

import org.greenrobot.greendao.query.Query;

 

import java.io.IOException;

import java.util.List;

 

import butterknife.BindView;

import butterknife.ButterKnife;

import butterknife.OnClick;

import okhttp3.Call;

import okhttp3.Callback;

import okhttp3.Response;

 

public class MainActivity extends AppCompatActivity {

 

    @BindView(R.id.edit_id)

    EditText edit_Id;

    @BindView(R.id.edit_name)

    EditText edit_Name;

    @BindView(R.id.btn_insert)

    Button btn_Insert;

    @BindView(R.id.btn_delete)

    Button btn_Delete;

    @BindView(R.id.btn_query)

    Button btn_Query;

    @BindView(R.id.my_lv)

    ListView my_Lv;

    @BindView(R.id.edit_title)

    EditText edit_Title;

    @BindView(R.id.btn_updata)

    Button btn_Updata;

    @BindView(R.id.btn_removeAll)

    Button btn_RemoveAll;

    @BindView(R.id.btn_queryByID)

    Button btn_QueryByID;

 

    private UserDao userDao;

    private List<UserBean.NewslistBean> newslist;

 

    Handler handler = new Handler() {

 

 

        @Override

        public void handleMessage(Message msg) {

            super.handleMessage(msg);

            newslist = (List<UserBean.NewslistBean>) msg.obj;

 

            if (!jinru_yes) {

 

                for (int i = 0; i < newslist.size(); i++) {

                    User user = new User();

                    user.setDescription(newslist.get(i).getDescription());

                    user.setTitle(newslist.get(i).getTitle());

 

                    //存入同意的状态值

                    editor.putBoolean("jinru_yes", true);

                    editor.commit();

 

                    userDao.insert(user);

                }

            }

 

        }

    };

    private MyAdapter myAdapter;

    private SharedPreferences sharedPreferences;

    private SharedPreferences.Editor editor;

    private boolean jinru_yes;

    private List<User> list;

 

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        //得到SharedPreferences

        sharedPreferences = getSharedPreferences("Person", MODE_PRIVATE);

        //用来操作sharedPreferences

        editor = sharedPreferences.edit();

 

        //自动的状态值

        jinru_yes = sharedPreferences.getBoolean("jinru_yes", false);

 

 

        ButterKnife.bind(this);

 

        //通过DaoMaster拿到DaoSession,再通过DaoSession拿到UserDao

        DaoMaster master = new DaoMaster(new DaoMaster.DevOpenHelper(this, "users").getWritableDatabase());

        DaoSession session = master.newSession();

        userDao = session.getUserDao();

 

 

        //获取数据

        OkhtttpUtils.doGet("https://api.tianapi.com/wxnew/?key=fe7aac932e1e94f10a4f8d0f2d5fe924&num=10", new Callback() {

            @Override

            public void onFailure(Call call, IOException e) {

 

            }

 

            @Override

            public void onResponse(Call call, Response response) throws IOException {

                String json = response.body().string();

                Gson gson = new Gson();

                UserBean userBean = gson.fromJson(json, UserBean.class);

                List<UserBean.NewslistBean> newslist = userBean.getNewslist();

 

                //向handler发送消息

                Message msg = new Message();

                msg.obj = newslist;

                handler.sendMessage(msg);

            }

        });

 

 

    }

 

    @OnClick({R.id.btn_insert, R.id.btn_delete, R.id.btn_query,R.id.btn_updata,R.id.btn_queryByID,R.id.btn_removeAll})

    public void onViewClicked(View view) {

        switch (view.getId()) {

            case R.id.btn_insert:

                //插入的方法

                insert();

                //查询的方法

                query();

 

                break;

            case R.id.btn_delete:

                //删除的方法

                delete();

                //查询的方法

                query();

 

                break;

            case R.id.btn_updata:

                //修改的方法

                updata();

                //查询的方法

                query();

 

                break;

            case R.id.btn_removeAll:

                //清空所有的方法

                removeAll();

                //查询的方法

                query();

 

                break;

            case R.id.btn_queryByID:

                //查询一条的方法

                byid();

 

                break;

            case R.id.btn_query:

                //查询的方法

                query();

 

                break;

        }

    }

 

    //清空所有数据

    private void removeAll() {

        userDao.deleteAll();

 

    }

 

 

    //根据ID查询信息

    public void byid() {

 

 

        String idd = edit_Id.getText().toString();

        Long id = Long.valueOf(idd);

        User user = userDao.load(id);

        if(user!=null){

            edit_Name.setText(user.getDescription());

            edit_Title.setText(user.getTitle());

        }else{

            Toast.makeText(this,"数据不存在",Toast.LENGTH_SHORT).show();

        }

 

 

    }

 

    //修改的方法

    private void updata() {

 

        String id_up = edit_Id.getText().toString();

        Long longid_up = Long.valueOf(id_up);

        String n = edit_Name.getText().toString();

        String t = edit_Title.getText().toString();

        User user_up = new User();

        user_up.setDescription(n);

        user_up.setTitle(t);

        user_up.setId(longid_up);

        userDao.update(user_up);

 

 

 

    }

 

    //删除的方法

    public void delete() {

        String idd = edit_Id.getText().toString();

        Long id = Long.valueOf(idd);

        userDao.deleteByKey(id);

        myAdapter.notifyDataSetChanged();

    }

 

    //插入的方法

    public void insert() {

        String name = edit_Name.getText().toString();

        String title = edit_Title.getText().toString();

        User user = new User();

        user.setDescription(name);

        user.setTitle(title);

        userDao.insert(user);

    }

 

 

    //查询的方法

    public void query() {

        list = userDao.loadAll();

 

        //设置适配器

        myAdapter = new MyAdapter(MainActivity.this, list);

        my_Lv.setAdapter(myAdapter);

    }

 

 

 

}


Myabapter

package com.example.dell.a0412rikao_greendao;

 

import android.content.Context;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.TextView;

 

import java.util.List;

 

/**

 * Created by dell on 2018/4/13.

 */

 

public class MyAdapter extends BaseAdapter{

 

    Context context;

    List<User> list;

 

    public MyAdapter(Context context, List<User> list) {

        this.context=context;

        this.list=list;

    }

 

    @Override

    public int getCount() {

        return list.size();

    }

 

    @Override

    public Object getItem(int i) {

        return list.get(i);

    }

 

    @Override

    public long getItemId(int i) {

        return i;

    }

 

    @Override

    public View getView(int i, View view, ViewGroup viewGroup) {

        MyViewHolder holder;

        if(view==null){

            holder = new MyViewHolder();

            view = View.inflate(context, R.layout.item, null);

            holder.id=view.findViewById(R.id.text_id);

            holder.name=view.findViewById(R.id.text_name);

            holder.title=view.findViewById(R.id.text_title);

            view.setTag(holder);

        }else{

            holder= (MyViewHolder) view.getTag();

        }

 

        holder.id.setText(list.get(i).getId()+"");

        holder.name.setText(list.get(i).getTitle());

        holder.title.setText(list.get(i).getDescription());

 

        return view;

    }

 

 

    class MyViewHolder{

        TextView id,title,name;

 

    }

}



程序员相声:增删改查

精彩回顾  点蓝字即可  ♡ 女程序媛与男程序猿的一天♡ 老说程序员如何看产品经理,今天说说产品经理讨厌哪些程序员♡ 史上最深(sàng)入(xīn)浅(bìng)出(kuáng)的IT术语解读♡ 如...
  • h8y0bDJVUkwE1LboZlE
  • h8y0bDJVUkwE1LboZlE
  • 2017-12-25 00:00:00
  • 184

mysq对数据的l查询,增加,修改和删除sql语句

sql查询语句: select * from student where sname like '%李%' and sgroup>55 and sgroup sql插入语句: inser...
  • cherry_cindy
  • cherry_cindy
  • 2017-04-18 14:57:02
  • 272

数据库增加表与更新字段语法 GREENDAO

if (newVersion > oldVersion) { String sql = "CREATE TABLE NODE_RICH_AND_INFO(CONTENT TEXT,ANSWE...
  • ghsy121
  • ghsy121
  • 2016-08-05 19:55:49
  • 3051

GreenDao的基本使用

  • 2018年03月30日 17:48
  • 5.18MB
  • 下载

学习编程的根本四要素--增删改查

    我不敢学说世界的根本万物都在 无极里面,范围太广,写了怕被别人骂,只敢谈谈编程,又不敢谈高深的技术,只因世界上高手太多。怕被人暗杀,只敢总结一下学习编程的玄机---增删改查。   据我观察,一...
  • shenyisyn
  • shenyisyn
  • 2006-02-16 22:58:00
  • 3756

QT(4)信号、SLOT和QMap - Addressbook例子2

在之前的MeeGo开发者(五):QT(3)对象和继承小例子基础上,我们增加三个button,参考http://doc.qt.nokia.com/latest/tutorials-addressbook...
  • flowingflying
  • flowingflying
  • 2011-01-10 11:40:00
  • 3798

greenDAO demo

  • 2016年12月27日 10:13
  • 17.62MB
  • 下载

greenDAO 简单使用配置

greenDAO 简单使用配置 ** * 1.引入greenDAO 的gradle文件和module的gradle文件一共修改四个地方 * 2。在module的gradle文件中高配置数...
  • github_36225693
  • github_36225693
  • 2017-01-18 19:41:44
  • 570

Android GreenDao实现CRUD和升级详解

我们不论在学习Android还是在开发应用的过程中或多或少的会接触到一些SQLite。增(insert)、删(delete)、改(update)、查(query),当然如果我们在使用的过程中想要添加字...
  • u014452224
  • u014452224
  • 2017-03-14 15:51:37
  • 2249

greendao使用说明

  • 2015年10月09日 15:56
  • 39KB
  • 下载
收藏助手
不良信息举报
您举报文章:GreenDao增减改查
举报原因:
原因补充:

(最多只允许输入30个字)