我正在制作一个可以维持所有销售额的应用程序。我在firebase中有一个库存数据库,其中包含所有重要信息,例如名称,价格,说明等。 我已经用所有产品的名称填充了微调器。现在,我想单击微调器中的特定商品名称,以获取相应产品的价格。 搜索了很多东西,没发现有用的东西
到目前为止,我的代码是
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
final List<String> items = new ArrayList<String>();
for (final DataSnapshot itemSnapshot: dataSnapshot.getChildren()) {
final String itemName = itemSnapshot.child("name").getValue(String.class);
items.add(itemName);
}
java.util.Collections.sort(items);
ArrayAdapter<String> itemAdapter = new ArrayAdapter<String>(Sale.this, android.R.layout.simple_spinner_item, items);
itemAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
itemSpinner.setAdapter(itemAdapter);
itemSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
答案 0 :(得分:1)
将pojo创建为-
public class Inventory
{
private String name;
private int qty;
private String price;
private String desc;
public Inventory()
{
}
public Inventory(String name, int qty, String price, String desc) {
this.name = name;
this.qty = qty;
this.price = price;
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return "Inventory [name=" + name + ", qty=" + qty + ", price=" + price
+ ", desc=" + desc + "]";
}
}
然后做-
final ArrayList<Inventory> items = new ArrayList<Inventory>();
ArrayList<String> listname = new ArrayList<String>();
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
for (final DataSnapshot itemSnapshot: dataSnapshot.getChildren()) {
Inventory inv= itemSnapshot.getValue(String.class);
listname.add(inv.getName());
items.add(inv);
}
ArrayAdapter<String> itemAdapter = new ArrayAdapter<String>(Sale.this, android.R.layout.simple_spinner_item, listname);
itemAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
itemSpinner.setAdapter(itemAdapter);
itemSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String price = items.get(i).getPrice();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
答案 1 :(得分:0)
在onItemSelected()
内部添加此元素以获得每个元素
String selected = items.getItemAtPosition(i).toString();
您还可以创建日志以查看您单击的元素
Log.e("Clicked:",""+selected);
如果要在微调器中显示用户ID,则应在onItemSelected()
中获取该用户ID,然后使用所选的用户ID对数据库进行另一个查询,并查询该产品的价格
例如,在获得所选项目后,可以说是您的用户ID
mDatabase.child("inventory").child(selected)..addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//Get your price from that element
...
所有完成的操作都应这样
itemSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String selected = items.getItemAtPosition(i).toString();
Log.e("Clicked:",""+selected);
mDatabase.child("inventory").child(selected).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//Get your price from that element
...
}
@Override
public void onNothingSelected(AdapterView<?> adapterView){
}
});
.....