public class MainActivity extends AppCompatActivity {
PullToRefreshListView pullToRefreshListView;
String url="http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=";
List list;
MyAdapter myAdapter;
private int page=1;
Button b1,b2;
DaoSession daoSession;
DataBean dataBean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pullToRefreshListView=findViewById(R.id.My_Pt);
pullToRefreshListView.setMode((PullToRefreshBase.Mode.BOTH));
b1=findViewById(R.id.b1);
b2=findViewById(R.id.b2);
daoSession=MyApp.getdaosession();
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i=0;i<list.size();i++){
dataBean=new DataBean(list.get(i).getTitle(),
list.get(i).getPic(),
list.get(i).getFood_str(),
list.get(i).getCollect_num());
daoSession.insert(dataBean);
}
Toast.makeText(MainActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
QueryBuilder dataBeanQueryBuilder=daoSession.queryBuilder(DataBean.class);
List list1 = dataBeanQueryBuilder.list();
Toast.makeText(MainActivity.this, list1.get(1).getTitle(), Toast.LENGTH_SHORT).show();
}
});
myAdapter=new MyAdapter(MainActivity.this);
getdata(1);
pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
@Override
public void onPullDownToRefresh(PullToRefreshBase pullToRefreshBase) {
list.clear();
getdata(1);
myAdapter.notifyDataSetChanged();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase pullToRefreshBase) {
page++;
getmoredate(page);
pullToRefreshListView.onRefreshComplete();
}
});
}
private class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Context context;
public MyAdapter(Context context) {
this.context = context;
mInflater=LayoutInflater.from(context);
}
// public MyAdapter(Context context){
// mInflater=LayoutInflater.from(context);
// }
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder = null;
if (view==null){
viewHolder=new ViewHolder();
view=mInflater.inflate(R.layout.item,null);
viewHolder.title=view.findViewById(R.id.item_textview);
viewHolder.content = view.findViewById(R.id.item_content);
viewHolder.imageView=view.findViewById(R.id.item_img);
view.setTag(viewHolder);
}else {
viewHolder= (ViewHolder) view.getTag();
}
viewHolder.title.setText(list.get(i).getTitle());
viewHolder.content.setText(list.get(i).getCollect_num());
Picasso.with(context).load(list.get(i).getPic()).into(viewHolder.imageView);
return view;
}
class ViewHolder{
TextView title;
TextView content;
ImageView imageView;
}
}
public void getdata(int page){
RequestQueue requestQueue= Volley.newRequestQueue(this);
StringRequest stringRequest=new StringRequest(Request.Method.GET,
url + page,
new Response.Listener() {
@Override
public void onResponse(String response) {
Gson gson=new Gson();
Bean bean=gson.fromJson(response,Bean.class);
list=bean.getData();
pullToRefreshListView.setAdapter(myAdapter);
pullToRefreshListView.onRefreshComplete();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(stringRequest);
}
public void getmoredate(int page){
RequestQueue requestQueue=Volley.newRequestQueue(this);
StringRequest stringRequest=new StringRequest(Request.Method.GET,
url + page, new Response.Listener() {
@Override
public void onResponse(String response) {
Gson gson=new Gson();
Bean bean = gson.fromJson(response, Bean.class);
list.addAll(bean.getData());
myAdapter.notifyDataSetChanged();
pullToRefreshListView.onRefreshComplete();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(stringRequest);
}
}
-----------------------------------------------------------------------------------
public class DataBean {
private String title;
String Pic;
String Name;
String num;
@Generated(hash = 1626917387)
public DataBean(String title, String Pic, String Name, String num) {
this.title = title;
this.Pic = Pic;
this.Name = Name;
this.num = num;
}
@Generated(hash = 908697775)
public DataBean() {
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPic() {
return Pic;
}
public void setPic(String pic) {
Pic = pic;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
}
----------------------------------------------------------------------
public class MyApp extends Application {
static DaoSession daoSession;
@Override
public void onCreate() {
super.onCreate();
DaoMaster.DevOpenHelper devOpenHelper=new DaoMaster.DevOpenHelper(getApplicationContext(),"My.db");
Database writableDatabase = devOpenHelper.getWritableDb();
daoSession=new DaoMaster(writableDatabase).newSession();
}
public static DaoSession getdaosession(){
return daoSession;
}
}
--------------------------------------------------------------------------------------
xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="添加到数据库"/>
<Button
android:id="@+id/b2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="展示数据库数据"
android:layout_below="@id/b1"/>
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/My_Pt"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_below="@id/b2">
com.handmark.pulltorefresh.library.PullToRefreshListView>
RelativeLayout>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
implementation 'com.mcxiaoke.volley:library:1.0.19'
implementation 'com.google.code.gson:gson:2.2.4'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.squareup.picasso:picasso:2.3.2'
implementation 'org.greenrobot:greendao:3.2.2'
implementation 'com.github.userswlwork:pull-to-refresh:1.0.0'