You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

点击‘Voir La Liste’按钮后,如何按选定条件展示牙医列表?

Hey there! Let's walk through exactly how to implement this feature—from capturing your selected filters to passing that data and displaying the matching dentist list in the next activity. I'll break it down into actionable steps with code examples to make it concrete.

1. Capture Selected Filter Criteria First

First, you need to gather all the user's selected conditions from your UI components (like checkboxes, spinners, radio groups, etc.). Let's use Kotlin for examples since it's the standard for modern Android development:

// Example: Collect selected dental specialties from checkboxes
val selectedSpecialties = mutableListOf<String>()
val specialtyCheckboxes = listOf(checkboxGeneralDentistry, checkboxOrthodontics, checkboxPediatrics)
specialtyCheckboxes.forEach { checkbox ->
    if (checkbox.isChecked) {
        selectedSpecialties.add(checkbox.text.toString())
    }
}

// Example: Get selected city from a spinner
val selectedCity = spinnerLocation.selectedItem.toString()

// Example: Get selected sort option from a radio group
val selectedSortOption = when (radioGroupSort.checkedRadioButtonId) {
    R.id.radioSortByRating -> "rating"
    R.id.radioSortByDistance -> "distance"
    else -> "name"
}
2. Manage "Selected Views" State

When you talk about "Selected Views," I assume you mean preserving the state of the user's selected filters (so if they navigate back, their choices are still visible). The best way to handle this is with a ViewModel to retain state across configuration changes and screen navigation:

// Create a ViewModel to hold filter state
class FilterViewModel : ViewModel() {
    val selectedSpecialties = MutableLiveData<MutableList<String>>()
    val selectedCity = MutableLiveData<String>()
    val selectedSortOption = MutableLiveData<String>()

    // Update methods to sync with user input
    fun updateSpecialties(specialties: MutableList<String>) {
        selectedSpecialties.value = specialties
    }

    fun updateCity(city: String) {
        selectedCity.value = city
    }
}

// In your filter Activity, update the ViewModel when user selects options
val viewModel = ViewModelProvider(this)[FilterViewModel::class.java]
viewModel.updateSpecialties(selectedSpecialties)
viewModel.updateCity(selectedCity)

If you need to persist the state between app sessions, you can use SharedPreferences to save the selected values and load them when the screen starts.

3. Pass Filter Data to the Next Activity

To send the collected filter criteria to the dentist list Activity, use an Intent with a Parcelable data class (this is cleaner than passing multiple individual extras):

First, define a Parcelable data class for your filter criteria:

@Parcelize
data class DentistFilters(
    val specialties: List<String>,
    val city: String,
    val sortBy: String
) : Parcelable

Then, in the click listener for your "Voir La Liste" button:

buttonVoirLaListe.setOnClickListener {
    // Compile all selected filters into the data class
    val filters = DentistFilters(selectedSpecialties, selectedCity, selectedSortOption)

    // Create intent and attach the filters
    val intent = Intent(this, DentistListActivity::class.java)
    intent.putExtra("DENTIST_FILTERS", filters)

    // Launch the list activity
    startActivity(intent)
}
4. Receive Data & Display Filtered Dentist List

In your target DentistListActivity, retrieve the filters, apply them to your dentist dataset (whether it's from a local database or API), and display the results in a RecyclerView:

class DentistListActivity : AppCompatActivity() {
    private lateinit var adapter: DentistAdapter
    // Assume this is your full dataset (replace with actual data source)
    private val allDentists = mutableListOf<Dentist>(
        Dentist("Dr. Smith", "General Dentistry", "Paris", 4.8, 2.5),
        Dentist("Dr. Lee", "Orthodontics", "Paris", 4.6, 1.2)
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_dentist_list)

        // Retrieve the filters from the intent
        val filters = intent.getParcelableExtra<DentistFilters>("DENTIST_FILTERS")

        // Filter and sort the dentist list based on criteria
        val filteredDentists = filters?.let {
            allDentists.filter { dentist ->
                // Match specialties (show all if no specialties are selected)
                it.specialties.isEmpty() || dentist.specialty in it.specialties
                // Match city
                && dentist.city == it.city
            }.sortedWith(
                when(it.sortBy) {
                    "rating" -> compareByDescending { dentist -> dentist.rating }
                    "distance" -> compareBy { dentist -> dentist.distance }
                    else -> compareBy { dentist -> dentist.name }
                }
            )
        } ?: allDentists

        // Set up RecyclerView to display results
        adapter = DentistAdapter(filteredDentists)
        recyclerViewDentists.adapter = adapter
        recyclerViewDentists.layoutManager = LinearLayoutManager(this)
    }
}

// Sample Dentist data class
data class Dentist(
    val name: String,
    val specialty: String,
    val city: String,
    val rating: Double,
    val distance: Double
)
Quick Bonus Tips
  • If your dentist data comes from an API, pass the filter criteria as query parameters in your network request instead of filtering locally (more efficient for large datasets).
  • Use LiveData in the list Activity's ViewModel to observe data changes (like if filters are updated later) and automatically refresh the list.
  • For "Selected Views" UI feedback (like highlighting selected checkboxes/spinners), make sure to sync the UI state with your ViewModel when the screen loads.

内容的提问来源于stack exchange,提问作者Nesrine

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:29:49