Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 限制在recyclerview中显示的项目_Php_Android_Mysql - Fatal编程技术网

Php 限制在recyclerview中显示的项目

Php 限制在recyclerview中显示的项目,php,android,mysql,Php,Android,Mysql,我在片段中使用recyclerview来显示远程MySQL数据库中的项目 当前数据库表有4个项,但recyclerview会连续重复这些项 我只想显示一次项目 这是一段代码: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.i

我在片段中使用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。不需要额外的代码。只需删除整个scrolllistenr

您就可以使用scrolllistener显示数据,这就是复制的原因data@DivyeshPatel,以及如何更改代码以仅显示一次现有项,仅在需要时滚动?您需要检查recyclerview是否滚动到底部的最后一项,然后再次使用递增页码调用api以加载更多数据,或者如果不需要,只需删除滚动侦听器并将getAddressesFromDBaddresses.getaddresses.size-1.getId_address;在recycler初始化之后在onCreateView中进行代码,然后您已经调用了该代码并填充了Recyclerview。无需额外代码。只需删除整个滚动列表