greendao实现增删改查,recyclerview展示数据

//greendao依赖
    compile 'org.greenrobot:greendao:3.2.0'
    compile 'org.greenrobot:greendao-generator:3.2.2'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
apply plugin: 'org.greenrobot.greendao'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
mavenCentral()
https://blog.csdn.net/wzgbgz/article/details/79140056
//导依赖
compile 'org.greenrobot:greendao:3.2.0'
    compile 'com.jakewharton:butterknife:8.5.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
    compile 'com.android.support:design:26.0.0-alpha1'

    compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
//User
package com.bwie.greendaodemo.gen;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;

/**
 * Created by 暗夜 on 2018/5/14.
 */
@Entity
public class User {
    @Id(autoincrement = true)
    private Long id;
    private String name;
    private String age;
    @Generated(hash = 1666193281)
    public User(Long id, String name, String age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    @Generated(hash = 586692638)
    public User() {
    }
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return this.age;
    }
    public void setAge(String age) {
        this.age = age;
    }

}
//MainActivity
package com.bwie.greendaodemo;

import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
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.bwie.greendaodemo.gen.DaoMaster;
import com.bwie.greendaodemo.gen.DaoSession;
import com.bwie.greendaodemo.gen.User;
import com.bwie.greendaodemo.gen.UserDao;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    private static final String TAG ="MainActivity----" ;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_age)
    EditText etAge;
    @BindView(R.id.bt_insert)
    Button btInsert;
    @BindView(R.id.bt_select)
    Button btSelect;
    @BindView(R.id.bt_delete)
    Button btDelete;
    @BindView(R.id.bt_update)
    Button btUpdate;
    @BindView(R.id.rlv)
    RecyclerView rlv;
    @BindView(R.id.et_id)
    EditText etId;
    private UserDao userDao;
    private String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this, "pwk.db", null);
        DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        userDao = daoSession.getUserDao();
    }

    @OnClick({R.id.bt_insert, R.id.bt_select, R.id.bt_delete, R.id.bt_update})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_insert:
                insert();
                break;
            case R.id.bt_select:
                select();
                break;
            case R.id.bt_delete:
                delete();
                break;
            case R.id.bt_update:
                update();
                break;
        }
    }




    private void insert() {
         name = etName.getText().toString().trim();
        String age = etAge.getText().toString().trim();

            User user = new User(null, name, age);
            userDao.insert(user);
            Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
     select();

    }

    private void select() {
        List users = userDao.loadAll();

        for(int i=0;i users = userDao.loadAll();

        for(int i=0;i
//MyAdapter
package com.bwie.greendaodemo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bwie.greendaodemo.gen.User;

import org.w3c.dom.Text;

import java.util.List;

/**
 * Created by 暗夜 on 2018/5/14.
 */

public class MyAdapter extends RecyclerView.Adapter {
    private List list;
    private Context context;

    public MyAdapter(List list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public MyAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context,R.layout.layout_item,null);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyAdapter.MyHolder holder, int position) {
        holder.tv_content.setText("ID:"+list.get(position).getId()+"  名字:"+list.get(position).getName()+"  年龄:"+list.get(position).getAge());
    }

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

    public class MyHolder extends RecyclerView.ViewHolder {
        private final TextView tv_content;
        public MyHolder(View itemView) {
            super(itemView);
            tv_content = itemView.findViewById(R.id.tv_content);
        }
    }
}




你可能感兴趣的:(greendao实现增删改查,recyclerview展示数据)