此图片显示没有元素(活动),如何解决?我想这很简单,请帮助我。 Click Here!
这是我的主要活动。 java *请评论以查看更多文件
| Code | Name | Total | Trading Account | Count |
|11475 | SANDY| 697 | Z07640C | 422 |
| | | | Z04276B | 93 |
...
|13139 |Sran | 34 | Z32421A | 2 |
| | | | Z43487B | 24 |
...
这是我的activity_main.xml *请评论您要查看的文件
package com.example.jdev.trytry;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
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.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List<userModel> result;
private userAdapter adapter;
private FirebaseDatabase database;
private DatabaseReference reference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
database = FirebaseDatabase.getInstance();
reference = database.getReference("users");
result = new ArrayList<>();
recyclerView = (RecyclerView) findViewById(R.id.user_list);
recyclerView.setHasFixedSize(true);
LinearLayoutManager lin = new LinearLayoutManager(this);
lin.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(lin);
adapter = new userAdapter(result);
recyclerView .setAdapter(adapter);
updateList();
}
@Override
public boolean onContextItemSelected (MenuItem item){
switch (item.getItemId()){
case 0:
break;
case 1:
break;
}
return super.onContextItemSelected(item);
}
private void updateList(){
reference.addChildEventListener(new ChildEventListener(){
@Override
public void onChildAdded (DataSnapshot dataSnapshot, String a){
result.add(dataSnapshot.getValue(userModel.class));
adapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String a){
userModel model = dataSnapshot.getValue(userModel.class);
int index = getItemIndex(model);
result.set(index, model);
adapter.notifyItemChanged(index);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot){
userModel model = dataSnapshot.getValue(userModel.class);
int index = getItemIndex(model);
result.remove(index);
adapter.notifyItemRemoved(index);
}
@Override
public void onChildMoved (DataSnapshot dataSnapshot, String a){
}
@Override
public void onCancelled (DatabaseError databaseError){
}
});
}
private int getItemIndex(userModel user){
int index = -1;
for (int i = 0; i < result.size(); i++){
if(result.get(i).key.equals(user.key))
index = i;
break;
}
return index;
}
}
这是userAdapter.java
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jdev.trytry.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/user_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
如果您需要其他文件,请发表评论,谢谢!
答案 0 :(得分:-1)
如果为空则检查结果列表数组大小,则不会显示任何内容。 当您获取数据时,数据会添加到列表中,然后在定义适配器并将适配器设置为recyclerview之后。比如制作一套适配器方法
private void setAdapter() {
if (adapter == null) {
if (!result.isEmpty()) {
adapter = new userAdapter(result);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
} else {
adapter.notifyDataSetChanged();
}
}
之后调用上面的方法定义方法updateList(),并从onCreate()方法中删除下面的代码
adapter = new userAdapter(result);
recyclerView .setAdapter(adapter);
总是在您获得数据的时候将数据绑定到回收站视图。