码迷,mamicode.com
首页 > 其他好文 > 详细

Xlistview_聚合菜谱大全数据

时间:2017-08-16 21:54:06      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:绑定   content   super   add   syn   create   菜谱   network   creat   

public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{

    private XListView xlv;
   private int page=1;
    private boolean flag;
    private MyBaseadapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //http://apis.juhe.cn/cook/query?key=900eb2e99f3c7b21f3914aefa914a327&menu=%E8%A5%BF%E7%BA%A2%E6%9F%BF&rn=10&pn=3
        xlv = (XListView) findViewById(R.id.listview);
        xlv.setPullLoadEnable(true);
        xlv.setXListViewListener(this);
        data();
    }
    public void data(){
        jiexi("http://apis.juhe.cn/cook/query?key=900eb2e99f3c7b21f3914aefa914a327&menu=%E8%A5%BF%E7%BA%A2%E6%9F%BF&rn=10&pn="+page+"");
    }

    public void jiexi(String path){
        new AsyncTask<String, Void, String>() {
            @Override
            protected void onPostExecute(String s) {

                if (s!=null){
                    Gson gson=new Gson();
                    Bean bean = gson.fromJson(s, Bean.class);
                    List<Bean.ResultBean.DataBean> list = bean.getResult().getData();
                   // MyBaseadapter adapter= new MyBaseadapter(MainActivity.this, list);
                    if(adapter == null) {
                        adapter = new MyBaseadapter(MainActivity.this,list);
                        //3.将数据映射到ListView
                        xlv.setAdapter(adapter);
                    }else{
                        //将获取到的新数据添加到原来与adapter绑定集合里面
                        adapter.sss(list,flag);

                    }
                }
            }

            @Override
            protected String doInBackground(String... params) {
                try {
                    URL url=new URL(params[0]);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setReadTimeout(3000);
                    connection.setConnectTimeout(3000);
                    int code = connection.getResponseCode();
                    if (code==200){
                        InputStream is = connection.getInputStream();
                        return  StreamTools.readFromNetWork(is);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute(path);

    }

    @Override
    public void onRefresh() {
        flag=false;
        ++page;
        data();
        xlv.stopRefresh(true);
    }

    @Override
    public void onLoadMore() {
        flag=true;
        ++page;
        data();
        xlv.stopLoadMore();
    }
}

MainActivity的一些方法是生成的重点注意第一行要实现Xlistview.IXListViewListener

public class MyBaseadapter extends BaseAdapter {
    private List<Bean.ResultBean.DataBean> list;
    private Context context;
    public MyBaseadapter(Context context,List<Bean.ResultBean.DataBean> list){
        this.context=context;
        this.list=list;

    }
    public void sss(List<Bean.ResultBean.DataBean> data, boolean flag){
        for (Bean.ResultBean.DataBean s: data) {
            if (flag){
                list.add(s);
            }else{
                list.add(0,s);
            }

        }
          notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return list!=null?list.size():0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView=View.inflate(context,R.layout.item,null);
        ImageView image= (ImageView) convertView.findViewById(R.id.image);
        TextView tv= (TextView) convertView.findViewById(R.id.tv);
        tv.setText(list.get(position).getTitle());
        ImageLoader.getInstance().displayImage(list.get(position).getAlbums().get(0),image,MyAppliction.getDiskImageoptoins());
        return convertView;
    }
}

还有几个类就不写了,不懂得看上一篇,imageloader那里面有

MyApplication是imageloader的一些代码,Bean,gson解析类

Xlistview_聚合菜谱大全数据

标签:绑定   content   super   add   syn   create   菜谱   network   creat   

原文地址:http://www.cnblogs.com/leshen/p/7375657.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!