I'm trying to populate my listview with text and images that stored in Firebase Real-time Database. I've been through many tutorials, I've checked many questions and answers on stack overflow but still, I'm not able to see any output and there is no error showing. Here's my code:
UPDATE: code is working after addeding onStart and onStop methods.
1- My Java class
public class MainAcitivty_2 extends AppCompatActivity {
private EditText nameeditText,urleditText;
private Button btnsave;
private ListView listView;
private DatabaseReference myRef;
private FirebaseListAdapter<Sub_Category_Data> adapter ;
ArrayList<Sub_Category_Data> subData= new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
myRef=FirebaseDatabase.getInstance().getReference("rose").child("two");
listView=(ListView)findViewById(R.id.listview);
FirebaseListOptions<Sub_Category_Data> options = new FirebaseListOptions.Builder<Sub_Category_Data>()
.setQuery(myRef, Sub_Category_Data.class).setLayout(R.layout.listview_layout).build();
adapter = new FirebaseListAdapter<Sub_Category_Data>(options) {
@Override
protected void populateView(View v, Sub_Category_Data model, int position) {
// Get references to the views of listview_layout.xml
TextView name = v.findViewById(R.id.nameTxt);
ImageView url = v.findViewById(R.id.img);
// Set their data
name.setText(model.getName());
Picasso.with(MainAcitivty_2.this).load(model.getUrl()).into(url);
}
};
listView.setAdapter(adapter);
}
// adding this made it work
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
2- My data model:
public class Sub_Category_Data {
private String name;
private String url;
public Sub_Category_Data() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
3- my listview layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listview"
/>
</LinearLayout>
4- my row layout (each list view item):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/img"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name"
android:id="@+id/nameTxt"
android:padding="10dp"
android:textColor="@color/colorAccent"
android:layout_toRightOf="@+id/img"
android:layout_marginLeft="20dp"
/>
</RelativeLayout>
5- my Gradle:
//image load picasso
compile 'com.squareup.picasso:picasso:2.5.2'
// firebase stuff
compile 'com.firebase:firebase-client-android:2.5.2'
compile 'com.google.firebase:firebase-core:11.4.2'
compile 'com.google.firebase:firebase-database:11.4.2'
compile 'com.firebaseui:firebase-ui:3.1.0'
//Circular ListView Images
compile 'de.hdodenhof:circleimageview:2.1.0'
6- Snapshot of Firebase database Databse sample
Please let me know where did I went wrong, what's the reason I'm not able to see the output? Also another question can I populate gridview using the same way?
Thank you alot in advance.