gpt4 book ai didi

java - 如何使用 RecyclerView 从 firebase 获取 key

转载 作者:行者123 更新时间:2023-12-02 09:57:25 25 4
gpt4 key购买 nike

我可以获取 key 并在 Log.d 中显示,但它不会在 RecyclerView 中显示。这是怎么回事?

HistoryActivity.java 它包含我的 recyclerView:

public class HistoryActivity extends AppCompatActivity {

FirebaseFirestore mFirestore;
FirebaseAuth firebaseAuth;
FirebaseDatabase database;

RecyclerView mHisList;
ArrayList<PreviousLst> history;
adt_rv_HisList adtRvHisList;

TextView ptName;
TextView ptPort;

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

getSupportActionBar().setSubtitle("History");

Intent it = getIntent();
String patName = it.getStringExtra("nm");
String patID = it.getStringExtra("idpat");
String portNum = it.getStringExtra("pNum");
String regisDate = it.getStringExtra("rdate");

ptName = findViewById(R.id.txName);
ptPort = findViewById(R.id.portpassValue);

ptName.setText(patName);
ptPort.setText(portNum);

history = new ArrayList<>();

setupRecyclerView();
setupFireBase();
loadDataFromDatabase(portNum);

}

private void setupFireBase() {

mFirestore = FirebaseFirestore.getInstance();
firebaseAuth = FirebaseAuth.getInstance();
database = FirebaseDatabase.getInstance();

}

private void setupRecyclerView() {
mHisList = findViewById(R.id.rv_prev_lst);
mHisList.setHasFixedSize(true);
mHisList.setLayoutManager(new LinearLayoutManager(this));
mHisList.setAdapter(adtRvHisList);
}

public void loadDataFromDatabase(String portNum) {
if(history.size()>0)
history.clear();

DatabaseReference myRef;
DatabaseReference passref;

switch (portNum){

case "Huang-Yai0002":
String p2 = "NETEKG-Huang-Yai0002";
myRef = database.getReference("MACHINE");
passref = myRef.child(p2).child("value");

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

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

String keydate = postSnapshot.getKey();
Log.d(TAG, "Child are: " + keydate );
PreviousLst previousLst = new PreviousLst(keydate);
history.add(previousLst);
}
adtRvHisList = new adt_rv_HisList(HistoryActivity.this, history);
mHisList.setAdapter(adtRvHisList);
}
@Override
public void onCancelled(DatabaseError error) {
Log.w(TAG, "Failed to read value.", error.toException());
}
});
break;

case "Huang-Yai0003":
String p3 = "NETEKG-Huang-Yai0003";
myRef = database.getReference("MACHINE");
passref = myRef.child(p3).child("value");

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

for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
String keydate = postSnapshot.getKey();
Log.d(TAG, "Child are: " + keydate );
PreviousLst previousLst = new PreviousLst(postSnapshot.getKey());
history.add(previousLst);
}
adtRvHisList = new adt_rv_HisList(HistoryActivity.this, history);
mHisList.setAdapter(adtRvHisList);
}
@Override
public void onCancelled(DatabaseError error) {

Log.w(TAG, "Failed to read value.", error.toException());
}
});
break;
default:
Log.d(TAG, "Value is: " + portNum);
Toast.makeText(getApplicationContext(),"Error...",Toast.LENGTH_LONG).show();
break;

}
}
}

adt_rv_HisList.java 适配器:

public class adt_rv_HisList extends RecyclerView.Adapter<adtrvHisListViewHolder> {
HistoryActivity historyActivity;
ArrayList<PreviousLst> history;


public adt_rv_HisList(HistoryActivity historyActivity, ArrayList<PreviousLst> history) {
this.history = history;
this.historyActivity = historyActivity;
}

@NonNull
@Override
public adtrvHisListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(historyActivity.getBaseContext()).inflate(R.layout.hislist_item, viewGroup, false);
return new adtrvHisListViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull adtrvHisListViewHolder holder, int position) {

holder.hdate.setText(history.get(position).getHisDate());

holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialogView();
}
});
}

private void dialogView() {
final Dialog dia = new Dialog(historyActivity);
dia.setContentView(R.layout.dialog_ask_view);
dia.show();

Button pvgraph = (Button)dia.findViewById(R.id.bt_pvgraph);
Button pvhr = (Button)dia.findViewById(R.id.bt_pvhrate);
Button cc = (Button)dia.findViewById(R.id.btn_cancel);

cc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dia.dismiss();
}
});

pvgraph.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dia.dismiss();
Intent it = new Intent(historyActivity, ViewgraphActivity.class);
historyActivity.startActivity(it);
}
});

pvhr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dia.dismiss();
Intent it = new Intent(historyActivity, ViewHRActivity.class);
historyActivity.startActivity(it);
}
});
}

@Override
public int getItemCount() {
return 0;
}
}

adtrvHisListViewHolder.java 包含 ViewHolder :


public class adtrvHisListViewHolder extends RecyclerView.ViewHolder {

public TextView hdate;

public adtrvHisListViewHolder(View itemView) {
super(itemView);

hdate = itemView.findViewById(R.id.his_date);
}
}

PreviousLst.java 是我的 recyclerView 的模型类:

public class PreviousLst {
String HisDate;

public PreviousLst(String HisDate){
this.HisDate = HisDate;
}

public PreviousLst(){

}

public String getHisDate() {
return HisDate;
}
public void setHisDate(String hisDate) {
HisDate = hisDate;
}
}

这是我获得并显示在 Log.d 中的 key

  • D/ContentValues:子项为:心率
  • D/ContentValues:子级为:LEAD 1
  • 子级是:LEAD 2
  • 子级是:LEAD 3
  • child 是:LEAD 4
  • child 是:LEAD 5
  • child 是:LEAD 6

但它不适合应用程序。 enter image description here

最佳答案

@Override
public int getItemCount() {
//return 0;
replace it with history.size();
}

添加的行告诉适配器有多少个项目,适配器必须将这些项目绑定(bind)到回收器 View 。默认为 0,表示适配器类永远不会调用 binView 方法在 View 侧显示项目

关于java - 如何使用 RecyclerView 从 firebase 获取 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55892332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com