存储数据库展示

public classOneextendsFragmentimplementsXListView.IXListViewListener {

privateListbeanList;
privateXListViewxListView;
private intstartNum=0;
privateMyAdapteradapter;
privateSQLiteDatabasedb;
privateXnewsDBxnewsDB;
privateListlist;
privateAadapteraadapter;
privateHandlerxhandler=newHandler() {
@Override
public voidhandleMessage(Message msg) {
String s = msg.obj.toString();
Gson gson =newGson();
if(isNetworkConnected(getActivity())){
Xnews xnews = gson.fromJson(s,Xnews.class);
beanList.addAll(xnews.getData());
adapter.notifyDataSetChanged();
}else{
Neww neww = gson.fromJson(s,Neww.class);
list.add(neww);
aadapter.notifyDataSetChanged();
}
stopXlist();
insert();
}
private voidstopXlist() {
xListView.stopRefresh();
xListView.stopLoadMore();
xListView.setRefreshTime("刚刚");
}
};
private voidinsert() {
for(Xnews.DataBean bean :beanList) {
ContentValues values =newContentValues();
values.put("id",bean.getID());
values.put("title",bean.getTITLE());
values.put("froname",bean.getFROMNAME());
values.put("image",bean.getIMAGEURL());
longaNew =db.insert("new", null,values);
if(aNew >0) {
Toast.makeText(getActivity(),"添加成功",Toast.LENGTH_SHORT).show();
}
}
}
@Nullable
@Override
publicViewonCreateView(LayoutInflater inflater,@NullableViewGroup container,@NullableBundle savedInstanceState) {
View view = View.inflate(getActivity(),R.layout.activity_one, null);
if(isNetworkConnected(getActivity())) {
xnewsDB=newXnewsDB(getActivity());
db=xnewsDB.getReadableDatabase();
xListView= (XListView) view.findViewById(R.id.xlist);
beanList=newArrayList<>();
xListView.setPullLoadEnable(true);
xListView.setXListViewListener(this);
adapter=newMyAdapter();
loaDate();
xListView.setAdapter(adapter);
Toast.makeText(getActivity(),"网络可用",Toast.LENGTH_LONG).show();
}else{
xnewsDB=newXnewsDB(getActivity());
db=xnewsDB.getReadableDatabase();
xListView= (XListView) view.findViewById(R.id.xlist);
xListView.setPullLoadEnable(true);
xListView.setXListViewListener(this);
show();
aadapter=newAadapter();
xListView.setAdapter(aadapter);
Toast.makeText(getActivity(),"网络不可用",Toast.LENGTH_LONG).show();
}
returnview;
}
private voidshow() {
list=newArrayList<>();
Cursor cursor =db.rawQuery("select * from new", null);
while(cursor.moveToNext()) {
【存储数据库展示】String id = cursor.getString(cursor.getColumnIndex("id"));
String title = cursor.getString(cursor.getColumnIndex("title"));
String froname = cursor.getString(cursor.getColumnIndex("froname"));
String image = cursor.getString(cursor.getColumnIndex("image"));
Neww neww =newNeww(id,title,froname,image);
list.add(neww);
}
}
public booleanisNetworkConnected(Context context) {
if(context !=null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if(mNetworkInfo !=null) {
returnmNetworkInfo.isAvailable();
}
}
return false;
}
private voidloaDate() {
newThread() {
@Override
public voidrun() {
String urlConnect = Net.getUrlConnect("http://www.93.gov.cn/93app/data.do?channelId=0&startNum="+startNum);
Message message = Message.obtain();
message.obj= urlConnect;
xhandler.sendMessage(message);
}
}.start();
}
@Override
public voidonRefresh() {
startNum++;
if(isNetworkConnected(getActivity())) {
beanList.clear();
}else{
list.clear();
}
loaDate();
}
@Override
public voidonLoadMore() {
startNum++;
loaDate();
}
classMyAdapterextendsBaseAdapter {
@Override
public intgetCount() {
returnbeanList.size();
}
@Override
publicObjectgetItem(inti) {
returnbeanList.get(i);
}
@Override
public longgetItemId(inti) {
returni;
}
@Override
publicViewgetView(inti,View view,ViewGroup viewGroup) {
ViewHolder holder;
if(view ==null) {
holder =newViewHolder();
view = View.inflate(getActivity(),R.layout.item, null);
holder.imageView= (ImageView) view.findViewById(R.id.i1);
holder.textView= (TextView) view.findViewById(R.id.t1);
holder.textView2= (TextView) view.findViewById(R.id.t2);
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
holder.textView.setText(beanList.get(i).getTITLE());
holder.textView2.setText(beanList.get(i).getFROMNAME());
Glide.with(getActivity()).load(beanList.get(i).getIMAGEURL()).into(holder.imageView);
returnview;
}
}
classViewHolder {
ImageViewimageView;
TextViewtextView,textView2;
}
classAadapterextendsBaseAdapter {
@Override
public intgetCount() {
returnlist.size();
}
@Override
publicObjectgetItem(inti) {
returnlist.get(i);
}
@Override
public longgetItemId(inti) {
returni;
}
@Override
publicViewgetView(inti,View view,ViewGroup viewGroup) {
ViewHolder2 holder;
if(view ==null) {
holder =newViewHolder2();
view = View.inflate(getActivity(),R.layout.item, null);
holder.imageView= (ImageView) view.findViewById(R.id.i1);
holder.textView= (TextView) view.findViewById(R.id.t1);
holder.textView2= (TextView) view.findViewById(R.id.t2);
view.setTag(holder);
}else{
holder = (ViewHolder2) view.getTag();
}
holder.textView.setText(list.get(i).getTitle());
holder.textView2.setText(list.get(i).getFroname());
Glide.with(getActivity()).load(list.get(i).getImage()).into(holder.imageView);
returnview;
}
}
classViewHolder2 {
ImageViewimageView;
TextViewtextView,textView2;
}
}

    推荐阅读