2

Firebaseを使用してリストビューを作成できません。Firebaseを使用してリストビューを作成する際の問題

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 Monitor

+0

uがレガシーコードを使用して再ようです。新しいfirebaseのドキュメントを参照してくださいhttps://firebase.google.com/docs/android/setup – uguboz

答えて

1

にデータを表示するには、このmessagesリストを使用します。 を私が間違ってリストビューを呼んでいました。

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

が、これは、このされている必要があります:

**ListView messageList=getListView();** 
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); 
    } 
} 
そして、私は間違いを発見し、あなたのリスト

+0

:私は申し訳ありませんが、私は初心者のようなので、この質問は愚かなかもしれない知っているが、私はあなたが何を意味したのだろうかと思っていた: " 私はウェブサイトに行ったが、私は完全にそれを理解することができませんでした。だから私は、あなたが私にいくつかの例を与えることができるかどうか、それがどのように動作し、どのように動くかについての簡単な説明(私はあなたの時間を無駄にしたくない) これは非常に感謝しています。私は初心者ですので、質問は申し訳ありません。 ありがとう – harsh

+0

表示したいデータがあるかどうか、Firebase Webコンソールを確認してください。それがfirebaseサーバにある場合は、上記のようにデータベースインスタンスを作成してデータを取得し、それに応じて使用すると、私はそれがあなたには明らかだと信じています。ありがとう。 –

+0

このエラーが発生しました。あなたが私に与えたものを正確にコピーしました エラー:com.fasterxml.jackson.databind.JsonMappingException:[simple type、class com.example.///.firebaselearn]の値をインスタンス化できません。ChatMessage]から文字列値;単一の文字列コンストラクタ/ファクトリメソッドはありません – harsh

関連する問題