简体   繁体   中英

Unable to retrieve data from firebase into list view

I am unable to retrieve real time data from firebase, i followed many sites but nothing solves my problem. They were mostly on recycler view, But i want to retrieve my data onto a list view.. Ders no error but my list view is blank when i am running it in emulator. Moreover i cannot use firebase ui because it creates some problems in card view dependency used in my project.. Please help--

package com.serverphone.healthyfoods;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.ListView;

import com.firebase.client.ChildEventListener;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;

import java.util.ArrayList;

public class AlaCarte extends AppCompatActivity {

    ListView lv;
    String arr[]=new String[]{"Paneer Butter Masala","Chicken Butter Masala","Chicken Biriyani"};
    ArrayList al=new ArrayList();
    ArrayList food=new ArrayList();
    Button btn;


    Firebase mref;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ala_carte);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Alacarte!!");

        mref=new Firebase("https://healthy-foods-270de.firebaseio.com/Food items");

        lv=(ListView)findViewById(R.id.listView);
        btn=(Button)findViewById(R.id.button8);
        final ArrayAdapter<String> aa=new ArrayAdapter<String>(AlaCarte.this,android.R.layout.simple_list_item_multiple_choice,food);
        lv.setAdapter(aa);
        mref.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                //String key=dataSnapshot.getKey();
                //String value=dataSnapshot.getValue(String.class);
                //String item=key+"-->"+value;
                String value=dataSnapshot.getValue(String.class);
                food.add(value);
                aa.notifyDataSetChanged();

            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                CheckedTextView checkedTextView = ((CheckedTextView) view);
                //checkedTextView.setChecked(!checkedTextView.isChecked());
                if (checkedTextView.isChecked()) {
                    checkedTextView.setChecked(false);
                    al.remove(adapterView.getItemAtPosition(i));
                } else {
                    checkedTextView.setChecked(true);
                    al.add(adapterView.getItemAtPosition(i));
                }

            }
        });
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (al.isEmpty()) {
                    Snackbar.make(view, "Select check boxes to place your order", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                } else {
                    Intent i = new Intent(AlaCarte.this, ConfirmAlacarte.class);
                    i.putExtra("mylist",al);
                    startActivity(i);

                }
            }
        });

        /*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });*/
    }

}

I've been there. I did not go through your whole code, but if you get the data from firebase and add it to your list, you need something like "refresh".

What i've done was;

I added a button on the bottom of my page and when i clicked it restart the intent.

Pseudo will be like ;

public void Refresh(View view){          //refresh is onClick name given to the button

    onRestart();
}


@Override
protected void onRestart() {

    // TODO Auto-generated method stub
    super.onRestart();
    Intent i = new Intent("YourClass".this, "YourClass".class);  //your class
    startActivity(i);
    finish();

}

This solved my issue, where i got the data but did not display in my listView.

You could also test it with ToastMessage if your "food" array is null or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM