Reputation: 791
I am a beginner to android studio! I am trying to retrieve the key(also known as the barcode for my app) from google firebase when I click on the respective listview. I would like to retrieve the respective keys and is facing a cannot resolve method error. eg. I click on the 1st ListView with the pname:"dick", i would like to retrieve only the key(barcode), 97, so that i can pass the key to another java activity so that i can retrieve the rest of the data in another activity. Hope i didnt get the logic wrong
Here is a pic of my database:
I have tried many different code such as :Android Firebase cannot resolve method getRef(position). Any help would be greatly appreciated! Thanks in advance
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();
mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String value = (String) mList.getItemAtPosition(i);
//Toast.makeText(Inventory.this,value,Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Inventory.this,Edit.class);
intent.putExtra("fk",value);
startActivity(intent);
}
});
database.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
arrayAdapter.clear();
for(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);
}
}
@Override
public void onChildChanged(final DataSnapshot dataSnapshot, String s) {
String value = dataSnapshot.getValue(String.class);
String key = dataSnapshot.getKey();
int index = keys.indexOf(key);
names.set(index, value);
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Update: I tried using either one of the code as shown below, but both shows cannot resolve method error! Any help would be greatly appreciated. Below are the codes i have tried out, but to no avail
Upvotes: 0
Views: 2730
Reputation: 138944
To solve this, you need to use the following line of code:
String value = (String) adapterView.getItemAtPosition(position);
To use the value in another activity you can use an Intent
like this:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("value", value);
startActivity(intent);
Upvotes: 1
Reputation: 7368
You get this error because this
references OnItemClickListener
and not your DatabaseReference
.
database.getRef().getKey()
instead of this.getRef().getKey()
should work.
Also, you might need to take a look at the API documentation as getRef()
does not take any argument.
Upvotes: 0
Reputation: 84
To get the key from Firebase Database, you ought to replace this code
String key = dataSnapshot.getKey();
and replace it with the following:
Object key= dataSnapshot.getKey();
String myKey = key.toString()
In android you need to specify an object, that will pick the key from firebase which you will have to convert using toString() method, for it to be used in android!
Upvotes: 0