GreenDao增减改查

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;

 

    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值