সেভ করা পৃষ্ঠা গুছিয়ে রাখতে 'সংগ্রহ' ব্যবহার করুন
আপনার পছন্দ অনুযায়ী কন্টেন্ট সেভ করুন ও সঠিক বিভাগে রাখুন।
জেটপ্যাক কম্পোজে টেক্সট ইনপুটের উপর ভিত্তি করে স্ট্রিংগুলির একটি তালিকার মাধ্যমে কীভাবে ফিল্টার করা যায় এই নির্দেশিকাটি ব্যাখ্যা করে। ব্যবহারকারী অনুসন্ধান প্রশ্নের উপর ভিত্তি করে গতিশীলভাবে একটি তালিকা আপডেট করতে এই পদ্ধতি ব্যবহার করুন।
সংস্করণ সামঞ্জস্য
এই বাস্তবায়ন কম্পোজ সংস্করণ 1.2.0 এবং উচ্চতর সাথে কাজ করে।
নির্ভরতা
আপনার build.gradle এ নিম্নলিখিত নির্ভরতা অন্তর্ভুক্ত করুন:
পাঠ্য ইনপুটের উপর ভিত্তি করে একটি তালিকা ফিল্টার করুন
একসাথে, নিম্নলিখিত স্নিপেটগুলি একটি তালিকা তৈরি করে যা ব্যবহারকারীর ধরন হিসাবে বাস্তব সময়ে আপডেট হয়। এই উদাহরণটি তালিকা ডেটা এবং ফিল্টারিং লজিক ধরে রাখতে একটি ViewModel ব্যবহার করে, যখন FilterTextView() ফাংশনটি UI তৈরি করে যা ফিল্টার পাঠ্য পরিবর্তিত হলে স্বয়ংক্রিয়ভাবে আপডেট হয়।
classFilterTextViewModel:ViewModel(){privatevalitems=listOf("Cupcake","Donut","Eclair","Froyo","Gingerbread","Honeycomb","Ice Cream Sandwich")privateval_filteredItems=MutableStateFlow(items)varfilteredItems:StateFlow<List<String>>=_filteredItemsfunfilterText(input:String){// This filter returns the full items list when input is an empty string._filteredItems.value=items.filter{it.contains(input,ignoreCase=true)}}}
ViewModel কোড কম্পোজেবল থেকে ফিল্টারিং কাজকে বিমূর্ত করে।
ViewModel মূল এবং ফিল্টার করা উভয় তালিকাই ধারণ করে। এটি ফিল্টার করা আইটেমগুলিকে ধরে রাখতে আইটেমগুলির একটি তালিকা এবং একটি MutableStateFlow সংজ্ঞায়িত করে।
filterText ফাংশন প্রদত্ত ইনপুট স্ট্রিং এর উপর ভিত্তি করে তালিকাটি ফিল্টার করে এবং filteredItems অবস্থা আপডেট করে, যা UI-তে ফেরত পাঠানো হয়।
text by rememberSaveable { mutableStateOf("") } ফিল্টার পাঠ্য ক্ষেত্রে প্রবেশ করা বর্তমান পাঠ্যটিকে ধরে রাখতে একটি রাষ্ট্র পরিবর্তনশীল text তৈরি করে।
rememberSaveable কনফিগারেশন পরিবর্তন জুড়ে পাঠ্যের মান সংরক্ষণ করে।
by কীওয়ার্ড টেক্সটের মানকে MutableState অবজেক্টের মান সম্পত্তিতে অর্পণ করে।
OutlinedTextField ভিউ মডেল থেকে filterText ফাংশনকে কল করে যখন টেক্সট পরিবর্তনগুলি onValueChange কলব্যাককে ট্রিগার করে।
ফলাফল
চিত্র 1. একটি ফিল্টার করা তালিকা যা নতুন পাঠ্য প্রবেশ করানো হলে আপডেট হয়।
এই নির্দেশিকা ধারণকারী সংগ্রহ
এই নির্দেশিকাটি এই কিউরেট করা কুইক গাইড সংগ্রহের অংশ যা বৃহত্তর অ্যান্ড্রয়েড উন্নয়ন লক্ষ্যগুলি কভার করে:
ব্যবহারকারীর ইনপুট অনুরোধ করুন
টেক্সট এন্ট্রি করে এবং ইনপুটের অন্যান্য উপায় ব্যবহার করে ব্যবহারকারীদের আপনার অ্যাপের সাথে ইন্টারঅ্যাক্ট করার উপায়গুলি কীভাবে বাস্তবায়ন করবেন তা শিখুন।
এই পৃষ্ঠার কন্টেন্ট ও কোডের নমুনাগুলি Content License-এ বর্ণিত লাইসেন্সের অধীনস্থ। Java এবং OpenJDK হল Oracle এবং/অথবা তার অ্যাফিলিয়েট সংস্থার রেজিস্টার্ড ট্রেডমার্ক।
2025-02-06 UTC-তে শেষবার আপডেট করা হয়েছে।
[[["সহজে বোঝা যায়","easyToUnderstand","thumb-up"],["আমার সমস্যার সমাধান হয়েছে","solvedMyProblem","thumb-up"],["অন্যান্য","otherUp","thumb-up"]],[["এতে আমার প্রয়োজনীয় তথ্য নেই","missingTheInformationINeed","thumb-down"],["খুব জটিল / অনেক ধাপ","tooComplicatedTooManySteps","thumb-down"],["পুরনো","outOfDate","thumb-down"],["অনুবাদ সংক্রান্ত সমস্যা","translationIssue","thumb-down"],["নমুনা / কোড সংক্রান্ত সমস্যা","samplesCodeIssue","thumb-down"],["অন্যান্য","otherDown","thumb-down"]],["2025-02-06 UTC-তে শেষবার আপডেট করা হয়েছে।"],[],[],null,["\u003cbr /\u003e\n\nThis guide explains how to filter through a list of strings based on text input\nin Jetpack Compose. Use this approach to dynamically update a list based on user\nsearch queries.\n\nVersion compatibility\n\nThis implementation works with Compose versions 1.2.0 and higher.\n\nDependencies\n\nInclude the following dependencies in your `build.gradle`:\n\nFilter a list based on text input\n\nTogether, the following snippets produce a list that updates in real time as the\nuser types. This example uses a [`ViewModel`](/reference/androidx/lifecycle/ViewModel)\nto hold the list data and filtering logic, while the `FilterTextView()` function\ncreates the UI that updates automatically whenever the filter text changes.\n\n\n```kotlin\nclass FilterTextViewModel : ViewModel() {\n private val items = listOf(\n \"Cupcake\",\n \"Donut\",\n \"Eclair\",\n \"Froyo\",\n \"Gingerbread\",\n \"Honeycomb\",\n \"Ice Cream Sandwich\"\n )\n\n private val _filteredItems = MutableStateFlow(items)\n var filteredItems: StateFlow\u003cList\u003cString\u003e\u003e = _filteredItems\n\n fun filterText(input: String) {\n // This filter returns the full items list when input is an empty string.\n _filteredItems.value = items.filter { it.contains(input, ignoreCase = true) }\n }\n}https://github.com/android/snippets/blob/e4396f6dd13aaa8099c4baa671cdd549a10f201c/compose/snippets/src/main/java/com/example/compose/snippets/text/FilterText.kt#L42-L60\n```\n\n\u003cbr /\u003e\n\nKey points about the code\n\n- The `ViewModel` code abstracts the filtering work away from the composable.\n- The `ViewModel` holds both the original and filtered lists. It defines a list of items and a `MutableStateFlow` to hold the filtered items.\n- The `filterText` function filters the list based on the provided input string and updates the `filteredItems` state, which is passed back into the UI.\n\n\n```kotlin\n@Composable\nfun FilterTextView(modifier: Modifier = Modifier, viewModel: FilterTextViewModel = viewModel()) {\n val filteredItems by viewModel.filteredItems.collectAsStateWithLifecycle()\n var text by rememberSaveable { mutableStateOf(\"\") }\n\n Column(\n modifier = Modifier\n .fillMaxSize()\n .padding(all = 10.dp)\n ) {\n OutlinedTextField(\n value = text,\n onValueChange = {\n text = it\n viewModel.filterText(text)\n },\n label = { Text(\"Filter Text\") },\n modifier = Modifier.fillMaxWidth()\n )\n\n LazyColumn {\n items(\n count = filteredItems.size,\n key = { index -\u003e filteredItems[index] }\n ) {\n ListItem(\n headlineContent = { Text(filteredItems[it]) },\n modifier = Modifier\n .fillParentMaxWidth()\n .padding(10.dp)\n )\n }\n }\n }\n}https://github.com/android/snippets/blob/e4396f6dd13aaa8099c4baa671cdd549a10f201c/compose/snippets/src/main/java/com/example/compose/snippets/text/FilterText.kt#L64-L98\n```\n\n\u003cbr /\u003e\n\nKey points about the code\n\n- Displays an [`OutlinedTextField`](/reference/kotlin/androidx/compose/material3/package-summary#OutlinedTextField(androidx.compose.ui.text.input.TextFieldValue,kotlin.Function1,androidx.compose.ui.Modifier,kotlin.Boolean,kotlin.Boolean,androidx.compose.ui.text.TextStyle,kotlin.Function0,kotlin.Function0,kotlin.Function0,kotlin.Function0,kotlin.Function0,kotlin.Function0,kotlin.Function0,kotlin.Boolean,androidx.compose.ui.text.input.VisualTransformation,androidx.compose.foundation.text.KeyboardOptions,androidx.compose.foundation.text.KeyboardActions,kotlin.Boolean,kotlin.Int,kotlin.Int,androidx.compose.foundation.interaction.MutableInteractionSource,androidx.compose.ui.graphics.Shape,androidx.compose.material3.TextFieldColors)) for user input and a [`LazyColumn`](/reference/kotlin/androidx/compose/foundation/lazy/package-summary#LazyColumn(androidx.compose.ui.Modifier,androidx.compose.foundation.lazy.LazyListState,androidx.compose.foundation.layout.PaddingValues,kotlin.Boolean,androidx.compose.foundation.layout.Arrangement.Vertical,androidx.compose.ui.Alignment.Horizontal,androidx.compose.foundation.gestures.FlingBehavior,kotlin.Boolean,androidx.compose.foundation.OverscrollEffect,kotlin.Function1)) to display the filtered list items.\n- Collects the `filteredItems` state flow from the `ViewModel` and converts it into a lifecycle-aware `State` object.\n - [`collectAsStateWithLifecycle`](/reference/kotlin/androidx/lifecycle/compose/package-summary#(kotlinx.coroutines.flow.StateFlow).collectAsStateWithLifecycle(androidx.lifecycle.Lifecycle,androidx.lifecycle.Lifecycle.State,kotlin.coroutines.CoroutineContext)) collects the latest value from the `StateFlow` and recomposes the UI when the value changes.\n- `text by rememberSaveable { mutableStateOf(\"\") }` creates a state variable `text` to hold the current text entered in the filter text field.\n - `rememberSaveable` preserves the value of text across configuration changes.\n - The `by` keyword delegates the value of text to the value property of the `MutableState` object.\n- `OutlinedTextField` calls the `filterText` function from the view model when text changes trigger the `onValueChange` callback.\n\nResult\n\n\u003cbr /\u003e\n\n**Figure 1.** A filtered list that updates as new text is entered.\n\n\u003cbr /\u003e\n\nCollections that contain this guide\n\nThis guide is part of these curated Quick Guide collections that cover\nbroader Android development goals: \n\nRequest user input \nLearn how to implement ways for users to interact with your app by entering text and using other means of input. \n[Quick guide collection](/develop/ui/compose/quick-guides/collections/request-user-input) \n\nHave questions or feedback \nGo to our frequently asked questions page and learn about quick guides or reach out and let us know your thoughts. \n[Go to FAQ](/quick-guides/faq) [Leave feedback](https://issuetracker.google.com/issues/new?component=1573691&template=1993320)"]]