Sriharsha Singam
Sriharsha Singam

Reputation: 53

Trouble populating a ListView using Firebase

I can't populate a ListView using Firebase.

There is no error that comes but the data that is stored in Firebase (Data is being stored onto firebase-- I can see it) is not at all populating the ListView. I have tried the commented part too.

Here is most of my code I think that will be relevant:

    import android.app.ListActivity;
    import android.provider.Settings;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.net.Uri;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.firebase.client.DataSnapshot;
    import com.firebase.client.Firebase;
    import com.firebase.client.FirebaseError;
    import com.firebase.client.ValueEventListener;
    import com.google.firebase.auth.UserInfo;
    import com.google.firebase.database.ChildEventListener;
    import com.google.firebase.database.DatabaseError;
    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;

public class socialpage2 extends ListActivity {
private ChatListAdapter mChatListAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_socialpage2);
   // GlobalClass g=(GlobalClass)getApplicationContext();
   // ListView messageList=g;

     final String user=getIntent().getExtras().get("UserName").toString();
    final String[] message = new String[1];
    Button sendBtn=(Button)findViewById(R.id.btnSend);
    final EditText txt=(EditText)findViewById(R.id.txt);
    final ListView messageList=(ListView)findViewById(R.id.list);


    Firebase.setAndroidContext(this);
    final Firebase ref=new Firebase("https://---.firebaseio.com/");


    sendBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            message[0] = txt.getText().toString();
            ChatMessage chat=new ChatMessage(user, message[0]);
               ref.push().setValue(chat);
                txt.setText("");

        }
    });
    /*ListAdapter mListAdapter = new FirebaseListAdapter<ChatMessage>(ref, ChatMessage.class,
            R.layout.chat_item_rcv, this) {
        @Override
        protected void populateView(View v, ChatMessage ChatMessage) {
            ((TextView)v.findViewById(R.id.lbl3)).setText(ChatMessage.getName());
            ((TextView)v.findViewById(R.id.lbl1)).setText(ChatMessage.getMessage());
        }
    };
    if(messageList!=null) {
        messageList.setAdapter(mListAdapter);
        //g.setContent(messageList);
        setContentView(messageList);
        }*/

    // Tell our list adapter that we only want 50 messages at a time
   /* mChatListAdapter = new ChatListAdapter(ref, this, R.layout.chat_item_rcv);
    if(messageList!=null) {
        //messageList.setAdapter(mListAdapter);
        //g.setContent(messageList);
        messageList.setAdapter(mChatListAdapter);
        setContentView(messageList);
    }*/
   // messageList.setAdapter(mChatListAdapter);

    final List<ChatMessage> messages=new LinkedList<>();
    final ArrayAdapter<ChatMessage> adapter=new ArrayAdapter<ChatMessage>(
            this, android.R.layout.two_line_list_item,messages
    ){
        @Override
        public View getView(int position,View view,ViewGroup parent)
        {
            if(view==null)
            {
                view=getLayoutInflater().inflate(android.R.layout.two_line_list_item, parent, false);
            }
            ChatMessage chat=messages.get(position);
            ((TextView)view.findViewById(R.id.lbl3)).setText(chat.getName());
            ((TextView)view.findViewById(R.id.lbl1)).setText(chat.getMessage());


            return view;
        }
    };
    if(messageList!=null)
    {
        messageList.setAdapter(adapter);
    }
    ref.addChildEventListener(new com.firebase.client.ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            ChatMessage chat=dataSnapshot.getValue(ChatMessage.class);
            messages.add(chat);
            adapter.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) {

        }
    });
 }
}

Here is my code : ChatMessage:

public class ChatMessage {

private String name,message;
public ChatMessage(){

}
public ChatMessage(String name, String message)
{
      this.name=name;
    this.message=message;
}
public String getName()
{
    return name;
}

public String getMessage() {
    return message;
 }
}

Here is my XML for socialpage2:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white">

<ListView
    android:id="@+id/android:list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:divider="@color/black"
    android:dividerHeight="@dimen/pad_5dp"
    android:fastScrollEnabled="true"
    android:paddingBottom="@dimen/pad_10dp"
    android:paddingTop="@dimen/pad_10dp"
    tools:listitem="@layout/chat_item_rcv" >
</ListView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/gray_light"
    android:gravity="center_vertical"
    android:padding="@dimen/pad_5dp"
    tools:context=".MainActivity" >


    <EditText
        android:id="@+id/txt"
        style="@style/edittext_msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="Type Here" >


    </EditText>

    <Button
        android:id="@+id/btnSend"
        style="@style/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_send" />

       </LinearLayout>

 </LinearLayout>

Here is my XML code for chat_item_rcv:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llaa"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
>

<LinearLayout
        android:id="@+id/v1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="@dimen/pad_10dp"
        android:background="@drawable/chat_bubble_gray"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:paddingBottom="@dimen/pad_5dp"
        android:paddingLeft="@dimen/pad_chat_item"
        android:paddingTop="@dimen/pad_5dp" >

        <TextView
            android:id="@+id/lbl1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="@color/main_color_gray_dk"
            android:textSize="@dimen/txt_13sp" />
        <TextView
            android:id="@+id/lbl2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="text"
            android:textColor="@color/main_color_gray_dk"
            android:textSize="@dimen/txt_14sp" 
            android:maxLines="4"/>

    </LinearLayout>

    <TextView
        android:id="@+id/lbl3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text"
        android:textColor="@color/main_color_gray"
        android:textSize="@dimen/txt_14sp"
        android:layout_alignRight="@+id/v1"
        android:layout_below="@+id/v1"
        android:layout_marginTop="@dimen/pad_5dp"
        android:layout_marginLeft="@dimen/pad_20dp"
        />

Here is what I get on my Android Monitor whenever I fill Firebase: Android Monitor

Upvotes: 2

Views: 595

Answers (2)

Sriharsha Singam
Sriharsha Singam

Reputation: 53

I found the mistake: I was calling the wrong ListView.

*final ListView messageList=(ListView)findViewById(R.id.list);*

but this should have been this:

**ListView messageList=getListView();**

Upvotes: 1

Uttam Panchasara
Uttam Panchasara

Reputation: 5865

First of all check that you are able push your data in firebase properly and use below instant to push/get data as define here.. https://firebase.google.com/docs/database/android/save-data

//Database instance ref. to push and get data
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();

Now, After successfully push data, try to retrieve data like this..

List<yourModel> mDataList = new ArrayList<>();

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

    for (DataSnapshot eventSnapshot : dataSnapshot.getChildren()) {

        // Here yourModel/Data class
        yourModel mChat= eventSnapshot.getValue(yourModel.class);


        Log.e("NAME " ,""+ mChat.getName());   
        Log.e("MESSAGE " ,""+ mChat.getMessage());  

        //Add Data to d List
        mDataList.add(mChat); 
    }
}

And then use this messagesList to show data in your List

Upvotes: 1

Related Questions