【问题标题】:Trouble populating a ListView using Firebase使用 Firebase 填充 ListView 时遇到问题
【发布时间】:2016-09-18 23:45:29
【问题描述】:

我无法使用 Firebase 填充 ListView。

没有出现错误,但存储在 Firebase 中的数据(数据正在存储到 firebase 上——我可以看到)根本没有填充 ListView。 注释部分我也试过了。

这是我认为相关的大部分代码:

    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) {

        }
    });
 }
}

这是我的代码: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;
 }
}

这是我的 socialpage2 的 XML:

<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>

这是我的 chat_item_rcv 的 XML 代码:

<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"
        />

以下是我在填充 Firebase 时在 Android 监视器上看到的内容:

【问题讨论】:

标签: android android-layout firebase firebase-realtime-database firebaseui


【解决方案1】:

首先检查您是否能够在firebase 中正确推送您的数据,并使用下面的即时推送/获取此处定义的数据..https://firebase.google.com/docs/database/android/save-data

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

现在,成功推送数据后,尝试像这样检索数据..

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); 
    }
}

然后使用这个messagesList 在你的列表中显示数据

【讨论】:

  • :对不起,但我是一个初学者,所以我知道这个问题可能听起来很愚蠢,但我想知道您的意思是什么:“首先检查您是否能够将数据推送到正确使用firebase并使用以下即时推送/获取此处定义的数据..”我去了网站,但我真的无法完全理解。所以我想知道你是否可以给我一些例子和一个简短的解释(我不想浪费你的时间)它是如何工作的以及如何去做。非常感谢 - 我是初学者,所以我很抱歉这个问题。谢谢
  • 检查您的 Firebase Web 控制台,您要显示的数据是否可用。如果它在 firebase 服务器上,那么创建如上所示的数据库实例以获取数据并相应地使用它,我相信你很清楚。谢谢。
  • 我收到了这个错误——我完全复制了你给我的错误:com.fasterxml.jackson.databind.JsonMappingException: 无法实例化类型的值 [简单类型,com.example 类。// /.firebaselearn.ChatMessage] 来自字符串值;没有单字符串构造函数/工厂方法
  • 错误出现在 :ChatMessage mChat= eventSnapshot.getValue(ChatMessage.class);
  • 我去掉了 for 循环,错误就消失了。然后我注意到“日志”,我看到程序能够从 firebase 接收数据,但现在用数据填充列表视图是问题。
【解决方案2】:

我发现了错误: 我调用了错误的 ListView。

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

但这应该是这样的:

**ListView messageList=getListView();**

【讨论】:

    猜你喜欢
    • 2015-06-16
    • 1970-01-01
    • 2020-02-23
    • 2022-01-04
    • 2013-09-28
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多