gpt4 book ai didi

php - 限制在回收 View 中显示的项目

转载 作者:行者123 更新时间:2023-11-29 10:49:21 24 4
gpt4 key购买 nike

我在 fragment 中使用 recyclerview 来显示远程 MySQL 数据库中的项目。

目前数据库表有4个项目,但recyclerview不断重复这些项目。

我只想将这些项目显示一次。

这是一段代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_addresses, container, false);


//getData();



recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
addresses = new ArrayList<>();
getAddressesFromDB(0);

gridLayout = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(gridLayout);

adapter = new AddressesAdapter(getActivity(), addresses);
recyclerView.setAdapter(adapter);

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
Log.d("HOLA ADDRESSES", "NUM ADDRESSES: " + addresses.size());
if (gridLayout.findLastCompletelyVisibleItemPosition() == addresses.size() - 1) {
getAddressesFromDB(addresses.get(addresses.size() - 1).getId_address());
}

}
});




return view;
}
private void getAddressesFromDB(int id) {

AsyncTask<Integer, Void, Void> asyncTask = new AsyncTask<Integer, Void, Void>() {
@Override
protected Void doInBackground(Integer... addressesIds) {

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://xxxx/android_addresses_api/direcciones_todas.php?id=" + addressesIds[0])
.build();
try {
okhttp3.Response response = client.newCall(request).execute();

JSONArray array = new JSONArray(response.body().string());

for (int i = 0; i < array.length(); i++) {

JSONObject object = array.getJSONObject(i);
Log.d("HOLA ADDRESSES", "DIRECCION LEIDA: " + i);
Address address = new Address(object.getInt("id_address"), object.getString("cia"),
object.getString("fn"), object.getString("ln"),
object.getString("ad1"),object.getString("ad2"),object.getString("type"),
object.getString("city"),object.getString("state"),object.getString("zip"),
object.getString("phone"),object.getString("ext"),object.getInt("fromto"),
object.getString("user"));

addresses.add(address);
}


} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
adapter.notifyDataSetChanged();
}
};

asyncTask.execute(id);
}

这是 PHP 文件。

<?php 
//Creating a connection
$con = mysqli_connect("X","X","X","X");

if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
/*Get the id of the last visible item in the RecyclerView from the request and store it in a variable. For the first request id will be zero.*/
$id = $_GET["id"];

$sql= "Select * from tb_direcciones where id_address between ($id+1) and ($id+10)";

$result = mysqli_query($con ,$sql);

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

$array[] = $row;

}
header('Content-Type:Application/json');

echo json_encode($array);

mysqli_free_result($result);

mysqli_close($con);

?>

最佳答案

您已经在调用它并填充 Recyclerview。不需要额外的代码。只需删除整个滚动监听器

关于php - 限制在回收 View 中显示的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44018674/

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