XListview(导入第三方module)


import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.google.gson.Gson;
import com.hgz.test.myxlistview.bean.Info;
import com.hgz.test.myxlistview.utils.StreamTools;
import com.limxing.xlistview.view.XListView;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener {

    private boolean flag;
    private int index = 1;
    private XListView lv;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (XListView) findViewById(R.id.lv);
        lv.setPullLoadEnable(true);
        lv.setXListViewListener(this);
        getData("http://apis.juhe.cn/cook/query.php", index + "", 10 + "");

    }

    //当下拉刷新的时候,会执行
    @Override
    public void onRefresh() {
        ++index;
        getData("http://apis.juhe.cn/cook/query.php", index + "", 10 + "");
        flag=true;
        //刷新成功之后,将下拉刷新进度回弹
        lv.stopRefresh(true);
    }

    //当上拉加载更多的是后,会被执行
    @Override
    public void onLoadMore() {
        ++index;
        getData("http://apis.juhe.cn/cook/query.php", index + "", 10 + "");
        flag=false;
        //停止加载更多
        lv.stopLoadMore();
    }

    private class MyAdapter extends BaseAdapter {
        private List<Info.ResultBean.DataBean> result;
        public MyAdapter(List<Info.ResultBean.DataBean> result){
            this.result=result;
        }
        private void loadMore(List<Info.ResultBean.DataBean> data,boolean flag) {
            for (Info.ResultBean.DataBean datas : data) {
                if (flag){
                    result.add(0,datas);
                }else{
                    result.add(datas);
                }
            }
        }
        @Override
        public int getCount() {
            return result.size();
        }

        @Override
        public Object getItem(int i) {
            return result.get(i);
        }

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

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if (view == null) {
                view = View.inflate(MainActivity.this, R.layout.listview_items, null);
            }
            TextView itemText = view.findViewById(R.id.itemText);
            itemText.setText(result.get(i).getTitle());
            return view;
        }
    }



    private void getData(String path, final String pn, final String rn) {
        new AsyncTask<String, Void, String>() {
            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                if (s == null)
                    return;
                ;
                Gson gson = new Gson();
                Info info = gson.fromJson(s, Info.class);
                List<Info.ResultBean.DataBean> result = info.getResult().getData();
                if (adapter == null) {
                    adapter = new MyAdapter(result);
                    lv.setAdapter(adapter);
                } else {
                    adapter.loadMore(result,flag);
                    adapter.notifyDataSetChanged();
                }

            }

            @Override
            protected String doInBackground(String... strings) {
                try {
                    String string = strings[0];
                    URL url = new URL(string);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("POST");
                    connection.setReadTimeout(5000);
                    connection.setConnectTimeout(5000);
                    OutputStream os = connection.getOutputStream();
                    os.write(("key=1fe0aebec3396c977560e16f14613413&pn=" + pn + "&rn=" + rn + "&menu=红烧肉").getBytes());
                    os.flush();
                    int code = connection.getResponseCode();
                    if (code == 200) {
                        InputStream is = connection.getInputStream();
                        String json = StreamTools.getDatas(is);
                        System.out.println(json.toString());
                        return json;

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute(path, pn, rn);
    }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SuperMonsterH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值