| Thành viên | Trả lời |
thinhngheo

17 bài
| 4-7-2019 10:23:50 Hiện text thông báo :
Android.App.AlertDialog.Builder dialog = new Android.App.AlertDialog.Builder(this); Android.App.AlertDialog alert = dialog.Create(); alert.SetTitle("Tieu de"); alert.SetMessage("Xin chao ban Khanh"); alert.SetButton("OK", (c, ev) => { // Ok button click task }); alert.Show();
Hiện text 3s rồi ẩn Toast.MakeText(ApplicationContext, "Xin chao Khanhnd", ToastLength.Long).Show();
|
aspnet
 Lập trình không biên giới 608 bài
| 4-7-2019 12:41:30 Hiển thị list sản phẩm :
ArrayList listProduct = new ArrayList(); listProduct.add(new Product(1, "Iphone 6", 500)); listProduct.add(new Product(2, "Iphone 7", 700)); listProduct.add(new Product(3, "Sony Abc", 800)); listProduct.add(new Product(4, "Samsung XYZ", 900)); listProduct.add(new Product(5, "SP 5", 500)); listProduct.add(new Product(6, "SP 6", 700)); listProduct.add(new Product(7, "SP 7", 800)); listProduct.add(new Product(8, "SP 8", 900));
final MyAdapter m_adapter = new MyAdapter(listProduct); ListView listViewProduct = findViewById(R.id.products); listViewProduct.setAdapter(m_adapter);
listViewProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Product product = (Product) m_adapter.getItem(position); //Làm gì đó khi chọn sản phẩm (ví dụ tạo một Activity hiện thị chi tiết, biên tập ..) Toast.makeText(MainActivity.this, product.name, Toast.LENGTH_LONG).show(); } });
MyAdapter thì viết thế này :
package com.example.ungdung; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView;
import java.util.ArrayList;
public class MyAdapter extends BaseAdapter {
final ArrayList<Product> listProduct;
MyAdapter(ArrayList<Product> listProduct) { this.listProduct = listProduct; }
@Override public int getCount() { //Trả về tổng số phần tử, nó được gọi bởi ListView return listProduct.size(); }
@Override public Object getItem(int position) { //Trả về dữ liệu ở vị trí position của Adapter, tương ứng là phần tử //có chỉ số position trong listProduct return listProduct.get(position); }
@Override public long getItemId(int position) { //Trả về một ID của phần return listProduct.get(position).productID; }
@Override public View getView(int position, View convertView, ViewGroup parent) { //convertView là View của phần tử ListView, nếu convertView != null nghĩa là //View này được sử dụng lại, chỉ việc cập nhật nội dung mới //Nếu null cần tạo mới
View viewProduct; if (convertView == null) { viewProduct = View.inflate(parent.getContext(), R.layout.product_view, null); } else viewProduct = convertView;
//Bind sữ liệu phần tử vào View Product product = (Product) getItem(position); ((TextView) viewProduct.findViewById(R.id.idproduct)).setText(String.format("ID = %d", product.productID)); ((TextView) viewProduct.findViewById(R.id.nameproduct)).setText(String.format("Tên SP : %s", product.name)); ((TextView) viewProduct.findViewById(R.id.priceproduct)).setText(String.format("Giá %d", product.price));
return viewProduct; } }
Class Product thì thế này :
package com.example.ungdung;
class Product { String name; int price; int productID;
public Product(int productID, String name, int price) { this.name = name; this.price = price; this.productID = productID; } } --- Cây sẽ cho lộc và cây sẽ cho hoa ...
|
 |