Populate the list view using specific data from the Firebase console

how to retrieve data from firebase in listview android
firebase get list of data android
retrieve multiple data from firebase android
firebase get data by key android
how to retrieve data from firebase in recyclerview android
retrieve specific data from firebase android
firebaselistadapter
firebase update data android

I need help with displaying data from the firebase console. So the scene is that I have put some data in the console using a seperate class called "TicketSubmitted". I need to display some of the children of the data in a list view in the "MainActivity". The model class for the MainActivity is called "Ticket" and the adapter for the list view is called "TicketAdapter"

the code for the MainActivity is:

package com.android.example.ithelpdesk;
//imports

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

DrawerLayout drawer;
ActionBarDrawerToggle toggle;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mDatabaseReference;
private ChildEventListener mChildEventListener;
private FirebaseAuth mFirebaseAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;
public TicketAdapter adapter;
private final static  int RC_SIGN_IN = 2;
public String mUsername;
private static final String TAG = "MainActivity";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    mFirebaseAuth = FirebaseAuth.getInstance();
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    mDatabaseReference = FirebaseDatabase.getInstance().getReference();

    FirebaseUser user = mFirebaseAuth.getCurrentUser();
    mUsername = user.getUid();

    mAuthStateListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = mFirebaseAuth.getCurrentUser();
            if (user != null) {
                onSignedInInitialized(user.getDisplayName());
            } else {
                onSignedOutCleanup();
                startActivityForResult(
                        getInstance()
                                .createSignInIntentBuilder()
                                .setIsSmartLockEnabled(false)
                                .setAvailableProviders(Arrays.asList(
                                        new AuthUI.IdpConfig.EmailBuilder().build(),
                                        new AuthUI.IdpConfig.GoogleBuilder().build()))
                                .build(),
                        RC_SIGN_IN);
            }
        }
    };

    final ListView listView = (ListView) findViewById(R.id.lvTicket);
   final ArrayList<String> arrayOfTicket = new ArrayList<>()
    mDatabaseReference.addChildEventListener(new ChildEventListener() {
                                                 @Override
                                                 public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                                                     Ticket ticket = (Ticket)dataSnapshot.getValue(Ticket.class);
                                                     adapter.add(ticket);
                                                 }

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

                                                 }

                                                 @Override
                                                 public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

                                                 }

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

                                                 }

                                                 @Override
                                                 public void onCancelled(@NonNull DatabaseError databaseError) {

                                                 }
                                             }

    );mDatabaseReference.

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FirebaseAuth.getInstance().signOut();
        }
    });

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            Intent intent = new Intent(view.getContext(), ComplaintDetail.class);

            startActivity(intent);
        }

    });

    final Button button = (Button) findViewById(R.id.complaintbutton);
    button.setOnClickListener(new View.OnClickListener() {
                                  public void onClick(View view) {
                                      Intent intent = new Intent(MainActivity.this, TicketCategory.class);
                                      startActivity(intent);
                                  }
                              }
    );

}

@Override
protected void onPostCreate(Bundle savedInstanceState)
{
    super.onPostCreate(savedInstanceState);
    toggle.syncState();
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId())
    {
        case R.id.action_settings:
        {
            AuthUI.getInstance().signOut(this);
            return true;
        }
        default:
            return super.onOptionsItemSelected(item);
    }
}

private void detachDatabaseReadListener()
{
    if(mChildEventListener != null)
    {
        mDatabaseReference.removeEventListener(mChildEventListener);
        mChildEventListener = null;
    }
}

@Override
protected void onResume()
{
    super.onResume();
    mFirebaseAuth.addAuthStateListener(mAuthStateListener);
}

@Override
protected void onPause()
{
    super.onPause();
    mFirebaseAuth.removeAuthStateListener(mAuthStateListener);
}

private void onSignedInInitialized(String username)
{
    mUsername = username;
   // attachDatabaseReadListener();
}

private void onSignedOutCleanup()
{
    mUsername = null;

}

The model class Ticket:

public class Ticket {
public String TicketCategory;
 // public String Name;
public String Subcategory;
public String priority;
private String status;
private String comments;
private String cannedReply;


public Ticket (String TicketCategory, String Subcategory, String priority, String status, String comments, String cannedReply)
{
    this.TicketCategory = TicketCategory;
 //   this.Name = Name;
    this.Subcategory = Subcategory;
    this.priority = priority;
    this.status = status;
    this.comments = comments;
    this.cannedReply = cannedReply;
}


public void setStatus(String status) {
    this.status = status;
}

public String getTicketCategory() {
    return TicketCategory;
}

//public String getName(){return Name;}

public String getSubcategory() {
    return Subcategory;
}

public String getPriority() {
    return priority;
}

public String getComments() {
    return comments;
}

public void setComments(String comments) {
    this.comments = comments;
}

public String getCannedReply() {
    return cannedReply;
}

public void setCannedReply(String cannedReply) {
    this.cannedReply = cannedReply;
}

public String getStatus() {
   return status;

}

public static ArrayList<Ticket> getTicket()
{
    ArrayList<Ticket> tickets = new ArrayList<Ticket>();
    return tickets;
}}

The code for the adapter TicketAdapter is:

