1

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.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
BlushyBuns
  • 13
  • 3
  • @PeterHaddad thank you dude it worked , It was just missing exactly what the dude commented there – BlushyBuns Dec 10 '17 at 16:07
  • thats me there :p anw regarding your second question `firebaselistadapter` does not work with gridview only with listview and great that everything worked! – Peter Haddad Dec 10 '17 at 16:08
  • ah I see, i'll have to create custom adapter for gridview, anyway thank you for your help! – BlushyBuns Dec 10 '17 at 16:14
  • @PeterHaddad I actually tested Firebse Listadpater with gridview, it does work! :D – BlushyBuns Dec 10 '17 at 19:57
  • ohh that's great then! :D – Peter Haddad Dec 10 '17 at 20:01
  • @PeterHaddad Sorry if I'm bothering you, but how to use firebaselistoption and firebaselistadapter in fragment? because the code above doesn't work in fragment, tried searching but no information about how to use firebase ui version 3.1.0 in fragment with firebaselistoption.. thank you – BlushyBuns Dec 11 '17 at 11:26
  • ok, the problem wasn't actually fragment it was mis-spelling , the same code firebaselistoption work fine with fragment, thank you :) – BlushyBuns Dec 11 '17 at 22:25
  • yes, thats great! For any further question its better to ask, so people here can see the code and help you in a better way – Peter Haddad Dec 11 '17 at 22:26
  • I'm glad you found a solution to your problem. However, an actual answer/solution should **not** be edited into your Question. In general, you should [edit] the Question to *clarify the Question*, but not to include an Answer within the Question. You should create your own Answer with the code you used to solve your problem, then accept it (the system may require a 48 hour delay prior to accepting your own answer). When you have solved the problem yourself, [answering your own question is encouraged](//stackoverflow.com/help/self-answer). – Makyen Dec 11 '17 at 23:21
  • oh my bad, I already accepted it as a duplicate so I can't fix that now. I'll consider that in future posts. Thank you for the clarification! :) – BlushyBuns Dec 11 '17 at 23:56

0 Answers0