Reputation: 15
I want to get all the values from a key inside a category key.
I'm using AndroidX, Gradle 5.1.1 and I am working with this structure:
db-name
category1
unique_random_id: value
unique_random_id2: value_value
category2
unique_random_id: value
unique_random_id2: value_value
ListView lv;
FirebaseDatabase fb;
DatabaseReference dr;
ArrayList<Article> list;
ArrayAdapter<Article> adapter;
Article article;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.articlelist);
Intent intent = getIntent();
Bundle gotExtras = intent.getExtras();
final TextView articleCatShow = (TextView)findViewById(R.id.articleCatShow);
articleTopCat.setText(gotExtras.getString("cat"));
article = new Article();
lv = (ListView)findViewById(R.id.listView);
fb = FirebaseDatabase.getInstance();
dr = fb.getReference(gotExtras.getString("cat"));
list = new ArrayList<>();
adapter = new ArrayAdapter<Article>(this, R.layout.article_each, R.id.articleTxt, list);
dr.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()) {
adapter.add(ds.getValue(Article.class));
lv.setAdapter(adapter);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
Gradle Dependencies:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.firebase:firebase-analytics:17.2.0'
implementation 'com.google.firebase:firebase-core:17.2.0'
implementation 'com.google.firebase:firebase-database:19.1.0'
}
I expected that the listview had the data.
I also get this error when I run the app and press the button that will Intent with Extras Can't convert an object of type java.lang.String to type <my-hidden-package-name>
Upvotes: 0
Views: 67
Reputation: 15
Ok! I found the answer. For a database structure like this
db-name:
category:
unique_id_very_random: value
what I did is
ArrayList<String> collectValues = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot) {
collectValues.add(ds.getValue(String.class));
}
adapter = new ArrayAdapter<String>(YOURACTIVITYJAVAFILENAME.this, R.layout.value_each, R.id.valuePerTxtValue, collectValues); // change YOURACTIVITYJAVAFILENAME to your java file name of the screen where you are showing the list and create a layout and put a textvalue inside the layout (basically its like how you want each items to look like, i think) and change 'R.layout.value_each' to 'R.layout.the_new_layout_name' and 'R.id.valuePerTxtValue' to the new text values name like 'R.id.text_value_name'
lv.setAdapter(adapter);
Upvotes: 0
Reputation: 80952
Change this:
dr.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds: dataSnapshot.getChildren()) {
adapter.add(ds.getValue(Article.class));
lv.setAdapter(adapter);
}
}
into this:
dr.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
adapter.add(dataSnapshot.getValue(Article.class));
lv.setAdapter(adapter);
}
}
Removing the for
loop since when you iterate you retrieve values of type String
and not the type of your package.
Upvotes: 1