Android,使用Retrofit填充Endless List View数据的最佳方式?

时间:2015-04-09 02:06:04

标签: java android web-services retrofit

我已成功将数据填充到listview中,但我的方式看起来非常糟糕,因为当我滚动列表视图时,改造总是获得All the Repository。那么,通过改造来填充Endless List视图数据的最佳方法是什么?谢谢......

MainActivity.java

    public class MainActivity extends Activity {

        public static final String ENDPOINT = "http://rfhan.com";
        private List<Tempat> tempatList;
        private TempatAdapter tadapter;
        private EndlessListView lv;
        private int start=0;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tempatList = new ArrayList<Tempat>();
            tadapter = new TempatAdapter(this, R.layout.item_tempat, tempatList);
            lv = (EndlessListView) findViewById(R.id.el);
            lv.setLoadingView(R.layout.loading_layout);
            lv.setAdapter(tadapter);

            //init data to my list
            requestData();

            //scroll listener
            lv.setListener(new EndlessListener() {

                @Override
                public void loadData() {
                    requestData();
                }
            });
        }

        private void requestData(){

            RestAdapter adapter = new RestAdapter.Builder()
                .setEndpoint(ENDPOINT)
                .build();

            TempatAPI api = adapter.create(TempatAPI.class);
            api.getAllData(new Callback<List<Tempat>>() {


                //here the problem 
                @Override
                public void success(List<Tempat> data, Response arg1) {
                    updateData(data);
                }

                @Override
                public void failure(RetrofitError arg0) {

                    Toast.makeText(MainActivity.this, "Retrofit Error!", Toast.LENGTH_LONG).show();
                    Toast.makeText(MainActivity.this, arg0+"", Toast.LENGTH_LONG).show();   
                }
            });
        }

        //the tmpt list is contain all Tempat's data
        //code below is to get some tmpt's data into new Arraylist
        //and update the adapter's data
        //it's work but seems bad way

        private void updateData(List<Tempat> tmpt){
            tempatList = new ArrayList<Tempat>(); 
            start+=10;
            for(int i=start-10;(i<start&&start<tmpt.size());i++)
            tempatList.add(tmpt.get(i));
            lv.addNewData(tempatList);
        }

        protected boolean isOnline() {
            ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isConnectedOrConnecting()) {
                return true;
            } else {
                return false;
            }
        }
   }

Model Tempat.java

import java.math.BigInteger;
import android.graphics.Bitmap;

public class Tempat {

    BigInteger idkoleksi;
    String nama;
    String kategori;
    String alamat;
    String notelp;
    String jambuka;
    String jamtutup;
    String harga;
    String imageHeaderURL;
    double latitude;
    double longitude;
    int jumlahview;
    int jumlatrate;
    double rating;
    String tanggal;
    Bitmap bitmap;

    public BigInteger getIdkoleksi() {
        return idkoleksi;
    }
    public String getNama() {
        return nama;
    }
    public String getKategori() {
        return kategori;
    }
    public String getAlamat() {
        return alamat;
    }
    public String getNotelp() {
        return notelp;
    }
    public String getJambuka() {
        return jambuka;
    }
    public String getJamtutup() {
        return jamtutup;
    }
    public String getHarga() {
        return harga;
    }
    public String getImageHeaderURL() {
        return imageHeaderURL;
    }
    public double getLatitude() {
        return latitude;
    }
    public double getLongitude() {
        return longitude;
    }
    public int getJumlahview() {
        return jumlahview;
    }
    public int getJumlatrate() {
        return jumlatrate;
    }
    public double getRating() {
        return rating;
    }
    public String getTanggal() {
        return tanggal;
    }
    public void setIdkoleksi(BigInteger idkoleksi) {
        this.idkoleksi = idkoleksi;
    }
    public void setNama(String nama) {
        this.nama = nama;
    }
    public void setKategori(String kategori) {
        this.kategori = kategori;
    }
    public void setAlamat(String alamat) {
        this.alamat = alamat;
    }
    public void setNotelp(String notelp) {
        this.notelp = notelp;
    }
    public void setJambuka(String jambuka) {
        this.jambuka = jambuka;
    }
    public void setJamtutup(String jamtutup) {
        this.jamtutup = jamtutup;
    }
    public void setHarga(String harga) {
        this.harga = harga;
    }
    public void setImageHeaderURL(String imageHeaderURL) {
        this.imageHeaderURL = imageHeaderURL;
    }
    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }
    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }
    public void setJumlahview(int jumlahview) {
        this.jumlahview = jumlahview;
    }
    public void setJumlatrate(int jumlatrate) {
        this.jumlatrate = jumlatrate;
    }
    public void setRating(double rating) {
        this.rating = rating;
    }
    public void setTanggal(String tanggal) {
        this.tanggal = tanggal;
    }
    public Bitmap getBitmap() {
        return bitmap;
    }
    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }





}

