I have this custom ArrayAdapter
class, the data source is a firebase real-time database. I use the same
custom ArrayAdapter
in multiple activities. on the each list item there is a button to open the same activity but I am passing doctor name from the list item.
I want to also pass the action bar title when of the activity that I call the custom ArrayAdapter
here is the DoctorAdapter class.
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.doc_list_item, parent, false);
}
final Doctor doctor = getItem(position);
TextView dName, dSpeciality, dAvailability, dTime, dWeekDays, dAbout;
dName = convertView.findViewById(R.id.doc_name_text_view);
dName.setText(doctor.getName());
dSpeciality = convertView.findViewById(R.id.doc_speciality_text_view);
dSpeciality.setText(doctor.getSpeciality());
dTime = convertView.findViewById(R.id.doc_time_text_view);
dTime.setText(doctor.getTime());
dWeekDays = convertView.findViewById(R.id.doc_weekDays_text_view);
dWeekDays.setText(doctor.getWeekDays());
dAbout = convertView.findViewById(R.id.doc_info_text_view);
dAbout.setText(doctor.getInformation());
ImageView docImage;
docImage = convertView.findViewById(R.id.doc_image_view);
Glide.with(docImage.getContext())
.load(doctor.getImage())
.into(docImage);
Button bookAptBtn = convertView.findViewById(R.id.doc_book_apt_btn);
dAvailability = convertView.findViewById(R.id.doc_availability_text_view);
dAvailability.setText(doctor.getAvailability());
bookAptBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, BookAppointmentActivity.class);
intent.putExtra("doctorName", doctor.getName());
mContext.startActivity(intent);
}
});
return convertView;
}
My OrthoDocsActivity
public class OrthoDocsActivity extends AppCompatActivity {
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mOrthoDocsRef;
private ListView orthoDocsListView;
private ArrayList<Doctor> doctorArrayList;
private DoctorAdapter adapter;
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
findViewById(R.id.ortho_docs_progressBar).setVisibility(View.GONE);
doctorArrayList.clear();
for (DataSnapshot ds : snapshot.getChildren()) {
Doctor doctor = ds.getValue(Doctor.class);
doctorArrayList.add(doctor);
}
orthoDocsListView.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ortho_docs);
mFirebaseDatabase = FirebaseDatabase.getInstance();
mOrthoDocsRef = mFirebaseDatabase.getReference().child("doctor");
orthoDocsListView = findViewById(R.id.ortho_docs_listView);
doctorArrayList = new ArrayList<>();
adapter = new DoctorAdapter(this, R.layout.doc_list_item, doctorArrayList);
Query query = FirebaseDatabase.getInstance().getReference().child("doctor").orderByChild("speciality")
.equalTo("Orthopedics");
query.addValueEventListener(valueEventListener);
}
Since I use the DoctorAdapter in multiple activties I want to pass the action bar title when I click on the button on list items
You can simply create a String variable title in your adapter class. And passes the value of the variable with the constructor of the adapter. Then on button click, you send the value with putExtra.
adapter = new DoctorAdapter(this, R.layout.doc_list_item, doctorArrayList,titleBar);
And in the adapter code will look like
bookAptBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, BookAppointmentActivity.class);
intent.putExtra("doctorName", doctor.getName());
intent.putExtra("Title", title); // the action bar title you want
mContext.startActivity(intent);
}
});