您好我正在尝试从我的ListView中检索数据库中的唯一键但我无法做到!请帮助!
这是我的代码
public class Inventory extends AppCompatActivity {
private Button AddProduct;
private DatabaseReference database;
private ListView mList;
private ArrayList<String> names = new ArrayList<>();
private ArrayList<String> keys = new ArrayList<>();
ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inventory);
AddProduct = (Button) findViewById(R.id.AddProduct);
AddProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Inventory.this, AddProduct.class));
}
});
mList = (ListView) findViewById(R.id.listView2);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names);
mList.setAdapter(arrayAdapter);
database = FirebaseDatabase.getInstance().getReference("All Barcodes");
database.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
arrayAdapter.clear();
for(final DataSnapshot snapshot : dataSnapshot.getChildren()){
arrayAdapter.notifyDataSetChanged();
String barcode = (String) snapshot.child("barcode").getValue();
String name = (String) snapshot.child("pname").getValue();
String exp = (String) snapshot.child("expiration").getValue();
String q = (String) snapshot.child("quantity").getValue();
arrayAdapter.add("Product Name: "+name+"\nExpiry Date: "+exp+ "\nQuantity: "+q+"\nBarcode: "+ barcode );
mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
//get position IMPT CODE
String myKey = mList.getItemAtPosition(i).toString();
String bc = myKey.split("Barcode:")[1];
bc = dataSnapshot.child(bc).getKey();
Toast.makeText(Inventory.this,bc,Toast.LENGTH_SHORT).show();
// Intent intent = new Intent(Inventory.this,Edit.class);
// intent.putExtra("value",bc);
// startActivity(intent);
}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
当我点击显示相应数据的Listview时,它将在Toast中显示条形码编号,如下所示。我想要的是检索 “-KvaATA3zliSuv7S1RPe”是Toast中相应数据的关键。非常感谢任何帮助,谢谢!
更新
我现在能够检索数据的密钥。但是,无论我在listview上单击哪个数据,它都将始终显示JSON树中最后一个数据的键,即-KvaATA3zliSuv7S1RPe。任何帮助将不胜感激。
答案 0 :(得分:2)
在for循环中,将dataSnapshot
更改为snapshot
。
for(final DataSnapshot snapshot : dataSnapshot.getChildren()){
arrayAdapter.notifyDataSetChanged();
String barcode = (String) snapshot.child("barcode").getValue();
String name = (String) snapshot.child("pname").getValue();
String exp = (String) snapshot.child("expiration").getValue();
String q = (String) snapshot.child("quantity").getValue();
arrayAdapter.add("Product Name: "+name+"\nExpiry Date: "+exp+ "\nQuantity: "+q+"\nBarcode: "+ barcode );
mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
//get position IMPT CODE
String myKey = mList.getItemAtPosition(i).toString();
String bc = myKey.split("Barcode:")[1];
bc = snapshot.getKey(); // changed from dataSnapshot to snapshot
Toast.makeText(Inventory.this,bc,Toast.LENGTH_SHORT).show();
// Intent intent = new Intent(Inventory.this,Edit.class);
// intent.putExtra("value",bc);
// startActivity(intent);
}
});
}
编辑:
public class Inventory extends AppCompatActivity {
private Button AddProduct;
private DatabaseReference database;
private ListView mList;
private ArrayList<String> names = new ArrayList<>();
private ArrayList<String> keys = new ArrayList<>();
ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inventory);
AddProduct = (Button) findViewById(R.id.AddProduct);
AddProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Inventory.this, AddProduct.class));
}
});
mList = (ListView) findViewById(R.id.listView2);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names);
mList.setAdapter(arrayAdapter);
database = FirebaseDatabase.getInstance().getReference("All Barcodes");
final List<String> keys = new ArrayList<>(); // will contain list of keys corresponding to listview item
database.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
arrayAdapter.clear();
for(final DataSnapshot snapshot : dataSnapshot.getChildren()){
String barcode = (String) snapshot.child("barcode").getValue();
String name = (String) snapshot.child("pname").getValue();
String exp = (String) snapshot.child("expiration").getValue();
String q = (String) snapshot.child("quantity").getValue();
names.add("Product Name: "+name+"\nExpiry Date: "+exp+ "\nQuantity: "+q+"\nBarcode: "+ barcode );
keys.add(snapshot.getKey());
}
arrayAdapter.addAll(names);
arrayAdapter.notifyDataSetChanged();
mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
String myKey = keys.get(i);
Toast.makeText(Inventory.this, myKey ,Toast.LENGTH_SHORT).show();
// Intent intent = new Intent(Inventory.this,Edit.class);
// intent.putExtra("value",bc);
// startActivity(intent);
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}