【发布时间】:2019-01-25 13:32:10
【问题描述】:
我有一个 MainActivity 片段,它有一个ListView。每个ListView 项目有三个TextViews。我制作了一个用于ListView 的自定义适配器,这个自定义适配器是我尝试在三个不同的TextView 上使用setText() 的地方。其中两个TextViews 能够更新,但问题是nameTextView.setText(singleAppt.getName()); 行。我认为问题在于 singleAppt.getName() 返回一个空值,因为 TextView 显示为空白,但我不确定它为什么为空。
自定义适配器:(问题就在这里)
public class AppointmentList extends ArrayAdapter<SingleAppointment> {
private Activity context;
List<SingleAppointment> appointments;
public AppointmentList(Activity context, List<SingleAppointment> appointments) {
super(context, R.layout.layout_appt_list, appointments);
this.context = context;
this.appointments = appointments;
}
public void updateAppointmentList(List<SingleAppointment> appointments) {
this.appointments = appointments;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.layout_appt_list, null, true);
TextView nameTextView = listViewItem.findViewById(R.id.nameTextView);
TextView addressTextView = listViewItem.findViewById(R.id.addressTextView);
TextView dateTextView = listViewItem.findViewById(R.id.dateTextView);
SingleAppointment singleAppt = appointments.get(position);
if(singleAppt != null && singleAppt.getName() != null) {
nameTextView.setText(singleAppt.getName());
}
if(singleAppt != null && singleAppt.getAddress() != null) {
addressTextView.setText(singleAppt.getAddress());
}
if(singleAppt != null && singleAppt.getDate() != null) {
dateTextView.setText(singleAppt.getDate());
}
// addressTextView.setText(singleAppt.getAddress());
// dateTextView.setText(singleAppt.getDate());
return listViewItem;
}
}
MainActivity 片段:
public class Appointments extends Fragment {
View myView;
AppointmentList appointmentAdapter;
ArrayList<SingleAppointment> appointments;
ListView appointmentList;
DatabaseReference databaseAppointments;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
databaseAppointments = FirebaseDatabase.getInstance().getReference("appointments");
myView = inflater.inflate(R.layout.appointments, container, false);
Button newApptButton = myView.findViewById(R.id.newApptButton);
newApptButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity().getApplicationContext(), EditApptActivity.class);
startActivity(intent);
}
});
appointmentList = myView.findViewById(R.id.listView);
appointments = new ArrayList<>();
appointmentAdapter = new AppointmentList(getActivity(), appointments);
appointmentList.setAdapter(appointmentAdapter);
return myView;
}
@Override
public void onStart() {
super.onStart();
databaseAppointments.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
appointments.clear();
for(DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
SingleAppointment singleAppt = postSnapshot.getValue(SingleAppointment.class);
appointments.add(singleAppt);
}
appointmentAdapter.updateAppointmentList(appointments);
appointmentAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
用户编辑TextViews的活动:
public class EditApptActivity extends AppCompatActivity {
DatabaseReference databaseAppointments;
ArrayList<SingleAppointment> appointments;
Button bookButton;
EditText clientEditText;
EditText addressEditText;
EditText dateEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_appt);
//Intent intent = getIntent();
clientEditText = findViewById(R.id.clientEditText);
addressEditText = findViewById(R.id.addressEditText);
dateEditText = findViewById(R.id.dateEditText);
databaseAppointments = FirebaseDatabase.getInstance().getReference("appointments");
bookButton = findViewById(R.id.bookButton);
bookButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addAppointment();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
}
private void addAppointment() {
String name = clientEditText.getText().toString();
String address = addressEditText.getText().toString();
String date = dateEditText.getText().toString();
if(!TextUtils.isEmpty(name) && !TextUtils.isEmpty(address) && !TextUtils.isEmpty(date)) {
String id = databaseAppointments.push().getKey();
SingleAppointment singleAppt = new SingleAppointment(id, name, address, date);
databaseAppointments.child(id).setValue(singleAppt);
clientEditText.setText("");
addressEditText.setText("");
dateEditText.setText("");
} else {
Toast.makeText(this, "Please enter a valid name, address, and date.", Toast.LENGTH_LONG).show();
}
}
}
SingleAppointment.java 类(每个ListView 项目都是这些对象之一):
public class SingleAppointment {
private String id;
private String clientName;
private String address;
private String date;
public SingleAppointment() {}
public SingleAppointment(String clientName, String address, String date) {
this.clientName = clientName;
this.address = address;
this.date = date;
}
public SingleAppointment(String id, String clientName, String address, String date) {
this.id = id;
this.clientName = clientName;
this.address = address;
this.date = date;
}
public String getId() {
return id;
}
public String getName() {
return clientName;
}
public String getAddress() {
return address;
}
public String getDate() {
return date;
}
}
【问题讨论】:
-
您可以从您的代码中提供示例,但请不要全部提供给我们。我们无法为您调试您的代码。更具体。
-
你得到什么错误?它发生在哪一行?
-
正如@26hmkk 评论的那样:您的问题中的代码比我们大多数人有兴趣阅读的要多。如果你用更少的代码重现同样的问题,你就更有可能得到答案,也不太可能被否决。请参阅how to create a minimal, complete, verifiable example,了解如何执行此操作以及为什么它会有所帮助。
标签: android firebase firebase-realtime-database