gpt4 book ai didi

java - 检索列表需要花费大量时间

转载 作者:行者123 更新时间:2023-11-29 18:46:16 25 4
gpt4 key购买 nike

enter image description here

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();
}
}
});
}
}

更新后的logcat如下:

08-09 17:26:49.078 17647-17647/akshay.shoppingapplication D/MyTag: Sample / apples / 900 / 10 08-09 17:26:49.128 17647-17647/akshay.shoppingapplication D/MyTag: This fell on Isaac Newton's head and made students cry / apples / 90 / 5 08-09 17:26:49.164 17647-17647/akshay.shoppingapplication D/MyTag:fghckjd / banana / 90 / 20 08-09 17:26:49.192 17647-17647/akshay.shoppingapplication D/MyTag: abcd / grapes / 40 / 20 08-09 17:26:49.219 17647-17647/akshay.shoppingapplication D/MyTag: alphansos / mangoes / 120 / 10 08-09 17:26:49.250 17647-17647/akshay.shoppingapplication D/MyTag: Sample / perfume / 5000 / 5 08-09 17:26:49.275 17647-17647/akshay.shoppingapplication D/MyTag: desc / soap / 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com