来自Firebase的数据不会通过CardView显示在我的RecyclerView上

时间:2016-09-20 10:02:43

标签: android firebase android-recyclerview firebase-realtime-database

我尝试显示来自Firebase的数据,但它不会显示给我的recyclerView。我不知道出了什么问题。 我也试图从stackoverflow中找到一些教程或相同的问题,但我没有得到它。仍然没有显示来自Firebase的数据。

HistoryActivity.class

 public class HistoryActivity extends AppCompatActivity {

    String name, route;
    int earnings, time, distance, totalPassenger, tripCount;
    private Firebase ref;
    private RecyclerView recycler;
    private RecyclerViewAdapter adapter;
    List<History> historyList;
    History history;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        Firebase.setAndroidContext(this);

        recycler = (RecyclerView) findViewById(R.id.recyclerview);

        historyList = fillData();

        recycler.setLayoutManager(new LinearLayoutManager(this));
    }

    public List<History> fillData() {
        historyList = new ArrayList<>();

        ref = new Firebase(Config.FIREBASE_URL_HISTORY_DRIVER);
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    Toast.makeText(HistoryActivity.this, snapshot.getValue()+"", Toast.LENGTH_SHORT).show();

                    name = snapshot.child("driverName").getValue().toString().trim();
                    route = snapshot.child("destination").getValue().toString().trim();
                    earnings = Integer.valueOf(snapshot.child("earnings").getValue().toString().trim());
                    time = Integer.valueOf(snapshot.child("time").getValue().toString().trim());
                    distance = Integer.valueOf(snapshot.child("distanceTravelled").getValue().toString().trim());
                    totalPassenger = Integer.valueOf(snapshot.child("totalPassenger").getValue().toString().trim());
                    tripCount = Integer.valueOf(snapshot.child("tripCount").getValue().toString().trim());
                    historyList.add(new History(name, route, earnings, time, distance, totalPassenger, tripCount));
                }
                Toast.makeText(HistoryActivity.this, historyList.size()+":size", Toast.LENGTH_LONG).show();
                adapter = new RecyclerViewAdapter(historyList);
                recycler.setAdapter(adapter);
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
            }
        });
        return historyList;
    }
}

RecyclerViewAdapter

    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.HistoryViewHolder> {

    List<History> historyList;
    Context context;

    public RecyclerViewAdapter(List<History> historyList) {
        this.historyList = historyList;
    }

    @Override
    public HistoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //inflate the layout, initialize the View Holder
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_layout, parent, false);
        HistoryViewHolder holder = new HistoryViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerViewAdapter.HistoryViewHolder holder, int position) {
        holder.driversName.setText(historyList.get(position).getDriversName());
        holder.route.setText(historyList.get(position).getRoute());
        holder.totalEarnings.setText(historyList.get(position).getEarnings());
        holder.time.setText(historyList.get(position).getTime());
        holder.distance.setText(historyList.get(position).getDistance());
        holder.totalPassenger.setText(historyList.get(position).getTotalPassenger());
        holder.tripCount.setText(historyList.get(position).getTripCount());
        animate(holder);
    }

    @Override
    public int getItemCount() {
        return historyList.size();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    public void animate(RecyclerView.ViewHolder viewHolder) {
        final Animation animAnticipateOvershoot = AnimationUtils.loadAnimation(context, R.anim.bounce_interpolator);
        viewHolder.itemView.setAnimation(animAnticipateOvershoot);
    }

    public static class HistoryViewHolder extends RecyclerView.ViewHolder {
        CardView cardView;
        EditText driversName;
        EditText route;
        EditText totalEarnings;
        EditText time;
        EditText distance;
        EditText totalPassenger;
        EditText tripCount;

        public HistoryViewHolder(View itemView) {
            super(itemView);
            cardView = (CardView) itemView.findViewById(R.id.cardView);
            driversName = (EditText) itemView.findViewById(R.id.edtDriversName);
            route = (EditText) itemView.findViewById(R.id.edtRoute);
            totalEarnings = (EditText) itemView.findViewById(R.id.edtEarning);
            time = (EditText) itemView.findViewById(R.id.edtTime);
            distance = (EditText) itemView.findViewById(R.id.edtDistance);
            totalPassenger = (EditText) itemView.findViewById(R.id.edtTotalPass);
            tripCount = (EditText) itemView.findViewById(R.id.edtTripCount);
        }
    }
}

历史(POJO)

public class History {

    private String username;
    private String driversName;
    private String route;
    private int earnings;
    private int time;
    private int distance;
    private int totalPassenger;
    private int tripCount;

    public History(String driversName, String route, int earnings, int time, int distance, int totalPassenger, int tripCount) {
        this.driversName = driversName;
        this.route = route;
        this.earnings = earnings;
        this.time = time;
        this.distance = distance;
        this.totalPassenger = totalPassenger;
        this.tripCount = tripCount;
    }

1 个答案:

答案 0 :(得分:0)

您需要在设置适配器之前设置布局,如下所示:

 recycler = (RecyclerView) findViewById(R.id.recyclerview);
 recycler.setLayoutManager(new LinearLayoutManager(this));
 historyList = fillData();