如何获取突出显示的节点并在卡片视图中显示它们?

时间:2019-03-24 19:37:05

标签: java android firebase firebase-realtime-database

我正在使用地理围栏应用程序,因为我有一个名为“位置历史记录”的活动,我想在该活动中从单独的卡视图中的位置日志中检索日期(2019年3月20日)的每个数据节点,并且(3月21日-2019)从位置登录到单独的卡视图中,依此类推直到当前日期,我该怎么办?  这是我的Firebase数据库的链接:

Firebase JSON image

这是我的Model课:

public class locationHistoryModel {

    private String Address, Key;
    private long Entered, Exited;

    public locationHistoryModel() {
    }


    public locationHistoryModel(String address_child, String key, long entered) {
        this.Address = address_child;
        Key = key;
        Entered = entered;
    }


    public String getAddress() {
        return this.Address;
    }

    public void setAddress(String address) {
        this.Address = address;
    }

    public String getKey() {
        return Key;
    }

    public void setKey(String key) {
        Key = key;
    }

    public long getEntered() {
        return Entered;
    }

    public void setEntered(long entered) {
        Entered = entered;
    }

    public long getExited() {
        return Exited;
    }

    public void setExited(long exited) {
        Exited = exited;
    }
}

这是我的适配器类:

public class LocationhistoryAdapter extends RecyclerView.Adapter<LocationhistoryAdapter.MyViewHolder>{

    private Context mctx;
    private ArrayList<locationHistoryModel> locationHistories;

    public LocationhistoryAdapter(Context context, ArrayList<locationHistoryModel> locationHistoryArrayList )
    {
        mctx = context;
        locationHistories = locationHistoryArrayList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new LocationhistoryAdapter.MyViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_location_historyadapter,viewGroup,false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        myViewHolder.Addresstxt.setText(locationHistories.get(i).getAddress());
        myViewHolder.EnterTimeTxt.setText(String.valueOf(locationHistories.get(i).getEntered()));
        myViewHolder.location.setText(locationHistories.get(i).getKey());
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder
    {
         TextView Addresstxt,EnterTimeTxt,location;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            Addresstxt = itemView.findViewById(R.id.Address_child);
            EnterTimeTxt = itemView.findViewById(R.id.Entered_timestamp);
            location = itemView.findViewById(R.id.Location_child);

        }
    }
}

这是我的MainActivity类:

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    ArrayList<locationHistoryModel> locationHistoryList;

    DatabaseReference databaseReference;
    DatabaseReference databaseReference1;
    DatabaseReference databaseReference2;
    DatabaseReference databaseReference3;
    String date;
    String location;

    locationHistoryModel historyModel;
    LocationhistoryAdapter locationAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.childsRecycler);
        locationHistoryList = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.scrollToPosition(0);
        databaseReference = FirebaseDatabase.getInstance().getReference("Child");
        databaseReference1 = databaseReference.child("+923452267601")
                .child("03326325635")
                .child("LocationLogs");

        databaseReference1.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {

                    date = dataSnapshot1.getKey();

                    System.out.println(date + "dateee keeeeeyyyyy");

                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

        databaseReference2 = databaseReference.child("+923452267601")
                .child("03326325635")
                .child("Locations");

        databaseReference2.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                    location = dataSnapshot1.getKey();
                    System.out.println(location + "Location Key");
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });


        System.out.println("03326325635");
        databaseReference3 = databaseReference.child("+923452267601")
                .child("03326325635")
                .child("LocationLogs");

        databaseReference3.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                historyModel = dataSnapshot.child(date).child(location).getValue(locationHistoryModel.class);
                historyModel.setKey(location);
                locationHistoryList.add(historyModel);
                System.out.println(historyModel.getAddress() + "haha" + historyModel.getKey() + "" + historyModel.getEntered());
                locationAdapter = new LocationhistoryAdapter(MainActivity.this, locationHistoryList);
                recyclerView.setAdapter(locationAdapter);
            }


            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

}

0 个答案:

没有答案