Pull Torefresh ListView

5949 ワード

アップデートしてローディングしたのは、リブラとgsonです。
MainActivity
public class MainActivity extends AppCompatActivity {
    String url="http://169.254.167.244:8080/Web_Data/GetPhones?pagesize=20&pageindex=";
    int index=1;
    private PullToRefreshListView lv;
    List> list;
    MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv=(PullToRefreshListView)findViewById(R.id.lv);
        list=new ArrayList<>();
        adapter=new MyAdapter(this,list);
        lv.setAdapter(adapter);
        getData();
        //       
        lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshBase refreshView) {
                index=1;//         ,         
                list.clear();//            
                getData();
            }
        });

        //          
        lv.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                index++;//     
                getData();
            }
        });
    }

    public void getData(){
        new MyAsyncTask(new MyAsyncTask.CallBack() {
            @Override
            public void sendResult(String json) {
                Gson gson=new Gson();
                Phone phone = gson.fromJson(json, Phone.class);
                for (int i=0;i map=new HashMap();
                    map.put("id",String.valueOf(phone.getPhones().get(i).getId()));
                    map.put("brand",phone.getPhones().get(i).getBrand());
                    list.add(map);
                }
                adapter.notifyDataSetChanged();
                lv.onRefreshComplete();//       ,    
            }
        }).execute(url+index);
    }
}
アダプター
public class MyAdapter extends BaseAdapter {
    Context context;
    List> list;
    public MyAdapter(Context context, List> list){
        this.context=context;
        this.list=list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @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) {
        ViewHolder vh;
        if(convertView==null){
            convertView=View.inflate(context,R.layout.list_item,null);
            vh=new ViewHolder();
            vh.tv_id=(TextView)convertView.findViewById(R.id.tv_id);
            vh.tv_brand=(TextView)convertView.findViewById(R.id.tv_brand);
            convertView.setTag(vh);
        }else {
            vh=(ViewHolder)convertView.getTag();
        }
        vh.tv_id.setText(list.get(position).get("id"));
        vh.tv_brand.setText(list.get(position).get("brand"));
        return convertView;
    }

    class ViewHolder{
        TextView tv_id;
        TextView tv_brand;
    }
}
非同期タスクローディングネットワークからデータをダウンロードする
public class MyAsyncTask extends AsyncTask {
    //Context context;
    CallBack callBack;
    public MyAsyncTask(CallBack callBack){
        //this.context=context;
        this.callBack=callBack;
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            URL url=new URL(params[0]);
            HttpURLConnection huc=(HttpURLConnection) url.openConnection();
            huc.setConnectTimeout(3000);
            huc.connect();
            if(huc.getResponseCode()==200){
                InputStream inputStream = huc.getInputStream();
                byte[] b=new byte[1024];
                int len=0;
                StringBuilder sb=new StringBuilder();
                while ((len=inputStream.read(b))!=-1){
                    sb.append(new String(b,0,len));
                }
                return sb.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        callBack.sendResult(s);
    }

    interface CallBack{
        void sendResult(String json);
    }
}
実体類はしません。
MainActivityレイアウト

    

リスト.アイテムレイアウト