populateView
课程的 FirebaseListAdapter
方法的运行频率是预期的两倍,我不知道为什么。这里我把数据库Firebase:
这是处理数据模型的类
public class Chat {
String name;
String text;
String uid;
public Chat() {
}
public Chat(String name, String uid, String message) {
this.name = name;
this.text = message;
this.uid = uid;
}
public String getName() {
return name;
}
public String getUid() {
return uid;
}
public String getText() {
return text;
}
这是主要活动:
public class MainActivity extends AppCompatActivity {
FirebaseListAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView messagesView = (ListView) findViewById(R.id.messages_list);
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
mAdapter = new FirebaseListAdapter<Chat>(this, Chat.class, android.R.layout.two_line_list_item, ref) {
@Override
protected void populateView(View view, Chat chatMessage, int position) {
((TextView)view.findViewById(android.R.id.text1)).setText(chatMessage.getName());
((TextView)view.findViewById(android.R.id.text2)).setText(chatMessage.getText());
}
};
messagesView.setAdapter(mAdapter);
}