数据展示+传值+存数据库

用到的依赖

大build

classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0'

implementation 'com.facebook.fresco:fresco:1.2.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.android.support:recyclerview-v7:28.0.0-beta01'
implementation 'de.greenrobot:eventbus:3.0.0-beta1'
apply plugin: 'org.greenrobot.greendao'
  greendao {

        schemaVersion 1
        daoPackage 'com.bwei.greendao.gen'
        targetGenDir 'src/main/java'
    }
    implementation'org.greenrobot:greendao:3.0.1'
    implementation'org.greenrobot:greendao-generator:3.0.0'

 

第一个页面

1.xml



2.activity

public class MainActivity extends AppCompatActivity {
    TextView textView;
    SimpleDraweeView simpleDraweeView;
    String path="https://img2.woyaogexing.com/2018/07/30/b2773d0e982efa66!400x400_big.jpg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //注册
        EventBus.getDefault().register(this);

        textView=findViewById(R.id.textview);
        simpleDraweeView=findViewById(R.id.sdv);
        Uri uri=Uri.parse(path);
        simpleDraweeView.setImageURI(uri);

        simpleDraweeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it=new Intent(MainActivity.this,ShowActivity.class);
                startActivity(it);
            }
        });

    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void getMsg(Chuan bean) {
        textView.setText(bean.title);
        simpleDraweeView.setImageURI(bean.url);
    }
}

数据展示

1.model层

1》

public class User {
    /**
     * _id : 5b63cd4e9d21225e0d3f58c9
     * createdAt : 2018-08-03T11:34:38.672Z
     * desc : 2018-08-03
     * publishedAt : 2018-08-03T00:00:00.0Z
     * source : api
     * type : 福利
     * url : https://ww1.sinaimg.cn/large/0065oQSqgy1ftwcw4f4a5j30sg10j1g9.jpg
     * used : true
     * who : lijinshan
     */

    public String _id;
    public String createdAt;
    public String desc;
    public String publishedAt;
    public String source;
    public String type;
    public String url;
    public boolean used;
    public String who;

}

2》

public class UserData {
    public List results;
}

3.ITask

public interface ITask {
    public void getUserList(String url, OkCallback callback);
}

4》Task

public class Task implements ITask {
    @Override
    public void getUserList(String url, OkCallback callback) {
        OK.getOk().doGet(url,callback);
    }
}

2.Presenter

1>基类

public interface BasePre {
    void onDestroy();
}

2》Ipre

public interface IPresenter extends BasePre{
    public void getData();
}

3>MyPresenter

public class MyPresenter implements IPresenter {
    Task task;
    IView view;

    public MyPresenter(ShowActivity activity){
        task = new Task();
        view=activity;
    }
    @Override
    public void getData() {
        task.getUserList("http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1", new OkCallback() {
            @Override
            public void onUI(String json) {
                Log.i("TAG",json);
                Gson gson=new Gson();
                UserData userData = gson.fromJson(json, UserData.class);
                view.getUser(userData.results);
            }

            @Override
            public void onFailed(String json) {

            }
        });
    }

    @Override
    public void onDestroy() {
        view=null;
    }
}

3.View

1>.IView

public interface IView {
    public void getUser(List users);
}

2>.适配器

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

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public MyAdapter(Context context) {
        this.context = context;
        this.list=new ArrayList<>();
    }

    public void addData(List users){
        list.addAll(users);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public TextHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_user,viewGroup,false);
        return new TextHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TextHolder textHolder, int i) {
        final User user=list.get(i);
        Uri uri=Uri.parse(user.url);
        textHolder.user_img.setImageURI(uri);
        textHolder.user_name.setText(user.who);
        textHolder.user_desc.setText(user.desc);
        textHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener!=null){
                    onItemClickListener.getdata(user.url,user.who);
                }
            }
        });
    }

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

    class TextHolder extends RecyclerView.ViewHolder{
        ImageView user_img;
        TextView user_name,user_desc;
      public TextHolder(@NonNull View itemView) {
          super(itemView);
          user_img=itemView.findViewById(R.id.user_img);
          user_name=itemView.findViewById(R.id.user_name);
          user_desc=itemView.findViewById(R.id.user_desc);
      }
  }

  public interface  OnItemClickListener{
        void getdata(String url,String title);
  }
}

4.xml

2>.展示条目的数据





5.展示数据的activity

public class ShowActivity extends AppCompatActivity implements IView{

    RecyclerView recyclerView;
    MyPresenter presenter;
    MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);

        recyclerView=findViewById(R.id.recyclerview);

        presenter=new MyPresenter(this);
        presenter.getData();

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, OrientationHelper.VERTICAL));
        adapter=new MyAdapter(this);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
            @Override
            public void getdata(String url, String title) {
                EventBus.getDefault().post(new Chuan(url,title));
                finish();
            }
        });


    }

    @Override
    public void getUser(List users) {
        adapter.addData(users);
    }
}

6.传值

1.要传的属性类

@Entity
public class Chuan {
    @Id(autoincrement = true)
    public Long id;
    public String url;
    public String title;

    public Chuan(String url, String title) {
        this.title = title;
        this.url = url;
    }
    public Chuan() {
    }
    @Generated(hash = 95212819)
    public Chuan(Long id, String url, String title) {
        this.id = id;
        this.url = url;
        this.title = title;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }
}

2.接收值

public class MainActivity extends AppCompatActivity {
    TextView textView;
    SimpleDraweeView simpleDraweeView;
    String path="https://img2.woyaogexing.com/2018/07/30/b2773d0e982efa66!400x400_big.jpg";
    private ChuanDao chuanDao;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //注册
        EventBus.getDefault().register(this);

        textView=findViewById(R.id.textview);
        simpleDraweeView=findViewById(R.id.sdv);
        Uri uri=Uri.parse(path);
        simpleDraweeView.setImageURI(uri);

        simpleDraweeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it=new Intent(MainActivity.this,ShowActivity.class);
                startActivity(it);
            }
        });
        DaoSession daoSession= MyApp.getSession();
        chuanDao=daoSession.getChuanDao();

    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void getMsg(Chuan bean) {
        textView.setText(bean.title);
        simpleDraweeView.setImageURI(bean.url);

       Long id= bean.id;
       String title=bean.title;
       String url=bean.url;
       Chuan chuan=new Chuan(id,title,url);
       chuanDao.insert(chuan);
        List chuans=chuanDao.loadAll();
        for (int i=0;i 
  

7.app

public class MyApp extends Application {
    private static MyApp app;
    private static DaoSession session;
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
        app=this;
        setDB();
    }

    private void setDB() {
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this, "user.db", null);
        SQLiteDatabase database = devOpenHelper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(database);
        session=daoMaster.newSession();

    }

    public static DaoSession getSession(){
        return session;
    }
}

你可能感兴趣的:(数据展示+传值+存数据库)