0

I am not getting my Database name items in my autocompletetextview. I am not sure what is wrong with my code.

Here is my small firebase Database.

 [ null, { "Coordinaat" : 75,
  "Naam" : "Cream"
}, {
  "Coordinaat" : 47885,
  "Naam" : "Cacao"
}, {
  "Coordinaat" : 48456,
  "Naam" : "Cola"
}, {
  "Coordinaat" : 25164,
  "Naam" : "Yoghurt"
}, {
  "Coordinaat" : 54,
  "Naam " : "Carrot"
}, {
  "Coordinaat" : 57,
  "Naam" : "Yum"
} ]

Here is my ProductClass.

public class Product {
public String name;
public int coordinaat;

public Product()
{

}

public Product(String name, int coordinaat)
{
   this.name = name;
   this.coordinaat = coordinaat;
}

public void setcoordinaat(int coordinaat) {
    this.coordinaat = coordinaat;
}

public void setname(String name)
 {
     this.name = name;
 }

public String getname()
{
    return name;
}

public int getcoordinaat()
{
    return coordinaat;
}
}

Here is my activity code.

public class tweede extends AppCompatActivity {

       private static final String[] producten = new String[] { "Yoghurt",
        "Cream", "Cacao", "Cola", "Yummy", "Chocolate" , "Can" , "Cannabis" , "Caramel", "Carrot", "Coconut" };
DatabaseReference databaseProducten;

      //ListView lstviewProducten;
    List <Product> lstPro = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tweede);
    databaseProducten = FirebaseDatabase.getInstance().getReference();
    AutocmpleteMeth();
   // lstviewProducten = (ListView) findViewById(R.id.listView2);
}
@Override
protected void onStart() {
    super.onStart();
    databaseProducten.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            lstPro.clear();
            for(DataSnapshot productenSnapshot : dataSnapshot.getChildren())
            {
                Product product = productenSnapshot.getValue(Product.class);
                lstPro.add(product);
            }
            AutocmpleteMeth();  }

        @Override public void onCancelled(DatabaseError databaseError) {
        }
    });
}
public void AutocmpleteMeth()
{
    // Hieronder is het code voor Autocomplete [BEGIN]
    AutoCompleteTextView ACTV;
    ACTV=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView2);
    ArrayAdapter<Product> adapter = new ArrayAdapter<Product>(this, android.R.layout.simple_dropdown_item_1line, lstPro);
    ACTV.setAdapter(adapter);
    ACTV.setThreshold(1);
    //Autocmplete [END]
}

}

That what i am getting on my phone..

image

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
Smith Gta
  • 7
  • 8
  • 1
    You need to reload the adapter... Call the autocomplete method again – OneCricketeer Nov 12 '17 at 19:08
  • how can u explain me more please – Smith Gta Nov 12 '17 at 19:09
  • At the end of onDataChange. Call that method. There's no data to load inside onCreate – OneCricketeer Nov 12 '17 at 19:10
  • You've included a picture of the JSON tree in your question. Please replace that with the actual JSON as text, which you can easily get by clicking the Export JSON link in [your Firebase Database console](https://console.firebase.google.com/project/_/database/data/). Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do. – Frank van Puffelen Nov 12 '17 at 19:10
  • 1
    Same for the code: please don't share screenshots of textual content (JSON, code, error messages, etc). Instead put the actual text in your question and use the formatting tools to mark it up. – Frank van Puffelen Nov 12 '17 at 19:11
  • Cricker_007 i tried what u said but instead of Names i get something like that, that i just edited in my using a foto of emulator. – Smith Gta Nov 12 '17 at 19:15
  • Override the `toString()` method in `Product` to `return name;`. – Mike M. Nov 12 '17 at 22:01
  • How can u explain more please... i posted screenshort of my mob where u can see i am getting totally diffrent thing it reads from database but it dont show. I am new to Java. – Smith Gta Nov 12 '17 at 22:11
  • Have a look at [this answer](https://stackoverflow.com/a/27475573). In your `Product` class, add a `toString()` method like is shown for the `NewsObject` class there, but `return name;`, instead of `title`. – Mike M. Nov 12 '17 at 23:01

1 Answers1

0

What you are getting there, is the address of your Product class from the memory. You need to know that The Firebase Realtime Database SDK will serialize all visible fields and JavaBean-type getter methods on an instance of an object using reflection to discover them. So, if it sees a field "name", it will serialize a property called "name". If it sees a getter method called "getName", it will serialize a property also called "name".

The problem in your code is that your getters and setter are named incorrectly. This is the correct way in which you can use Product class:

public class Product {
    public String name;
    public int coordinaat;

    public Product() {}

    public Product(String name, int coordinaat) {
        this.name = name;
        this.coordinaat = coordinaat;
    }

    public void setCoordinaat(int coordinaat) {
        this.coordinaat = coordinaat;
    }

    public void setName(String name){
        this.name = name;
    }

    public String getName(){
        return name;
    }

    public int getCoordinaat(){
        return coordinaat;
    }

    public String toString(){
        return name;
    }
}

See that i have changed setcoordinaat in setCoordinaat, setname in setName, getname in getName and getcoordinaat in getCoordinaat.

Assuming that instead of null the name of the node is records and is a direct child of your Firebase root, to display those values, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String> list = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            long Coordinaat = ds.child("Coordinaat").getValue(Long.class);
            String Naam = ds.child("Naam").getValue(String.class);
            Log.d("TAG", Coordinaat + " / " + Naam);
            list.add(Coordinaat + " / " + Naam);
        }
        Log.d("TAG", list);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
rootRef.addListenerForSingleValueEvent(eventListener);

The output will be:

3212 / Cream
47885 / Cacao
5 / Cola
466468 / Youghurt
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/158971/discussion-on-answer-by-alex-mamo-unable-to-retrieve-data-from-firebase-to-a-lis). – Andy Nov 14 '17 at 13:51