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

单纯的xlistview

时间:2017-08-24 14:56:47      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:json   int   syn   count()   top   exception   api   result   stat   

public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{

    private XListView xlv;
    private int page=1;
    private boolean flag;
    private List<Bean.ResultBean.DataBean> list;
    private MyBaseadapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        xlv = (XListView) findViewById(R.id.xlv);
        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.getResult().getData();
                      if (adapter==null) {
                          adapter = new MyBaseadapter(MainActivity.this, list);
                          xlv.setAdapter(adapter);
                      }else{
                          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 info.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();
    }
}
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 null;
    }

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

    @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.textView);
        ImageLoader.getInstance().displayImage(list.get(position).getAlbums().get(0),image,appliction.getImageOptions());
        tv.setText(list.get(position).getTitle());
        return convertView;
    }
}

 

单纯的xlistview

标签:json   int   syn   count()   top   exception   api   result   stat   

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

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