gpt4 book ai didi

java - Android,使用 Retrofit 填充无尽 ListView 数据的最佳方法?

转载 作者:太空宇宙 更新时间:2023-11-04 13:58:48 25 4
gpt4 key购买 nike

我成功地将数据填充到 ListView 中,但我的方式似乎非常糟糕,因为当我滚动 ListView 时,改造总是获得所有存储库。那么,使用 Retrofit 填充 Endless List View 数据的最佳方法是什么?谢谢...

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;
}
}
}

模型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);
}

Web 服务 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);
}
?>

最佳答案

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

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;
}
});
}
}

});

关于java - Android,使用 Retrofit 填充无尽 ListView 数据的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29528533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com