BÀI 54 - ĐỔ DỮ LIỆU LỚN TỪ CSDL PHP MYADMIN VỀ ỨNG DỤNG ANDROID

  • Dữ liệu đổ về thường được đổ vào listView hoặc RecyclerView. Vì thế trước tiên cũng phải tạo một class chứa khuôn dữ liệu và một adapter để đổ dữ liệu vào listView hay RecyclerView...

  • Ví dụ mình có một CSDL chứa các thuộc tính lần lượt: ID, tên sản phẩm, giá sản phẩm, hình ảnh sản phẩm, mô tả sản phẩm và ID loại sản phẩm. Và mình sẽ sử dụng RecyclerView để hiển thị dữ liệu nên adapter sẽ hơi khác một chút so với adapter của listView.

public class adapterSanPham extends RecyclerView.Adapter<adapterSanPham.ItemHolder> {


Context context;

ArrayList<classSanPham> arrayListSanPham;


public adapterSanPham(Context context, ArrayList<classSanPham> arrayListSanPham) {

this.context = context;

this.arrayListSanPham = arrayListSanPham;

}


@NonNull

@Override

public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

@SuppressLint("InflateParams")

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_frame_sanpham, null);

return new ItemHolder(v);

}


@SuppressLint("SetTextI18n")

@Override

public void onBindViewHolder(@NonNull ItemHolder holder, int position) {

classSanPham sanPham = arrayListSanPham.get(position);

holder.tvTenSp.setText(sanPham.getTenSanPham());


DecimalFormat decimalFormat = new DecimalFormat("###,###,###");

holder.tvGiaSp.setText(decimalFormat.format(sanPham.getGiaSanPham()) + " ₫");


Picasso.get().load(sanPham.getHinhAnhSanPham())

.placeholder(R.drawable.no_image_shop)

.error(R.drawable.error_image_shop)

.into(holder.imgSanPham);

}


@Override

public int getItemCount() {

return arrayListSanPham.size();

}


public static class ItemHolder extends RecyclerView.ViewHolder{

public ImageView imgSanPham;

public TextView tvTenSp, tvGiaSp;


public ItemHolder(View itemView){

super(itemView);

imgSanPham = itemView.findViewById(R.id.imageViewSanPham);

tvTenSp = itemView.findViewById(R.id.textViewTenSanPham);

tvGiaSp = itemView.findViewById(R.id.textViewGiaSanPham);


// Sự kiện click item recryclerView

itemView.setOnClickListener(v -> {

Intent intent = new Intent(context, Activity.class);

intent.putExtra("Key", arrayListSanPham.get(getPosition()));

context.startActivity(intent);

});

}

}

}

  • Và tất nhiên bạn cũng phải thiết kế layout của RecyclerView để adapter biết đường đổ dữ liệu vào nữa.

  • Sau khi xong xuôi thì chỉ cần code để lấy dữ liệu từ PHP MyAdmin chứa tạm trong một ArrayList. Sau đó đổ vào RecyclerView là xong.

  • À bạn phải thêm 2 thư viện của Picasso và Volley vào trước khi sử dụng code bên dưới!

arrayListSanPham = new ArrayList<>();

adapterSanPham = new adapterSanPham(getApplicationContext(), arrayListSanPham);

rvMenuShop.setHasFixedSize(true);

rvMenuShop.setLayoutManager(new GridLayoutManager(getApplicationContext(), 2));

rvMenuShop.setAdapter(adapterSanPham);


RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());

@SuppressLint("NotifyDataSetChanged")

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(getServer.linkSanPham, response -> {

if (response != null) {

int ID;

String tenSp;

int giaSp;

String imgSp;

String mtaSp;

int idSp;


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

try {

JSONObject jsonObject = response.getJSONObject(i);

ID = jsonObject.getInt("ID");

giaSp = jsonObject.getInt("GiaSanPham");

tenSp = jsonObject.getString("TenSanPham");

imgSp = jsonObject.getString("HinhAnhSanPham");

mtaSp = jsonObject.getString("MoTa");

idSp = jsonObject.getInt("IDSanPham");


arrayListSanPham.add(new classSanPham(ID, tenSp, giaSp, imgSp, mtaSp, idSp));

adapterSanPham.notifyDataSetChanged();

} catch (JSONException e) {

e.printStackTrace();

}

}

}

}, error -> { });

requestQueue.add(jsonArrayRequest); // Hàm này để lấy dữ liệu là json về

  • Trong đoạn code của JsonArrayRequest bên trên có xuất hiện getServer. Đây chính là một class public mình đã tạo sẵn để truy cập vào địa chỉ của CSDL PHP MyAdmin có chứa json chúng ta cần.

public class getServer {

public static String localhost = "192.168.1.4"; // host wifi

public static String linkSanPham = "http://" + localhost + ":8080/ShopRobot/getSanPham.php";

}