限制要在recyclerview中显示的项目

时间:2017-05-17 07:48:59

标签: php android mysql

我在片段中使用recyclelerview来显示来自远程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);

 ?>

1 个答案:

答案 0 :(得分:1)

您已经在调用它并填写Recyclerview.no需要额外的代码。只需删除整个scrolllistenr