Dao
package com.bwei.guojinlong.Dao;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.ContactsContract;
import android.widget.ImageView;
import com.bwei.guojinlong.bean.DataLv;
import com.bwei.guojinlong.bean.DataPd;
import com.bwei.guojinlong.bean.DataVp;
import com.bwei.guojinlong.utils.MySQLite;
import java.util.ArrayList;
import java.util.List;
/**
* Created by 郭金龙 on 2017/8/24.
*/
public class MyDao {
static final String TABLE1="banner";
static final String TABLE2="data";
static final String TABLE3="channel1";
MySQLite mySQLite;
public MyDao(Context context){
mySQLite=new MySQLite(context);
}
public void insertbanner(List<DataVp.BannerBean> list){
SQLiteDatabase db = mySQLite.getWritableDatabase();
for (int i = 0; i < list.size(); i++) {
ContentValues values=new ContentValues();
values.put("img",list.get(i).getImage_url());
db.insert(TABLE1,null,values);
}
}
public void insertdata(List<DataLv.DataBean> list){
SQLiteDatabase db = mySQLite.getWritableDatabase();
for (int i = 0; i < list.size(); i++) {
ContentValues values=new ContentValues();
values.put("title",list.get(i).getTitle());
values.put("content",list.get(i).getContent());
values.put("img",list.get(i).getImage_url());
db.insert(TABLE2,null,values);
}
}
// public void insertchannel1(List<DataPd.ChannelBean> list){
// SQLiteDatabase db = mySQLite.getWritableDatabase();
// for (int i = 0; i < list.size(); i++) {
// ContentValues values=new ContentValues();
// values.put("channel_me",list.get(i).getChannel_me());
// values.put("channel_more",list.get(i).getChannel_more());
// db.insert(TABLE3,null,values);
// }
// }
public void insertchannel1(ContentValues values){
SQLiteDatabase db = mySQLite.getWritableDatabase();
db.insert(TABLE3,null,values);
}
public List<String> querychannel1(int type){
List<String> list=new ArrayList<String>();
SQLiteDatabase db = mySQLite.getReadableDatabase();
Cursor query=db.query(TABLE3,null,"type=?",new String[]{type+""},null,null,null);
while (query.moveToNext()){
String name = query.getString(1);
list.add(name);
}
return list;
}
public void updatechannel1(String type,String name){
SQLiteDatabase db = mySQLite.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("type",type);
db.update(TABLE3,values,"name=?",new String[]{name});
db.close();
}
public List<DataVp.BannerBean> querybanner(){
List<DataVp.BannerBean> list=new ArrayList<DataVp.BannerBean>();
SQLiteDatabase db = mySQLite.getReadableDatabase();
Cursor query = db.query(TABLE1, null, null, null, null, null, null, null);
while (query.moveToNext()){
String img=query.getString(query.getColumnIndex("img"));
DataVp.BannerBean data=new DataVp.BannerBean();
data.setImage_url(img);
list.add(data);
}
return list;
}
public List<DataLv.DataBean> querydata(){
List<DataLv.DataBean> list=new ArrayList<DataLv.DataBean>();
SQLiteDatabase db = mySQLite.getReadableDatabase();
Cursor query = db.query(TABLE2, null, null, null, null, null, null, null);
while (query.moveToNext()){
String title=query.getString(query.getColumnIndex("title"));
String content=query.getString(query.getColumnIndex("content"));
String img=query.getString(query.getColumnIndex("img"));
DataLv.DataBean data=new DataLv.DataBean();
data.setTitle(title);
data.setContent(content);
data.setImage_url(img);
list.add(data);
}
return list;
}
public List<DataPd.ChannelBean> querychannel1(){
List<DataPd.ChannelBean> list=new ArrayList<DataPd.ChannelBean>();
SQLiteDatabase db = mySQLite.getReadableDatabase();
Cursor query = db.query(TABLE3, null, null, null, null, null, null, null);
while (query.moveToNext()){
String channel_me=query.getString(query.getColumnIndex("channel_me"));
String channel_more=query.getString(query.getColumnIndex("channel_more"));
DataPd.ChannelBean data=new DataPd.ChannelBean();
data.setChannel_me(channel_me);
data.setChannel_more(channel_more);
list.add(data);
}
return list;
}
}
fragment 里的数据存储
package com.bwei.guojinlong.fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.bwei.guojinlong.Dao.MyDao;
import com.bwei.guojinlong.bean.DataLv;
import com.bwei.guojinlong.bean.DataVp;
import com.bwei.guojinlong.utils.GetJson;
import com.example.guojinlong20170823atest.R;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
/**
* Created by 2 on 2017/8/23.
*/
public class FragmentOne extends Fragment {
MyDao dao;
ListView lv;
ViewPager vp;
DataVp datavp;
DataLv datalv;
List<ImageView> listimg;
RadioGroup head_rg;
int index = 100;
List<DataVp.BannerBean> querybanner;
Handler handler = new Handler();
View head;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.activity_body, null);
vp=view.findViewById(R.id.vp);
dao=new MyDao(getActivity());
head_rg = view.findViewById(R.id.head_rg);
lv = view.findViewById(R.id.lv);
// head = View.inflate(getActivity(), R.layout.activity_head, null);
// vp = head.findViewById(R.id.vp);
// head_rg = head.findViewById(R.id.head_rg);
initVpandLvData();
// initNtData();
// lv.addHeaderView(head);
return view;
}
private void initVpandLvData() {
new AsyncTask<String, String, String>() {
@Override
protected String doInBackground(String... strings) {
//List<DataVp.BannerBean> querybanner = dao.querybanner();
querybanner = dao.querybanner();
//datavp=new DataVp();
//datavp.setBanner(querybanner);
List<DataLv.DataBean> querydata = dao.querydata();
datalv=new DataLv();
datalv.setData(querydata);
return null;
}
@Override
protected void onPostExecute(String s) {
getVPData();
getVpDataAdapter();
getlvData();
super.onPostExecute(s);
}
}.execute();
}
private void initNtData() {
new AsyncTask<String, String, String>() {
@Override
protected String doInBackground(String... strings) {
Gson gson = new Gson();
datavp = gson.fromJson(GetJson.getVpJson(), DataVp.class);
datalv=gson.fromJson(GetJson.getLvJson(), DataLv.class);
return null;
}
@Override
protected void onPostExecute(String s) {
getVPData();
getVpDataAdapter();
getlvData();
dao.insertbanner(datavp.getBanner());
dao.insertdata(datalv.getData());
super.onPostExecute(s);
}
}.execute();
}
public void getVPData() {
listimg = new ArrayList<ImageView>();
//for (int i = 0; i < datavp.getBanner().size(); i++) {
for (int i = 0; i < querybanner.size(); i++) {
ImageView img = new ImageView(getActivity());
img.setScaleType(ImageView.ScaleType.FIT_XY);
//ImageLoader.getInstance().displayImage(datavp.getBanner().get(i).getImage_url(), img);
ImageLoader.getInstance().displayImage(querybanner.get(i).getImage_url(), img);
listimg.add(img);
}
}
public void getVpDataAdapter(){
MyViewpage myViewpage = new MyViewpage();
vp.setAdapter(myViewpage);
vp.setCurrentItem(1000);
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position % listimg.size()) {
case 0:
head_rg.check(R.id.head_rb1);
break;
case 1:
head_rg.check(R.id.head_rb2);
break;
case 2:
head_rg.check(R.id.head_rb3);
break;
case 3:
head_rg.check(R.id.head_rb4);
break;
default:
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
handler.postDelayed(new Runnable() {
@Override
public void run() {
int id = vp.getCurrentItem();
id++;
vp.setCurrentItem(id);
handler.postDelayed(this, 2000);
}
}, 2000);
}
public void getlvData(){
MylvAdapter mylvapapter=new MylvAdapter();
lv.setAdapter(mylvapapter);
}
class MylvAdapter extends BaseAdapter{
@Override
public int getCount() {
if(datalv!=null){
return datalv.getData().size();
}
return 0;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if(view==null){
view=View.inflate(getActivity(),R.layout.activity_item,null);
holder=new ViewHolder();
holder.img_rul=view.findViewById(R.id.img_url);
holder.title=view.findViewById(R.id.title);
holder.content=view.findViewById(R.id.content);
view.setTag(holder);
}else{
holder=(ViewHolder) view.getTag();
}
holder.content.setText(datalv.getData().get(i).getContent());
holder.title.setText(datalv.getData().get(i).getTitle());
ImageLoader.getInstance().displayImage(datalv.getData().get(i).getImage_url(),holder.img_rul);
return view;
}
}
class ViewHolder{
ImageView img_rul;
TextView title,content;
}
class MyViewpage extends PagerAdapter {
@Override
public int getCount() {
if (listimg != null) {
return Integer.MAX_VALUE;
}
return 0;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
position %= listimg.size();
container.addView(listimg.get(position));
return listimg.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// super.destroyItem(container, position, object);
container.removeView((View) object);
}
}
}
网友评论