首页 > 解决方案 > 如何获取突出显示的节点并在卡片视图中显示它们?

问题描述

我正在使用地理围栏应用程序,因为我有一个名为 Location history logs 的活动,我想在单独的卡片视图中从位置日志中检索日期(2019 年 3 月 20 日)的每个数据节点和(2019 年 3 月 21 日)从单独的卡片视图中的位置日志等到当前日期,我该怎么做?这是我的firebase数据库的链接:

Firebase JSON 图像

这是我的模型类:

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) {

            }
        });
    }

}

标签: javaandroidfirebasefirebase-realtime-database

解决方案


推荐阅读