如何从自定义数组适配器传递一些数据

How to pass some data from a custom Array adapter

我有这个自定义 ArrayAdapter class,数据源是 firebase real-time 数据库。我用的一样 在多个活动中自定义 ArrayAdapter。在每个列表项上都有一个按钮可以打开相同的 activity 但我正在从列表项中传递医生姓名。 当我调用自定义 ArrayAdapter

的 activity 时,我还想传递操作栏标题

这是 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;
    } 

我的 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);
    }

因为我在多个活动中使用 DoctorAdapter,所以我想在单击列表项上的按钮时传递操作栏标题

您只需在适配器 class 中创建一个 String 变量标题即可。并将变量的值传递给适配器的构造函数。然后在单击按钮时,您使用 putExtra 发送值。

adapter = new DoctorAdapter(this, R.layout.doc_list_item, doctorArrayList,titleBar);

适配器代码看起来像

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);
            }
        });