我有一个MainActivity片段,其中有一个ListView
。每个ListView
项目都有三个TextViews
。我制作了一个用于ListView
的自定义适配器,而这个自定义适配器正是我尝试在三个不同的setText()
上使用TextView
的地方。 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;
}
}
答案 0 :(得分:0)
检查nameTextView的值是否为空