111 lines
3.6 KiB
Java
111 lines
3.6 KiB
Java
package com.example.lookifyv2;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import android.os.Bundle;
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import androidx.recyclerview.widget.GridLayoutManager;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import org.json.JSONArray;
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import okhttp3.OkHttpClient;
|
|
import okhttp3.Request;
|
|
import okhttp3.Response;
|
|
|
|
public class Results extends AppCompatActivity {
|
|
|
|
private static final String TAG = MainActivity.class.getSimpleName();
|
|
private List<Product> products;
|
|
private RecyclerView recyclerView;
|
|
private GridLayoutManager gridLayout;
|
|
private ProductsAdapter adapter;
|
|
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_results);
|
|
|
|
recyclerView = findViewById(R.id.recyclerview);
|
|
products = new ArrayList<>();
|
|
getProductsFromDB(0);
|
|
|
|
gridLayout = new GridLayoutManager(this, 2);
|
|
recyclerView.setLayoutManager(gridLayout);
|
|
|
|
adapter = new ProductsAdapter(this, products);
|
|
recyclerView.setAdapter(adapter);
|
|
|
|
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
|
@Override
|
|
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
|
|
|
|
if (gridLayout.findLastCompletelyVisibleItemPosition() == products.size() - 1) {
|
|
getProductsFromDB(products.get(products.size() - 1).getId());
|
|
}
|
|
|
|
}
|
|
});
|
|
}
|
|
|
|
private void getProductsFromDB(int id) {
|
|
|
|
@SuppressLint("StaticFieldLeak") AsyncTask<Integer, Void, Void> asyncTask = new AsyncTask<Integer, Void, Void>() {
|
|
@Override
|
|
protected Void doInBackground(Integer... productIds) {
|
|
|
|
OkHttpClient client = new OkHttpClient();
|
|
Request request = new Request.Builder()
|
|
.url("http://192.168.8.101/products.php?id=" + productIds[0]) //???????????????????
|
|
.build();
|
|
try {
|
|
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);
|
|
|
|
Product product = new Product(object.getInt("id"), object.getString("product_code"),
|
|
object.getString("product_name"), object.getString("product_image"),
|
|
object.getString("product_price"), object.getString("product_retailer"),
|
|
object.getString("product_colour"), object.getString("product_style"),
|
|
object.getString("product_collection"),
|
|
object.getString("product_type"), object.getString("product_sex"));
|
|
|
|
Results.this.products.add(product);
|
|
}
|
|
|
|
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
} catch (JSONException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
protected void onPostExecute(Void aVoid) {
|
|
adapter.notifyDataSetChanged();
|
|
}
|
|
};
|
|
|
|
asyncTask.execute(id);
|
|
}
|
|
|
|
}
|