public class TicketAdapter extends ArrayAdapter<Ticket> {

TicketAdapter(Context context, ArrayList<Ticket> tickets) {
    super(context, 0, tickets);
}

@SuppressLint("SetTextI18n")
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
    Ticket ticket = getItem(position);
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.tickets, parent, false);
    }

    TextView tvCategory = (TextView) convertView.findViewById(R.id.TicketCategory);
    TextView tvSubcategory = (TextView) convertView.findViewById(R.id.Subcategory);
    TextView tvPriority = (TextView) convertView.findViewById(R.id.priority);


    assert ticket != null;
    tvCategory.setText(ticket.TicketCategory);
    tvSubcategory.setText(ticket.Subcategory);
    tvPriority.setText(ticket.priority);


    return convertView;

}}

I just want to display the TicketCategory, Subcategory and priority in a set manner in the list view but i am unable to do so. Also when I introduce a new childEventListener, i get this error and the app crashes:

Screenshot

I am new at android programming so any input will be appreciated...

thanks!!


First your log say that you need to rename your Ticket fields like (can't start with an uppercase) :

public String ticketCategory;
public String subcategory;

Then you must instanciate your adapter with your arrayOfTicket and then set the adapter to your ListView (+ don't forget to call adapter.notifyDataSetChanged() when you add/remove object in your list) :

final ListView listView = (ListView) findViewById(R.id.lvTicket);
final ArrayList<Ticket> arrayOfTicket = new ArrayList<>();
adapter = new TicketAdapter(this, arrayOfTicket); // Instanciate your adapter
listView.setAdapter(adapter); // Set the adapter to your ListView
mDatabaseReference.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        Ticket ticket = (Ticket)dataSnapshot.getValue(Ticket.class);
        adapter.add(ticket);
        adapter.notifyDataSetChanged(); // Notify for changes to reload the list
    }

    ...

firebase, Now let's create a model for the data we're going to populate our ListView with: public class Person { private String name public Person() { // Constructor required​  You can use FirebaseUI. It is very simple to populate the listView with the firebase data. Just add the dependencies then: Connect to Firebase; Create custom FirebaseListAdapter subclass; Something like:


To solve this error:

com.google.firebase.database.DatabaseException: Found two getters or fields with conflicting case sensitivity for property: subcategory

Please change the following lines of code:

public String TicketCategory;
// public String Name;
public String Subcategory;
public String priority;

to

private String TicketCategory;
// private String Name;
private  String Subcategory;
private  String priority;

And don't forget to add the no-argument constructor in your Ticket class.

public Ticket() {} //Needed for Firebase

A Firebase Realtime Database List Data Tutorial, data before working through a tutorial outlining the steps in reading and writing individual data items. In this, the final chapter covering the Firebase Realtime Database, we will build chapter by creating an example app that makes use of many of the list data features of the realtime database. Implementing the ListView. Contents in this project Show Firebase database data into RecyclerView ListView Tutorial : 1. Read our previous tutorial Inserting data into Firebase real time database. Because using this tutorial you can know how to insert data into firebase database using android application and how to configure firebase project in android studio properly. 2.


I guess that your problem is in the way that you refer to the data, you don't seem to refer to the actual node that must list the data.

lets say your node that you save the data in is called Tickets

so instead of this:

 mDatabaseReference.addChildEventListener(new ChildEventListener() {....

do this:

 DatabaseReference tickets_ref = mDatabaseReference.child("Tickets");
 tickets_ref.addChildEventListener(new ChildEventListener() {....

instead of this:

final ArrayList<String> arrayOfTicket = new ArrayList<>();

do this:

final ArrayList<Ticket> arrayOfTicket = new ArrayList<>();

now after this:

final ArrayList<Ticket> arrayOfTicket = new ArrayList<>();

initialize the adapter like this:

 adapter = new TicketAdapter(context,arrayOfTicket);

and instead of this:

adapter.add(ticket);

do this:

arrayOfTicket.add(ticket);
adapter.notifyDataSetChanged();

Work with Lists of Data on Android, Introduction. Vision. Recognize text. Overview · iOS · Android. Detect faces Note that adding Firebase to your Unity project involves tasks both in the Firebase console and Use with ChildAdded and ChildRemoved to monitor changes to lists. time data is changed at the specified database reference, including changes  Note that in addition to appearing in the ListView within the app, the items also appear in the database tree within the Firebase console: Figure 25-5 Select an item from the list and click the Delete button to remove it from the database. Verify that the item is removed from both the ListView and the tree in the Firebase console.


Retrieving Data, This is an android Firebase Realtime Database with ListView examples. How to save from edittext,retrieve that particular data and of course show in a simple listview. Fill Simple ArrayList; Bind the arraylist to ListView Head over to Firebase Console, create a Firebase App, Register your app id and  Append to a list of data. Use the push() method to append data to a list in multiuser applications. The push() method generates a unique key every time a new child is added to the specified Firebase reference. By using these auto-generated keys for each new element in the list, several clients can add children to the same location at the same time without write conflicts.


Android Firebase Realtime Database ListView – Camposha, Largest Stars App – Kotlin+MySQL+MVVM+Retrofit2 Multipart+Data Then populate a custom listview with cardviews. First you need to create a Firebase App in the online Firebae Console. Do not place your application dependencies here; they belong // in the individual module build.gradle files } }. Firebase Real time database with ListView Fine Code 5ncode is a website this provides many android tutorials with minimum code and easy way,this website useful for both bigenners and experts also


Android Firebase ListView – Write, Read, Scroll to Last Added Item , This an android firebase listview tutorial and example. firebase app in the firebase console Duration: 36:08 Posted: May 23, 2018 Today we will see how to retrieve multiple data from Firebase into a ListView with the help of a ArrayList and ArrayAdapter using Android Studio. Download Firebase Databas Example Source Code