package com.example.moni.tab;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.ListView;
import android.widget.Toast;
import com.example.moni.HTTPUtil;
import com.example.moni.HTTPtitle;
import com.example.moni.MainActivity;
import com.example.moni.R;
import com.example.moni.adapter.TTAdapter;
import com.example.moni.base.BaseFragment;
import com.example.moni.bean.One;
import com.example.moni.bean.Three;
import com.example.moni.bean.Two;
import com.example.moni.sql.MyHelper;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.List;
public class Tab1 extends BaseFragment {
private PullToRefreshListView listView;
private SQLiteDatabase db;
private String str="https://api.apiopen.top/musicBroadcasting";
private List<One> channellist;
@Override
public int bindlayout() {
return R.layout.tab01;
}
@Override
protected void initView() {
listView = getView().findViewById(R.id.pull);
//允许上啦下
listView.setMode(PullToRefreshListView.Mode.BOTH);
//滑动刷新
listView.setScrollingWhileRefreshingEnabled(true);
//获取SQLite
MyHelper myHelper = new MyHelper(getActivity());
db = myHelper.getWritableDatabase();
}
@Override
protected void initData() {
if (HTTPUtil.iswork((MainActivity) getActivity())){
HTTPtitle.getAsyncTask(str,new HTTPtitle.CALLbACK() {
@Override
public void getDate(String s) {
Gson gson=new Gson();
Three three = gson.fromJson(s,Three.class);
channellist = three.getResult().get(0).getChannellist();
//设置适配器
TTAdapter ttAdapter = new TTAdapter(getActivity(), channellist);
listView.setAdapter(ttAdapter);
//查询数据库
Cursor query = db.query("person", null, null, null, null, null, null);
if (!query.moveToFirst()){
ContentValues values=new ContentValues();
values.put("TIT",s);
db.insert("person",null,values);
}
}
});
}else{
//没有网的时候查询
Toast.makeText(getActivity(),"没有网络",Toast.LENGTH_SHORT).show();
Cursor query = db.query("person", null, null, null, null, null, null);
if (query.moveToFirst()){
String s="";
do {
s=query.getString(query.getColumnIndex("TIT"));
}while (query.moveToNext());
Gson gson=new Gson();
Three three = gson.fromJson(s,Three.class);
channellist = three.getResult().get(0).getChannellist();
//设置适配器
TTAdapter ttAdapter = new TTAdapter(getActivity(), channellist);
listView.setAdapter(ttAdapter);
}
}
}
@Override
protected void Event() {
listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
if (HTTPUtil.iswork((MainActivity) getActivity())){
HTTPtitle.getAsyncTask(str,new HTTPtitle.CALLbACK() {
@Override
public void getDate(String s) {
Gson gson=new Gson();
Three three = gson.fromJson(s,Three.class);
List<One> channellist1 = three.getResult().get(0).getChannellist();
channellist.addAll(channellist1);
listView.onRefreshComplete();
}
});
}else{
Toast.makeText(getActivity(),"没有网络",Toast.LENGTH_SHORT).show();
}
listView.onRefreshComplete();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
if (HTTPUtil.iswork((MainActivity) getActivity())){
HTTPtitle.getAsyncTask(str,new HTTPtitle.CALLbACK() {
@Override
public void getDate(String s) {
Gson gson=new Gson();
Three three = gson.fromJson(s,Three.class);
List<One> channellist2 = three.getResult().get(0).getChannellist();
channellist.addAll(channellist2);
listView.onRefreshComplete();
}
});
}else{
Toast.makeText(getActivity(),"没有网络",Toast.LENGTH_SHORT).show();
}
listView.onRefreshComplete();
}
});
}
}
数据库核心
最新推荐文章于 2023-04-20 16:26:40 发布