它是一个简单的客户端查询聊天应用程序。我能够在firebase中插入数据并通过解析tsp页面来获取所需的数据(这是一个临时安排,有人正在制作一个将返回json数据的api)。我无法在Recycler视图中显示我的firebase数据。我能够在数据库中插入数据。在scree上只显示null / 0/0。
以下是我的活动代码:
public class VD extends AppCompatActivity {
private EditText ed;
private String content;
private RecyclerView recyclerView;
String chatId;
DatabaseReference myRef;
private FirebaseDatabase database;
private final List<Messages> mList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vd);
ed = findViewById(R.id.editText);
//gets you the contents of edit text
//
database = FirebaseDatabase.getInstance();
/*myRef = database.getReference("message");
myRef.setValue("Hello vcbv World!");*/
}
@Override
protected void onPause() {
super.onPause();
database.getReference("onlineStatus").child("status").setValue("n");
}
@Override
protected void onResume() {
super.onResume();
database.getReference("onlineStatus").child("status").setValue("y");
}
public void updateDatabase(View view) {
content = ed.getText().toString();
//Log.d("vidhu",content);
String[] cd = content.split("/");
final String caseType = cd[0];
final int caseNumber = Integer.parseInt(cd[1]);
final int caseYear = Integer.parseInt(cd[2]);
myRef = database.getReference("case_details");
chatId = myRef.push().getKey();
CaseDetails caseDetails = new CaseDetails(caseType, caseNumber, caseYear);
myRef.child(chatId).setValue(caseDetails);
myRef.child(chatId).child("status").setValue("");
VD.JsoupAsyncTask jsoupAsyncTask = new VD.JsoupAsyncTask();
jsoupAsyncTask.execute();
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Messages a = dataSnapshot.getValue(Messages.class);
mList.add(a);
}
recyclerView = findViewById(R.id.recycler_view);
MessagePopulator mAdapter = new MessagePopulator(mList);
recyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
/*myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ed.setText("");
}
}
}
以下是我的Adapter类
public class MessagePopulator extends RecyclerView.Adapter<MessagePopulator.MyViewHolder> {
private List<Messages> messageList;
public MessagePopulator(List<Messages> mList) {
this.messageList = mList;
}
public List<Messages> getMessageList() {
return messageList;
}
public void setMessageList(List<Messages> messageList) {
this.messageList = messageList;
}
/*public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).
inflate(R.layout.messageslist, parent, false);
return new MyViewHolder(itemView);
//return null;
}*/
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).
inflate(R.layout.messageslist, parent, false);
return new MyViewHolder(itemView);
//return null;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
//System.out.println("position is "+ position);
Messages msgs = messageList.get(position);
//System.out.println("vidhu data "+msgs.getCaseType()+"/"+msgs.getCaseNumber()+"/"+msgs.getCaseYear());
holder.caseDetail.setText(msgs.getCaseType()+"/"+msgs.getCaseNumber()+"/"+msgs.getCaseYear());
holder.caseStatus.setText(msgs.getStatus());
}
@Override
public int getItemCount() {
return messageList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView caseDetail, caseStatus;
public MyViewHolder(View view) {
super(view);
caseDetail = (TextView) view.findViewById(R.id.tvCaseDetail);
caseStatus = (TextView) view.findViewById(R.id.tvStatus);
}
}
}
答案 0 :(得分:0)
只需使用官方Firebase repository
中的FirebaseRecyclerAdapter即可以下是官方文档中的示例代码:
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(options) {
@Override
public ChatHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Create a new instance of the ViewHolder, in this case we are using a custom
// layout called R.layout.message for each item
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.message, parent, false);
return new ChatHolder(view);
}
@Override
protected void onBindViewHolder(ChatHolder holder, int position, Chat model) {
// Bind the Chat object to the ChatHolder
// ...
}
};