我之前已经在这里问过类似的问题了,但是它们似乎都没有帮助我,所以我在这里尝试从我的firebase数据库中检索数据:ShiftInformation/1515547726961/PatientInformatin/5512552
中的和强制转换它是 PatientActivity 中的列表视图。
public class PatientActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private ListView listview;
private List<Patient> patientList;
DatabaseReference dref;
ArrayList<String> list=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_patient);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getPatientList();
ListViewAdapter adapter=new ListViewAdapter(PatientActivity.this,R.layout.list_item,patientList);
listview.setAdapter(adapter);
}
public List<Patient> getPatientList(){
patientList = new ArrayList<>();
mAuth = FirebaseAuth.getInstance();
listview = (ListView) findViewById(R.id.myListView);
FirebaseUser user = mAuth.getCurrentUser();
final FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference shiftRef = rootRef.child("UserInformation").child("Shift").child(user.getUid()).child("Shift");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
String ushift = dataSnapshot.getValue(String.class);
DatabaseReference PatientInfo = rootRef.child("ShiftInformation/" + ushift + "/PatientInformation");
final ListViewAdapter adapter = new ListViewAdapter(PatientActivity.this, R.layout.list_item, patientList);
PatientInfo.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Patient patient = dataSnapshot.getValue(Patient.class);
String txtTitle = patient.getPatientName();
String txtLeito = patient.getPatientLeito();
list.add(new Patient(R.drawable.common_google_signin_btn_icon_dark, txtTitle, txtLeito));
adapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
list.remove(dataSnapshot.getValue(String.class));
adapter.notifyDataSetChanged();
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
shiftRef.addListenerForSingleValueEvent(eventListener);
return patientList;
}
我的 ListViewAdapter.java 如下所示:
public class ListViewAdapter extends ArrayAdapter<Patient> {
public ListViewAdapter(@NonNull Context context, int resource, @NonNull List<Patient> objects) {
super(context, resource, objects);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
if (v==null){
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item, null);
}
Patient patient = getItem(position);
ImageView img = (ImageView) v.findViewById(R.id.imageView);
TextView txtTitle = (TextView) v.findViewById(R.id.txtTitle);
TextView txtLeito = (TextView) v.findViewById(R.id.txtLeito);
//img.setImageResource(patient.getImageId());
txtTitle.setText(patient.getPatientName());
txtLeito.setText(patient.getPatientLeito());
return v;
}
我的 Patient.java 如下所示: 公共级病人{
private int imageId;
private String patientName;
private String patientLeito;
public Patient(int imageId, String patientName, String patientLeito){
this.imageId = imageId;
this.patientName = patientName;
this.patientLeito = "Leito "+patientLeito;
}
/* public int getImageId() {
return imageId;
}
public void setImageId(int imageId) {
this.imageId = imageId;
}*/
public String getPatientName() {
return patientName;
}
public void setPatientName(String patientName) {
this.patientName = patientName;
}
public String getPatientLeito() {
return patientLeito;
}
public void setPatientLeito(String patientLeito) {
this.patientLeito = patientLeito;
}
问题 编译应用程序时,我收到以下错误。由于我的病人只能归还这些字符串,我想不出如何解决这个问题!