存储库TempatAPI.java

import java.util.List;

import retrofit.Callback;
import retrofit.http.GET;

public interface TempatAPI {
    @GET("api/tempat/get_all_data.php")
    public void getAllData(Callback<List<Tempat>> response);
}

Webservice API get_all_data.php

<?php

$response = array();
require_once __DIR__ . '/db_connect.php';

$db = new DB_CONNECT();
$nama=$_GET["nama"];

$query = "SELECT DataKoleksi.* , DataStatistikKoleksi.* FROM DataKoleksi, DataStatistikKoleksi WHERE DataKoleksi.idkoleksi = DataStatistikKoleksi.idkoleksi";
$result = mysql_query($query) or die(mysql_error());
$response = array();

if (mysql_num_rows($result) > 0) {



    while ($row = mysql_fetch_array($result)) {

        $koleksi = array();
        $koleksi["idkoleksi"] = $row["idkoleksi"];
        $koleksi["nama"] = $row["nama"];
        $koleksi["kategori"] = $row["kategori"];
        $koleksi["alamat"] = $row["alamat"];
        $koleksi["notelp"] = $row["notelp"];
        $koleksi["jambuka"] = $row["jambuka"];
        $koleksi["jamtutup"] = $row["jamtutup"];
        $koleksi["harga"] = $row["harga"];
        $koleksi["imageHeaderURL"] = $row["imageHeaderURL"];
        $koleksi["latitude"] = $row["latitude"];
        $koleksi["longitude"] = $row["longitude"];

        $koleksi["jumlahview"] = $row["jumlahview"];
        $koleksi["jumlahrate"] = $row["jumlahrate"];
        $koleksi["rating"] = $row["rating"];
        $koleksi["tanggal"] = $row["tanggal"];

        array_push($response, $koleksi);
    }
    echo json_encode($response);
} else {
    // no found
    echo json_encode($response);
}
?>

1 个答案:

答案 0 :(得分:0)

您需要向列表视图添加一个侦听器,以查看是否已滚动到列表的末尾。在下面的示例中,我们检查是否已到达列表的末尾,如果有,我们会尝试再加载10个项目。然后更新列表视图。

lvEvents.setOnScrollListener(new AbsListView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(AbsListView absListView, int scrollState) {}

    @Override
    public void onScroll(AbsListView absListView, int firstVisible, int visibleCount, int totalCount) {
        boolean loadMore = firstVisible + visibleCount >= totalCount;

        if (loadMore && !isLoadingEvents && futureEvents.size() >= 10 && !noMoreEvents) {
            Log.d(TAG, "LOADING MORE EVENTS");
            isLoadingEvents = true;
            networkManager.getTenMoreEvents(getActivity(), new NetworkManager.Request() {
                @Override
                public void finished() {
                    updateEventsFeed();
                    isLoadingEvents = false;
                }
            });
        }
    }

});