public class ProductsAll extends AppCompatActivity {
ListView prdctlist;
int count=0;
//Integer[] productimage={R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,R.drawable.img6,R.drawable.img7};
Integer[] productimage={R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher};
FirebaseFirestore ff;
CollectionReference cref;
DocumentReference dref;
ArrayList<Product> product;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_products_all);
prdctlist=findViewById(R.id.productlist);
product=new ArrayList<>();
ff=FirebaseFirestore.getInstance();
cref=ff.collection("Products");
cref.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
String pdesc = document.getString("pdesc");
String pname = document.getString("pname");
String pprice = document.getString("pprice");
String pstock = document.getString("pstock");
Product p=new Product(productimage[count++],pname,pdesc,pprice,pstock);
product.add(p);
Log.d("MyTag", pdesc + " / " + pname + " / " + pprice + " / " + pstock);
}
MyProductAdapter ma = new MyProductAdapter(ProductsAll.this,product);
prdctlist.setAdapter(ma);
}
}
});
}
}
public class Product {
String pname,pdesc,pprice,pquant;
Integer pimage;
public Product(Integer pimage,String pname, String pdesc, String pprice, String pquant) {
this.pname = pname;
this.pimage=pimage;
this.pdesc = pdesc;
this.pprice = pprice;
this.pquant = pquant;
}
public Integer getPimage() {
return pimage;
}
public void setPimage(Integer pimage) {
this.pimage = pimage;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getPdesc() {
return pdesc;
}
public void setPdesc(String pdesc) {
this.pdesc = pdesc;
}
public String getPprice() {
return pprice;
}
public void setPprice(String pprice) {
this.pprice = pprice;
}
public String getPquant() {
return pquant;
}
public void setPquant(String pquant) {
this.pquant = pquant;
}
}
public class MyProductAdapter extends BaseAdapter {
Integer pimage;
Context con;
ImageView prdctimg;
LayoutInflater prdctinflater;
View prdctview;
TextView prdctname,prdctdesc,prdctprice,prdctquant;
ArrayList<Product> product;
@Override
public int getCount() {
return product.size();
}
@Override
public Object getItem(int i) {
return product.get(i).getPname();
}
public MyProductAdapter(Context con,ArrayList<Product> product) {
/*this.pimage = productimage;*/
this.product=product;
this.con = con;
/*this.productname = productname;
this.productdescription = productdescription;
this.productquantity = productquantity;
this.productprice = productprice;*/
prdctinflater=LayoutInflater.from(con);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
prdctview=prdctinflater.inflate(R.layout.plist,null);
prdctname=prdctview.findViewById(R.id.pname);
prdctdesc=prdctview.findViewById(R.id.pdesc);
prdctimg=prdctview.findViewById(R.id.ppic);
prdctprice=prdctview.findViewById(R.id.pprice);
prdctquant=prdctview.findViewById(R.id.pquant);
prdctimg.setImageResource(product.get(i).getPimage());
prdctname.setText(product.get(i).getPname().toString());
prdctdesc.setText(product.get(i).getPdesc().toString());
prdctprice.setText(product.get(i).getPprice().toString());
prdctquant.setText(product.get(i).getPquant().toString());
return prdctview;
}
}
public void insertdb(EditText pname, String pdesc,String pprice,String pquant,int mode) //mode 1-insert 2-update 3-delete
{
this.pName=pname;
pd=pdesc;
pq=pquant;
pp=pprice;
ff=FirebaseFirestore.getInstance();
cref=ff.collection("Products");
dref=cref.document(pname.getText().toString());
if(mode==1)
{
dref.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
pName.setError("Product Name already exists");
} else {
Map<String,Object> m=new HashMap<>();
m.put("pname",pName.getText().toString());
m.put("pdesc",pd);
m.put("pprice",pp);
m.put("pstock",pq);
dref.set(m).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
aistatus.setVisibility(View.VISIBLE);
aistatus.setText("Inserted Product Details");
Toast.makeText(AdminHome.this, "Inserted Product Details", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
aistatus.setVisibility(View.VISIBLE);
aistatus.setText("Couldn't insert Product details");
}
});
}
} else {
Map<String,Object> m=new HashMap<>();
m.put("pname",pName.getText().toString());
m.put("pdesc",pd);
m.put("pprice",pp);
m.put("pstock",pq);
dref.set(m).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
aistatus.setVisibility(View.VISIBLE);
aistatus.setText("Inserted Product Details");
Toast.makeText(AdminHome.this, "Inserted Product Details", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
aistatus.setVisibility(View.VISIBLE);
aistatus.setText("Couldn't insert Product details");
}
});
Toast.makeText(AdminHome.this, "Internal Failure", Toast.LENGTH_SHORT).show();
}
}
});
}
else if(mode==2)
{
dref.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Map<String,Object> m=new HashMap<>();
if(!pd.isEmpty())
{
m.put("pdesc",pd);
}
if(!pp.isEmpty())
{
m.put("pprice",pp);
}
if(!pq.isEmpty())
{
m.put("pstock",pq);
}
dref.update(m).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
austatus.setVisibility(View.VISIBLE);
austatus.setText("Product Info Updated");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
austatus.setVisibility(View.VISIBLE);
austatus.setText("Product Info not Updated");
}
});
} else {
pName.setError("Product Name doesn't exist");
}
} else {
pName.setError("Product Name doesn't exist");
Toast.makeText(AdminHome.this, "Internal Failure", Toast.LENGTH_SHORT).show();
}
}
});
}
}
以下是更新后的日志:
08-09 17:26:49.078 17647-17647 / akshay.shoppingapplication D / MyTag:
样本/苹果/ 900/10 10-09-09 17:26:49.128
17647-17647 / akshay.shoppingapplication D / MyTag:落在Isaac上
牛顿的头让学生哭泣/苹果/ 90/5 08-09
17:26:49.164 17647-17647 / akshay.shoppingapplication D / MyTag:fghckjd /
香蕉/ 90/20 08-09 17:26:49.192
17647-17647 / akshay.shoppingapplication D / MyTag:abcd /葡萄/ 40 /
20 08-09 17:26:49.219 17647-17647 / akshay.shoppingapplication D / MyTag:
alphansos /芒果/ 120/10 08-09 17:26:49.250
17647-17647 / akshay.shoppingapplication D / MyTag:样本/香水/
5000/5 08-09 17:26:49.275 17647-17647 / akshay.shoppingapplication
D / MyTag:desc /肥皂/ 40/5
最佳答案
为了解决这个问题,只需要以下几行代码:
cref.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
String pdesc = document.getString("pdesc");
productdescription.add(pdesc);
String pname = document.getString("pname");
productname.add()pname;
String pprice = document.getString("pprice");
productprice.add(pprice);
String pstock = document.getString("pstock");
productquantity.add(pstock);
Log.d("TAG", pdesc + " / " + pname + " / " + pprice + " / " + pstock);
}
MyProductAdapter ma = new MyProductAdapter(
productimage,
ProductsAll.this,
productname,
productdescription,
productquantity,
productprice
);
prdctlist.setAdapter(ma);
}
}
});
您的logcat中的输出将是:
This feels on Isaac Newton's head and made students cry / apples / 90 / 5
//and so on
编辑:
创建一个如下所示的模型类:
public class Product {
private String pdesc, pname;
private int pprice, pstock;
public Product() { }
public Product(String pdesc, String pname, int pprice, int pstock) {
this.pdesc = pdesc;
this.pname = pname;
this.pprice = pprice;
this.pstock = pstock;
}
public String getPdesc() { return pdesc; }
public String getPname() { return pname; }
public int getPprice() { return pprice; }
public int getPstock() { return pstock; }
}
使用模型类删除实际数据并添加新数据,如下所示:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference productsRef = rootRef.collection("products");
Model model = new Model("This feels on Isaac Newton's head and made students cry", "apples", 90, 5);
productsRef.document("apples").set(model);
然后要获取数据,请使用以下代码:
cref.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<Product> productList = new ArrayList<>();
for (DocumentSnapshot document : task.getResult()) {
Product product = document.toObject(Product.class);
productList.add(product);
Log.d("TAG", product.getPdesc() + " / " + product.getPname() + " / " + product.getPprice() + " / " + product.getPstock());
}
MyProductAdapter ma = new MyProductAdapter(
productimage,
ProductsAll.this,
productList
);
prdctlist.setAdapter(ma);
}
}
});
如您所见,我只传递了一个列表,而不是传递4个列表给适配器。因此,不要忘记更改适配器类。
关于java - 列表需要花费大量时间才能检索,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51765248/