Android--------------无线轮播,Xlistview展示数据并保存到数据库。

public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{
    private Banner ban;
    private XListView mxv;
    private int type=1;
    //加载内容的接口
    private String url="http://qhb.2dyt.com/Bwei/news?page=1&postkey=1503d&type=";
    private ArrayList<News1.ListBean> mlist=new ArrayList<>();
    private Myadapter md;
    private Sqlhelp sh;
    private SQLiteDatabase db;
    private  long l;
    private ArrayList<User> list=new ArrayList<>();
    //轮播图的接口
    private String urlpath="http://api.kuaikanmanhua.com/v1/daily/comic_lists/0?since=0&gender=0&sa_event=eyJwcm9qZWN0Ijoia3VhaWthbl9hcHAiLCJ0aW1lIjoxNDg5NjQzNjMzNzI1LCJwcm9wZXJ0aWVzIjp7IkhvbWVwYWdlVGFiTmFtZSI6IueDremXqCIsIlZDb21tdW5pdHlUYWJOYW1lIjoi54Ot6ZeoIiwiJG9zX3ZlcnNpb24iOiI0LjQuMiIsIkdlbmRlclR5cGUiOiLlpbPniYgiLCJGcm9tSG9tZXBhZ2VUYWJOYW1lIjoi54Ot6ZeoIiwiJGxpYl92ZXJzaW9uIjoiMS42LjM0IiwiJG5ldHdvcmtfdHlwZSI6IldJRkkiLCIkd2lmaSI6dHJ1ZSwiJG1hbnVmYWN0dXJlciI6IkxFTk9WTyIsIkZyb21Ib21lcGFnZVVwZGF0ZURhdGUiOjAsIiRzY3JlZW5faGVpZ2h0IjoxNDQwLCJIb21lcGFnZVVwZGF0ZURhdGUiOjEsIlByb3BlcnR5RXZlbnQiOiJSZWFkSG9tZVBhZ2UiLCJGaW5kVGFiTmFtZSI6IuaOqOiNkCIsImFidGVzdF9ncm91cCI6MTAsIiRzY3JlZW5fd2lkdGgiOjkwMCwiJG9zIjoiQW5kcm9pZCIsIlRyaWdnZXJQYWdlIjoiSG9tZVBhZ2UiLCIkY2FycmllciI6IkNISU5BIE1PQklMRSIsIiRtb2RlbCI6Ikxlbm92byBQNzAtdCIsIiRhcHBfdmVyc2lvbiI6IjMuOS4yIn0sInR5cGUiOiJ0cmFjayIsImRpc3RpbmN0X2lkIjoiQTo1MDdCOUQyN0Q2NUEwMDAwIiwib3JpZ2luYWxfaWQiOiJBOjUwN0I5RDI3RDY1QTAwMDAiLCJldmVudCI6IlJlYWRIb21lUGFnZSJ9";
    private Handler hand=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            //轮播图
           if (msg.what==1){
               Gson gson=new Gson();
               News news = gson.fromJson(msg.obj.toString(), News.class);
               ArrayList<String> titles=new ArrayList<>();
               ArrayList<String> images=new ArrayList<>();
               List<News.DataBean.ComicsBean> com = news.getData().getComics();
               for (News.DataBean.ComicsBean q:com){
                   titles.add(q.getTitle());
                   images.add(q.getCover_image_url());

               }
               ban.setImages(images);
               ban.setBannerTitles(titles);
               ban.start();

           }
             //展示的详细数据
            if (msg.what==2){
                Gson gson=new Gson();
                News1 news1 = gson.fromJson(msg.obj.toString(), News1.class);
                mlist.addAll(news1.getList());
                md.notifyDataSetChanged();
                stop();
                //把数据存到数据库
                cun();

            }

        }
    };
     //把数据存到数据库的方法
    private void cun() {
        ContentValues values=new ContentValues();
        for (News1.ListBean qq:mlist){
           values.put("title",qq.getTitle());
            values.put("idd",qq.getId());
            values.put("image",qq.getPic());
             l = db.insert("user", null, values);
        }
        if (l>0){
            Toast.makeText(this,"插入成功!!",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this,"插入失败!!!",Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (isNetworkAvailable(this)){
            //有网的情况下
            sh=new Sqlhelp(this);
            db = sh.getWritableDatabase();
            initview();
            //加载轮播接口
            initdata();
            loaddata();
        }else {
            //没网的情况下查询数据库中的信息并存到User类中,并用Xlistview展示出数据。
            mxv=(XListView)findViewById(R.id.x_listview) ;
            sh=new Sqlhelp(this);
            db = sh.getWritableDatabase();
            Cursor cursor = db.rawQuery("select * from user", null);
            while (cursor.moveToNext()) {
                String title = cursor.getString(cursor.getColumnIndex("title"));
                String idd = cursor.getString(cursor.getColumnIndex("idd"));
                String image = cursor.getString(cursor.getColumnIndex("image"));
                User user = new User(title, idd, image);
                list.add(user);
            }
            //设置User类的适配器
            Useradapter uad=new Useradapter(this,list);
            mxv.setAdapter(uad);
        }


    }
    //加载详细数据的方法
    private void loaddata() {
        new Thread(){
            @Override
            public void run() {
                String str1 = Httpurl.getUrlConnect(url + type);
                Message msg=Message.obtain();
                msg.what=2;
                msg.obj=str1;
                hand.sendMessage(msg);
            }
        }.start();


    }

    private void initview() {
        ban=(Banner)findViewById(R.id.ban);
        mxv=(XListView)findViewById(R.id.x_listview) ;
        mxv.setPullLoadEnable(true);
        mxv.setXListViewListener(this);
        //设置图片加载器
        BannerImageload bil=new BannerImageload();
        ban.setImageLoader(bil);
        //设置显示样式(显示数字指示器和标题)
        ban.setBannerStyle(BannerConfig.NUM_INDICATOR_TITLE);

        //设置适配器
        md=new Myadapter(this,mlist);
        mxv.setAdapter(md);
        //xlistview的监听事件
        mxv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                News1.ListBean bean = mlist.get(position-1);
                Intent intent=new Intent(MainActivity.this,NextActivity.class);
                intent.putExtra("aa",bean.getPic());
                startActivity(intent);


            }
        });



    }
   //加载轮播图的方法
    private void initdata() {
        new Thread(){
            @Override
            public void run() {
                String str = Httpurl.getUrlConnect(urlpath);
                Message msg=Message.obtain();
                msg.what=1;
                msg.obj=str;
                hand.sendMessage(msg);
            }
        }.start();


    }

    @Override
    public void onRefresh() {
        type=1;
        mlist.clear();
        loaddata();

    }

    @Override
    public void onLoadMore() {
        type++;
        loaddata();


    }
    private void stop() {
        mxv.stopLoadMore();
        mxv.stopRefresh();
        mxv.setRefreshTime("刚刚");

    }
    //判断网络是否连接的方法
    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected())
            {
                // 当前网络是连接的
                if (info.getState() == NetworkInfo.State.CONNECTED)
                {
                    // 当前所连接的网络可用
                    return true;
                }
            }
        }
        return false;
    }
}

效果图:  点击后:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值