Issue
I'm making a E-commerce app, which has a brand list to choose from to filter the products, I'm saving all the brands in arrays.xml inside string array, but some of the strings shows as white spaces at runtime just like the screenshot, and when I click on this white space value it shows value correctly, I don't understand and searched for similar issue but couldn't find anything
Here's a sample of my brand_list in strings.xml and the same in strings.xml(ar-rEG)
<string-array name="brand_list">
<item>الكل</item>
<item>212</item>
<item>Acm</item>
<item>Anivagene</item>
<item>Balea</item>
<item>Bioblas</item>
<item>BioGroup</item>
<item>Breeze</item>
<item>Exaggerate</item>
<item>Exfoliate</item>
<item>Farm Stay</item>
<item>Fresh Talk</item>
<item>Isis</item>
<item>Kolagra</item>
<item>Mash</item>
<item>Melatex</item>
<item>OGX</item>
<item>Olay</item>
<item>Rionire</item>
<item>Sessa</item>
<item>Si</item>
</string-array>
StoreHomeFragment
private lateinit var brandAdapter: ArrayAdapter<String>
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Rest of the code
brandAdapterImpl()
// Rest of the code
// Setting brand adapter ready.
private fun brandAdapterImpl() {
brandAdapter = ArrayAdapter(
requireActivity(),
android.R.layout.simple_list_item_1,
resources.getStringArray(R.array.brand_list)
)
// Setting the adapter to brandAdapter and observe the selected brand for any future changes.
binding.autoCompleteBrand.run {
setAdapter(brandAdapter)
viewModel.selectedBrandPosition.observe(viewLifecycleOwner) {
setText(brandAdapter.getItem(it), false)
isSaveEnabled = false
freezesText = false
}
}
// When choosing item from the brandList save it into liveData in the viewModel.
binding.autoCompleteBrand.setOnItemClickListener { _, _, position, _ ->
val brand = brandAdapter.getItem(position)
viewModel.saveSelectedBrand(brand!!, position)
viewModel.filterProducts()
}
}
Here is what I have tried:
-Changed the size of the string array to be smaller.
-Changed the string array to be array
-Deleted the arabic and left only english brand as it's might be the 2 languages
-Put the brand list inside string.xml and string.xml (ar - rEG)
Solution
It was all about the layout, I've used ArrayAdapter and the
android.R.layout.simple_dropdown_item_1line
So the only difference is in declaring brandAdapter:
brandAdapter = ArrayAdapter(
requireActivity(),
android.R.layout.simple_dropdown_item_1line,
resources.getStringArray(R.array.brand_list)
)
The list now is showns all the values in the array whether it was arabic or english.
Answered By - Mahmoud Reda
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.