How to implement abbreviation search to SearchViewHow to use SharedPreferences in Android to store, fetch and edit valuesCreating hashmap/map from XML resourcesAndroid Search in ListView not working properlyHow to save an Android Activity state using save instance state?How do I center text horizontally and vertically in a TextView?Why is the Android emulator so slow? How can we speed up the Android emulator?How do I fix android.os.NetworkOnMainThreadException?Unfortunately MyApp has stopped. How can I solve this?How to filter a RecyclerView with a SearchViewHow to filter Recyclerview with SearchView on multiple parametersMoshi's Custom Adapter with RxAndroid & Retrofit & KotlinApp crash when use mobie data (3g/4g)Retrofit 2 - Getting response 200, but list is empty
How to make a setting relevant?
Russian equivalent of the French expression "broyer du noir"
I have 2 attacks. Can I shoot a light/heavy crossbow, then throw a dart in same turn?
PL/SQL function to receive a number and return its binary format
What is the advantage of carrying a tripod and ND-filters when you could use image stacking instead?
How do I write "Show, Don't Tell" as an Asperger?
How to retract the pitched idea from employer?
Building a road to escape Earth's gravity by making a pyramid on Antartica
4 Layer PCB stack up
You've spoiled/damaged the card
Implement Homestuck's Catenative Doomsday Dice Cascader
Does the first version of Linux developed by Linus Torvalds have a GUI?
What happens when the attacking player dies to damage triggers after killing the blocking creatures in the first combat step of double strike?
Why is the application of an oracle function not a measurement?
My coworkers think I had a long honeymoon. Actually I was diagnosed with cancer. How do I talk about it?
Why don’t airliners have temporary liveries?
How can drunken, homicidal elves successfully conduct a wild hunt?
Etymology of 'calcit(r)are'?
Can a user sell my software (MIT license) without modification?
What LISP compilers and interpreters were available for 8-bit machines?
Does an ice chest packed full of frozen food need ice?
Smooth switching between 12v batteries, with toggle switch
Are there any existing monsters I can use as a basis for a baby skeleton statblock?
About the expansion of seq_set_split
How to implement abbreviation search to SearchView
How to use SharedPreferences in Android to store, fetch and edit valuesCreating hashmap/map from XML resourcesAndroid Search in ListView not working properlyHow to save an Android Activity state using save instance state?How do I center text horizontally and vertically in a TextView?Why is the Android emulator so slow? How can we speed up the Android emulator?How do I fix android.os.NetworkOnMainThreadException?Unfortunately MyApp has stopped. How can I solve this?How to filter a RecyclerView with a SearchViewHow to filter Recyclerview with SearchView on multiple parametersMoshi's Custom Adapter with RxAndroid & Retrofit & KotlinApp crash when use mobie data (3g/4g)Retrofit 2 - Getting response 200, but list is empty
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a Fragment containing a list of strings (company names) which works with SearchView. However, due to many companies having long names, is there a way where I can type in an abbreviation for a company name rather than having to type in the whole company name? 'FTSE 150' and 'FTSE 250' are self-explanatory hence don't need abbereviations.
Abberviations for company names
- GSK - GlaxoSmithKline plc
- HSX - Hiscox Ltd
- IHG - InterContinental
Hotels Group plc - MKS - Marks & Spencer Group plc
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="company_names">
<item>@string/glaxosmithkline_plc</item>
<item>@string/hiscox_ltd</item>
<item>@string/intercontinental_hotels_group_plc</item>
<item>@string/marks_and_spencer_group_plc</item>
<item>@string/ftse_150</item>
<item>@string/ftse_250</item>
</string-array>
<string name="glaxosmithkline_plc">GlaxoSmithKline plc</string>
<string name="hiscox_ltd">Hiscox Ltd</string>
<string name="intercontinental_hotels_group_plc">InterContinental Hotels Group plc</string>
<string name="marks_and_spencer_group_plc">Marks & Spencer Group plc</string>
<string name="ftse_150">FTSE 150</string>
<string name="ftse_250">FTSE 250</string>
</resources>
fragment class
class MyFragment : androidx.fragment.app.Fragment()
private var mAdapter: MyListAdapter? = null
private lateinit var mRecyclerView: androidx.recyclerview.widget.RecyclerView
private var mTwoPane: Boolean = false
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
val view = inflater.inflate(R.layout.layout_recyclerview, container, false)
mTwoPane = (activity as androidx.fragment.app.FragmentActivity).findViewById<View>(R.id.detail_container) != null
mRecyclerView = view.findViewById(R.id.recyclerView_list)
mRecyclerView.setHasFixedSize(true)
mRecyclerView.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this.activity)
mRecyclerView.addItemDecoration(androidx.recyclerview.widget.DividerItemDecoration(Objects.requireNonNull<Context>(context), LinearLayout.VERTICAL))
val myList = ArrayList<Companies>()
// val items = resources.getStringArray(R.array.company_names)
// for (n in items)
// val company = Companies(0, "", "")
// myList.add(company)
//
val companyA = Companies(1, "GlaxoSmithKline plc", "GSK")
val companyB = Companies(2, "Hiscox Ltd", "HSX")
val companyC = Companies(3, "InterContinental Hotels Group plc", "IHG")
val companyD = Companies(4, "Marks & Spencer Group plc", "MKS")
val companyE = Companies(5, "FTSE 150", "")
val companyF = Companies(6, "FTSE 250", "")
val myList = DatabaseHandler(this.context!!)
myList.insertData(companyA)
myList.insertData(companyB)
myList.insertData(companyC)
myList.insertData(companyD)
myList.insertData(companyE)
myList.insertData(companyF)
mAdapter = MyListAdapter(activity!!, myList, mTwoPane)
mRecyclerView.adapter = mAdapter
return view
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater)
val mInflater = Objects.requireNonNull<androidx.fragment.app.FragmentActivity>(activity).menuInflater
mInflater.inflate(R.menu.menu_search, menu)
val searchView = searchitem.actionView as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener
override fun onQueryTextSubmit(query: String): Boolean
return false
override fun onQueryTextChange(newText: String): Boolean
mAdapter!!.filter.filter(newText)
return false
)
super.onCreateOptionsMenu(menu, inflater)
MyListAdapter class
class MyListAdapter(private val mCtx: Context, private val myList: MutableList<Companies>,
private val
mTwoPane: Boolean) : androidx.recyclerview.widget.RecyclerView.Adapter<MyListAdapter
.CompanyViewHolder>(), Filterable
private var myListFull = myList.toMutableList()
private val companyFilter = object : Filter()
override fun performFiltering(constraint: CharSequence?): Filter.FilterResults
val filteredList = ArrayList<Companies>()
when
constraint == null
val results = Filter.FilterResults()
results.values = filteredList
return results
override fun publishResults(constraint: CharSequence?, results: Filter.FilterResults?)
myList.clear()
myList.addAll(results!!.values as List<Companies>)
notifyDataSetChanged()
inner class CompanyViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView
.ViewHolder(itemView)
var tvTitle: TextView = itemView.findViewById(R.id.tv_RVItem)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CompanyViewHolder
val inflater = LayoutInflater.from(mCtx)
val v = inflater.inflate(R.layout.recyclerview_item_textview, parent, false)
return CompanyViewHolder(v)
override fun onBindViewHolder(holder: CompanyViewHolder, position: Int)
val product = myList[holder.adapterPosition]
holder.tvTitle.text = product.companyfuName
override fun getItemCount(): Int
return myList.size
override fun getFilter(): Filter
return companyFilter



UPDATES
Custom model class
data class Companies (val id: String, val fullName: String, val abbreviation: String)
updated Adapter class
class MyListAdapter(private val mCtx: Context,
private val mCompanies: MutableList<Companies>,
private val mTwoPane: Boolean) : androidx.recyclerview.widget.RecyclerView.Adapter<MyListAdapter
.CompanyViewHolder>(), Filterable
private val mCompaniesFull = mCompanies.toMutableList()
private val companyFilter = object : Filter()
override fun performFiltering(constraint: CharSequence?): Filter.FilterResults
val filteredList = if (constraint == null
override fun publishResults(constraint: CharSequence?, results: Filter.FilterResults?)
mCompanies.clear()
mCompanies.addAll(results!!.values as List<Companies>)
notifyDataSetChanged()
private fun String.matchesIgnoreCase(otherString: String): Boolean
return this.toLowerCase().contains(otherString.trim().toLowerCase())
inner class CompanyViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView
.ViewHolder(itemView)
var tvTitle: TextView = itemView.findViewById(R.id.tv_RVItem)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CompanyViewHolder
val inflater = LayoutInflater.from(mCtx)
val v = inflater.inflate(R.layout.recyclerview_item_textview, parent, false)
return CompanyViewHolder(v)
override fun onBindViewHolder(holder: CompanyViewHolder, position: Int)
val product = mCompanies[holder.adapterPosition]
holder.tvTitle.text = product.companyName
override fun getItemCount(): Int
return mCompanies.size
override fun getFilter(): Filter
return companyFilter
updated Fragment class
class MonFragment : androidx.fragment.app.Fragment()
private var mAdapter: MyListAdapter? = null
private lateinit var mRecyclerView: androidx.recyclerview.widget.RecyclerView
private var mTwoPane: Boolean = false
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
val view = inflater.inflate(R.layout.layout_recyclerview, container, false)
mTwoPane = (activity as androidx.fragment.app.FragmentActivity).findViewById<View>(R.id.detail_container) != null
mRecyclerView = view.findViewById<RecyclerView>(R.id.recyclerView_list)
mRecyclerView.setHasFixedSize(true)
mRecyclerView.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this.activity)
mRecyclerView.addItemDecoration(androidx.recyclerview.widget.DividerItemDecoration(Objects.requireNonNull<Context>(context), LinearLayout.VERTICAL))
mCompanies.add(Companies("GlaxoSmithKline plc", "GSK"))
mCompanies.add(Companies("Hiscox Ltd", "HSX"))
mCompanies.add(Companies("InterContinental Hotels Group plc", "IHG"))
mCompanies.add(Companies("Marks & Spencer Group plc", "MKS"))
mCompanies.add(Companies("FTSE 150", ""))
mCompanies.add(Companies("FTSE 250", ""))
mAdapter = MyListAdapter(activity!!, mCompanies, mTwoPane)
mRecyclerView.adapter = mAdapter
return view
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater)
val mInflater = Objects.requireNonNull<androidx.fragment.app.FragmentActivity>(activity).menuInflater
mInflater.inflate(R.menu.menu_search, menu)
val searchitem = menu.findItem(R.id.action_search)
val searchView = searchitem.actionView as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener
override fun onQueryTextSubmit(query: String): Boolean
return false
override fun onQueryTextChange(newText: String): Boolean
mAdapter!!.filter.filter(newText)
mAdapter!!.notifyDataSetChanged()
return false
)
super.onCreateOptionsMenu(menu, inflater)
|
show 6 more comments
I have a Fragment containing a list of strings (company names) which works with SearchView. However, due to many companies having long names, is there a way where I can type in an abbreviation for a company name rather than having to type in the whole company name? 'FTSE 150' and 'FTSE 250' are self-explanatory hence don't need abbereviations.
Abberviations for company names
- GSK - GlaxoSmithKline plc
- HSX - Hiscox Ltd
- IHG - InterContinental
Hotels Group plc - MKS - Marks & Spencer Group plc
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="company_names">
<item>@string/glaxosmithkline_plc</item>
<item>@string/hiscox_ltd</item>
<item>@string/intercontinental_hotels_group_plc</item>
<item>@string/marks_and_spencer_group_plc</item>
<item>@string/ftse_150</item>
<item>@string/ftse_250</item>
</string-array>
<string name="glaxosmithkline_plc">GlaxoSmithKline plc</string>
<string name="hiscox_ltd">Hiscox Ltd</string>
<string name="intercontinental_hotels_group_plc">InterContinental Hotels Group plc</string>
<string name="marks_and_spencer_group_plc">Marks & Spencer Group plc</string>
<string name="ftse_150">FTSE 150</string>
<string name="ftse_250">FTSE 250</string>
</resources>
fragment class
class MyFragment : androidx.fragment.app.Fragment()
private var mAdapter: MyListAdapter? = null
private lateinit var mRecyclerView: androidx.recyclerview.widget.RecyclerView
private var mTwoPane: Boolean = false
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
val view = inflater.inflate(R.layout.layout_recyclerview, container, false)
mTwoPane = (activity as androidx.fragment.app.FragmentActivity).findViewById<View>(R.id.detail_container) != null
mRecyclerView = view.findViewById(R.id.recyclerView_list)
mRecyclerView.setHasFixedSize(true)
mRecyclerView.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this.activity)
mRecyclerView.addItemDecoration(androidx.recyclerview.widget.DividerItemDecoration(Objects.requireNonNull<Context>(context), LinearLayout.VERTICAL))
val myList = ArrayList<Companies>()
// val items = resources.getStringArray(R.array.company_names)
// for (n in items)
// val company = Companies(0, "", "")
// myList.add(company)
//
val companyA = Companies(1, "GlaxoSmithKline plc", "GSK")
val companyB = Companies(2, "Hiscox Ltd", "HSX")
val companyC = Companies(3, "InterContinental Hotels Group plc", "IHG")
val companyD = Companies(4, "Marks & Spencer Group plc", "MKS")
val companyE = Companies(5, "FTSE 150", "")
val companyF = Companies(6, "FTSE 250", "")
val myList = DatabaseHandler(this.context!!)
myList.insertData(companyA)
myList.insertData(companyB)
myList.insertData(companyC)
myList.insertData(companyD)
myList.insertData(companyE)
myList.insertData(companyF)
mAdapter = MyListAdapter(activity!!, myList, mTwoPane)
mRecyclerView.adapter = mAdapter
return view
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater)
val mInflater = Objects.requireNonNull<androidx.fragment.app.FragmentActivity>(activity).menuInflater
mInflater.inflate(R.menu.menu_search, menu)
val searchView = searchitem.actionView as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener
override fun onQueryTextSubmit(query: String): Boolean
return false
override fun onQueryTextChange(newText: String): Boolean
mAdapter!!.filter.filter(newText)
return false
)
super.onCreateOptionsMenu(menu, inflater)
MyListAdapter class
class MyListAdapter(private val mCtx: Context, private val myList: MutableList<Companies>,
private val
mTwoPane: Boolean) : androidx.recyclerview.widget.RecyclerView.Adapter<MyListAdapter
.CompanyViewHolder>(), Filterable
private var myListFull = myList.toMutableList()
private val companyFilter = object : Filter()
override fun performFiltering(constraint: CharSequence?): Filter.FilterResults
val filteredList = ArrayList<Companies>()
when
constraint == null
val results = Filter.FilterResults()
results.values = filteredList
return results
override fun publishResults(constraint: CharSequence?, results: Filter.FilterResults?)
myList.clear()
myList.addAll(results!!.values as List<Companies>)
notifyDataSetChanged()
inner class CompanyViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView
.ViewHolder(itemView)
var tvTitle: TextView = itemView.findViewById(R.id.tv_RVItem)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CompanyViewHolder
val inflater = LayoutInflater.from(mCtx)
val v = inflater.inflate(R.layout.recyclerview_item_textview, parent, false)
return CompanyViewHolder(v)
override fun onBindViewHolder(holder: CompanyViewHolder, position: Int)
val product = myList[holder.adapterPosition]
holder.tvTitle.text = product.companyfuName
override fun getItemCount(): Int
return myList.size
override fun getFilter(): Filter
return companyFilter



UPDATES
Custom model class
data class Companies (val id: String, val fullName: String, val abbreviation: String)
updated Adapter class
class MyListAdapter(private val mCtx: Context,
private val mCompanies: MutableList<Companies>,
private val mTwoPane: Boolean) : androidx.recyclerview.widget.RecyclerView.Adapter<MyListAdapter
.CompanyViewHolder>(), Filterable
private val mCompaniesFull = mCompanies.toMutableList()
private val companyFilter = object : Filter()
override fun performFiltering(constraint: CharSequence?): Filter.FilterResults
val filteredList = if (constraint == null
override fun publishResults(constraint: CharSequence?, results: Filter.FilterResults?)
mCompanies.clear()
mCompanies.addAll(results!!.values as List<Companies>)
notifyDataSetChanged()
private fun String.matchesIgnoreCase(otherString: String): Boolean
return this.toLowerCase().contains(otherString.trim().toLowerCase())
inner class CompanyViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView
.ViewHolder(itemView)
var tvTitle: TextView = itemView.findViewById(R.id.tv_RVItem)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CompanyViewHolder
val inflater = LayoutInflater.from(mCtx)
val v = inflater.inflate(R.layout.recyclerview_item_textview, parent, false)
return CompanyViewHolder(v)
override fun onBindViewHolder(holder: CompanyViewHolder, position: Int)
val product = mCompanies[holder.adapterPosition]
holder.tvTitle.text = product.companyName
override fun getItemCount(): Int
return mCompanies.size
override fun getFilter(): Filter
return companyFilter
updated Fragment class
class MonFragment : androidx.fragment.app.Fragment()
private var mAdapter: MyListAdapter? = null
private lateinit var mRecyclerView: androidx.recyclerview.widget.RecyclerView
private var mTwoPane: Boolean = false
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
val view = inflater.inflate(R.layout.layout_recyclerview, container, false)
mTwoPane = (activity as androidx.fragment.app.FragmentActivity).findViewById<View>(R.id.detail_container) != null
mRecyclerView = view.findViewById<RecyclerView>(R.id.recyclerView_list)
mRecyclerView.setHasFixedSize(true)
mRecyclerView.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this.activity)
mRecyclerView.addItemDecoration(androidx.recyclerview.widget.DividerItemDecoration(Objects.requireNonNull<Context>(context), LinearLayout.VERTICAL))
mCompanies.add(Companies("GlaxoSmithKline plc", "GSK"))
mCompanies.add(Companies("Hiscox Ltd", "HSX"))
mCompanies.add(Companies("InterContinental Hotels Group plc", "IHG"))
mCompanies.add(Companies("Marks & Spencer Group plc", "MKS"))
mCompanies.add(Companies("FTSE 150", ""))
mCompanies.add(Companies("FTSE 250", ""))
mAdapter = MyListAdapter(activity!!, mCompanies, mTwoPane)
mRecyclerView.adapter = mAdapter
return view
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater)
val mInflater = Objects.requireNonNull<androidx.fragment.app.FragmentActivity>(activity).menuInflater
mInflater.inflate(R.menu.menu_search, menu)
val searchitem = menu.findItem(R.id.action_search)
val searchView = searchitem.actionView as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener
override fun onQueryTextSubmit(query: String): Boolean
return false
override fun onQueryTextChange(newText: String): Boolean
mAdapter!!.filter.filter(newText)
mAdapter!!.notifyDataSetChanged()
return false
)
super.onCreateOptionsMenu(menu, inflater)
please share MyListAdapter and specifically it's filter property impl. You would have to do the magic there
– Jakub Licznerski
Mar 24 at 19:51
@JakubLicznerski Added :-)
– MacaronLover
Mar 24 at 20:38
Why don't you want to add one field with abbreviations and fill it with data? Then search for any of two fields.
– CoolMind
Mar 24 at 21:22
1
I didn't read the code, but I think you have a database with strings. So you can read data from two fields of the database.strings.xmlis intended for constant resources, it cannot be changed until application upgrade.
– CoolMind
Mar 25 at 7:08
1
As @CoolMind saidstrings.xmlmost problably is not great solution for you, but if you need to use it here is an answer to similar question.
– Jakub Licznerski
Mar 25 at 7:25
|
show 6 more comments
I have a Fragment containing a list of strings (company names) which works with SearchView. However, due to many companies having long names, is there a way where I can type in an abbreviation for a company name rather than having to type in the whole company name? 'FTSE 150' and 'FTSE 250' are self-explanatory hence don't need abbereviations.
Abberviations for company names
- GSK - GlaxoSmithKline plc
- HSX - Hiscox Ltd
- IHG - InterContinental
Hotels Group plc - MKS - Marks & Spencer Group plc
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="company_names">
<item>@string/glaxosmithkline_plc</item>
<item>@string/hiscox_ltd</item>
<item>@string/intercontinental_hotels_group_plc</item>
<item>@string/marks_and_spencer_group_plc</item>
<item>@string/ftse_150</item>
<item>@string/ftse_250</item>
</string-array>
<string name="glaxosmithkline_plc">GlaxoSmithKline plc</string>
<string name="hiscox_ltd">Hiscox Ltd</string>
<string name="intercontinental_hotels_group_plc">InterContinental Hotels Group plc</string>
<string name="marks_and_spencer_group_plc">Marks & Spencer Group plc</string>
<string name="ftse_150">FTSE 150</string>
<string name="ftse_250">FTSE 250</string>
</resources>
fragment class
class MyFragment : androidx.fragment.app.Fragment()
private var mAdapter: MyListAdapter? = null
private lateinit var mRecyclerView: androidx.recyclerview.widget.RecyclerView
private var mTwoPane: Boolean = false
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
val view = inflater.inflate(R.layout.layout_recyclerview, container, false)
mTwoPane = (activity as androidx.fragment.app.FragmentActivity).findViewById<View>(R.id.detail_container) != null
mRecyclerView = view.findViewById(R.id.recyclerView_list)
mRecyclerView.setHasFixedSize(true)
mRecyclerView.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this.activity)
mRecyclerView.addItemDecoration(androidx.recyclerview.widget.DividerItemDecoration(Objects.requireNonNull<Context>(context), LinearLayout.VERTICAL))
val myList = ArrayList<Companies>()
// val items = resources.getStringArray(R.array.company_names)
// for (n in items)
// val company = Companies(0, "", "")
// myList.add(company)
//
val companyA = Companies(1, "GlaxoSmithKline plc", "GSK")
val companyB = Companies(2, "Hiscox Ltd", "HSX")
val companyC = Companies(3, "InterContinental Hotels Group plc", "IHG")
val companyD = Companies(4, "Marks & Spencer Group plc", "MKS")
val companyE = Companies(5, "FTSE 150", "")
val companyF = Companies(6, "FTSE 250", "")
val myList = DatabaseHandler(this.context!!)
myList.insertData(companyA)
myList.insertData(companyB)
myList.insertData(companyC)
myList.insertData(companyD)
myList.insertData(companyE)
myList.insertData(companyF)
mAdapter = MyListAdapter(activity!!, myList, mTwoPane)
mRecyclerView.adapter = mAdapter
return view
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater)
val mInflater = Objects.requireNonNull<androidx.fragment.app.FragmentActivity>(activity).menuInflater
mInflater.inflate(R.menu.menu_search, menu)
val searchView = searchitem.actionView as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener
override fun onQueryTextSubmit(query: String): Boolean
return false
override fun onQueryTextChange(newText: String): Boolean
mAdapter!!.filter.filter(newText)
return false
)
super.onCreateOptionsMenu(menu, inflater)
MyListAdapter class
class MyListAdapter(private val mCtx: Context, private val myList: MutableList<Companies>,
private val
mTwoPane: Boolean) : androidx.recyclerview.widget.RecyclerView.Adapter<MyListAdapter
.CompanyViewHolder>(), Filterable
private var myListFull = myList.toMutableList()
private val companyFilter = object : Filter()
override fun performFiltering(constraint: CharSequence?): Filter.FilterResults
val filteredList = ArrayList<Companies>()
when
constraint == null
val results = Filter.FilterResults()
results.values = filteredList
return results
override fun publishResults(constraint: CharSequence?, results: Filter.FilterResults?)
myList.clear()
myList.addAll(results!!.values as List<Companies>)
notifyDataSetChanged()
inner class CompanyViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView
.ViewHolder(itemView)
var tvTitle: TextView = itemView.findViewById(R.id.tv_RVItem)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CompanyViewHolder
val inflater = LayoutInflater.from(mCtx)
val v = inflater.inflate(R.layout.recyclerview_item_textview, parent, false)
return CompanyViewHolder(v)
override fun onBindViewHolder(holder: CompanyViewHolder, position: Int)
val product = myList[holder.adapterPosition]
holder.tvTitle.text = product.companyfuName
override fun getItemCount(): Int
return myList.size
override fun getFilter(): Filter
return companyFilter



UPDATES
Custom model class
data class Companies (val id: String, val fullName: String, val abbreviation: String)
updated Adapter class
class MyListAdapter(private val mCtx: Context,
private val mCompanies: MutableList<Companies>,
private val mTwoPane: Boolean) : androidx.recyclerview.widget.RecyclerView.Adapter<MyListAdapter
.CompanyViewHolder>(), Filterable
private val mCompaniesFull = mCompanies.toMutableList()
private val companyFilter = object : Filter()
override fun performFiltering(constraint: CharSequence?): Filter.FilterResults
val filteredList = if (constraint == null
override fun publishResults(constraint: CharSequence?, results: Filter.FilterResults?)
mCompanies.clear()
mCompanies.addAll(results!!.values as List<Companies>)
notifyDataSetChanged()
private fun String.matchesIgnoreCase(otherString: String): Boolean
return this.toLowerCase().contains(otherString.trim().toLowerCase())
inner class CompanyViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView
.ViewHolder(itemView)
var tvTitle: TextView = itemView.findViewById(R.id.tv_RVItem)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CompanyViewHolder
val inflater = LayoutInflater.from(mCtx)
val v = inflater.inflate(R.layout.recyclerview_item_textview, parent, false)
return CompanyViewHolder(v)
override fun onBindViewHolder(holder: CompanyViewHolder, position: Int)
val product = mCompanies[holder.adapterPosition]
holder.tvTitle.text = product.companyName
override fun getItemCount(): Int
return mCompanies.size
override fun getFilter(): Filter
return companyFilter
updated Fragment class
class MonFragment : androidx.fragment.app.Fragment()
private var mAdapter: MyListAdapter? = null
private lateinit var mRecyclerView: androidx.recyclerview.widget.RecyclerView
private var mTwoPane: Boolean = false
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
val view = inflater.inflate(R.layout.layout_recyclerview, container, false)
mTwoPane = (activity as androidx.fragment.app.FragmentActivity).findViewById<View>(R.id.detail_container) != null
mRecyclerView = view.findViewById<RecyclerView>(R.id.recyclerView_list)
mRecyclerView.setHasFixedSize(true)
mRecyclerView.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this.activity)
mRecyclerView.addItemDecoration(androidx.recyclerview.widget.DividerItemDecoration(Objects.requireNonNull<Context>(context), LinearLayout.VERTICAL))
mCompanies.add(Companies("GlaxoSmithKline plc", "GSK"))
mCompanies.add(Companies("Hiscox Ltd", "HSX"))
mCompanies.add(Companies("InterContinental Hotels Group plc", "IHG"))
mCompanies.add(Companies("Marks & Spencer Group plc", "MKS"))
mCompanies.add(Companies("FTSE 150", ""))
mCompanies.add(Companies("FTSE 250", ""))
mAdapter = MyListAdapter(activity!!, mCompanies, mTwoPane)
mRecyclerView.adapter = mAdapter
return view
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater)
val mInflater = Objects.requireNonNull<androidx.fragment.app.FragmentActivity>(activity).menuInflater
mInflater.inflate(R.menu.menu_search, menu)
val searchitem = menu.findItem(R.id.action_search)
val searchView = searchitem.actionView as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener
override fun onQueryTextSubmit(query: String): Boolean
return false
override fun onQueryTextChange(newText: String): Boolean
mAdapter!!.filter.filter(newText)
mAdapter!!.notifyDataSetChanged()
return false
)
super.onCreateOptionsMenu(menu, inflater)
I have a Fragment containing a list of strings (company names) which works with SearchView. However, due to many companies having long names, is there a way where I can type in an abbreviation for a company name rather than having to type in the whole company name? 'FTSE 150' and 'FTSE 250' are self-explanatory hence don't need abbereviations.
Abberviations for company names
- GSK - GlaxoSmithKline plc
- HSX - Hiscox Ltd
- IHG - InterContinental
Hotels Group plc - MKS - Marks & Spencer Group plc
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="company_names">
<item>@string/glaxosmithkline_plc</item>
<item>@string/hiscox_ltd</item>
<item>@string/intercontinental_hotels_group_plc</item>
<item>@string/marks_and_spencer_group_plc</item>
<item>@string/ftse_150</item>
<item>@string/ftse_250</item>
</string-array>
<string name="glaxosmithkline_plc">GlaxoSmithKline plc</string>
<string name="hiscox_ltd">Hiscox Ltd</string>
<string name="intercontinental_hotels_group_plc">InterContinental Hotels Group plc</string>
<string name="marks_and_spencer_group_plc">Marks & Spencer Group plc</string>
<string name="ftse_150">FTSE 150</string>
<string name="ftse_250">FTSE 250</string>
</resources>
fragment class
class MyFragment : androidx.fragment.app.Fragment()
private var mAdapter: MyListAdapter? = null
private lateinit var mRecyclerView: androidx.recyclerview.widget.RecyclerView
private var mTwoPane: Boolean = false
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
val view = inflater.inflate(R.layout.layout_recyclerview, container, false)
mTwoPane = (activity as androidx.fragment.app.FragmentActivity).findViewById<View>(R.id.detail_container) != null
mRecyclerView = view.findViewById(R.id.recyclerView_list)
mRecyclerView.setHasFixedSize(true)
mRecyclerView.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this.activity)
mRecyclerView.addItemDecoration(androidx.recyclerview.widget.DividerItemDecoration(Objects.requireNonNull<Context>(context), LinearLayout.VERTICAL))
val myList = ArrayList<Companies>()
// val items = resources.getStringArray(R.array.company_names)
// for (n in items)
// val company = Companies(0, "", "")
// myList.add(company)
//
val companyA = Companies(1, "GlaxoSmithKline plc", "GSK")
val companyB = Companies(2, "Hiscox Ltd", "HSX")
val companyC = Companies(3, "InterContinental Hotels Group plc", "IHG")
val companyD = Companies(4, "Marks & Spencer Group plc", "MKS")
val companyE = Companies(5, "FTSE 150", "")
val companyF = Companies(6, "FTSE 250", "")
val myList = DatabaseHandler(this.context!!)
myList.insertData(companyA)
myList.insertData(companyB)
myList.insertData(companyC)
myList.insertData(companyD)
myList.insertData(companyE)
myList.insertData(companyF)
mAdapter = MyListAdapter(activity!!, myList, mTwoPane)
mRecyclerView.adapter = mAdapter
return view
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater)
val mInflater = Objects.requireNonNull<androidx.fragment.app.FragmentActivity>(activity).menuInflater
mInflater.inflate(R.menu.menu_search, menu)
val searchView = searchitem.actionView as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener
override fun onQueryTextSubmit(query: String): Boolean
return false
override fun onQueryTextChange(newText: String): Boolean
mAdapter!!.filter.filter(newText)
return false
)
super.onCreateOptionsMenu(menu, inflater)
MyListAdapter class
class MyListAdapter(private val mCtx: Context, private val myList: MutableList<Companies>,
private val
mTwoPane: Boolean) : androidx.recyclerview.widget.RecyclerView.Adapter<MyListAdapter
.CompanyViewHolder>(), Filterable
private var myListFull = myList.toMutableList()
private val companyFilter = object : Filter()
override fun performFiltering(constraint: CharSequence?): Filter.FilterResults
val filteredList = ArrayList<Companies>()
when
constraint == null
val results = Filter.FilterResults()
results.values = filteredList
return results
override fun publishResults(constraint: CharSequence?, results: Filter.FilterResults?)
myList.clear()
myList.addAll(results!!.values as List<Companies>)
notifyDataSetChanged()
inner class CompanyViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView
.ViewHolder(itemView)
var tvTitle: TextView = itemView.findViewById(R.id.tv_RVItem)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CompanyViewHolder
val inflater = LayoutInflater.from(mCtx)
val v = inflater.inflate(R.layout.recyclerview_item_textview, parent, false)
return CompanyViewHolder(v)
override fun onBindViewHolder(holder: CompanyViewHolder, position: Int)
val product = myList[holder.adapterPosition]
holder.tvTitle.text = product.companyfuName
override fun getItemCount(): Int
return myList.size
override fun getFilter(): Filter
return companyFilter



UPDATES
Custom model class
data class Companies (val id: String, val fullName: String, val abbreviation: String)
updated Adapter class
class MyListAdapter(private val mCtx: Context,
private val mCompanies: MutableList<Companies>,
private val mTwoPane: Boolean) : androidx.recyclerview.widget.RecyclerView.Adapter<MyListAdapter
.CompanyViewHolder>(), Filterable
private val mCompaniesFull = mCompanies.toMutableList()
private val companyFilter = object : Filter()
override fun performFiltering(constraint: CharSequence?): Filter.FilterResults
val filteredList = if (constraint == null
override fun publishResults(constraint: CharSequence?, results: Filter.FilterResults?)
mCompanies.clear()
mCompanies.addAll(results!!.values as List<Companies>)
notifyDataSetChanged()
private fun String.matchesIgnoreCase(otherString: String): Boolean
return this.toLowerCase().contains(otherString.trim().toLowerCase())
inner class CompanyViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView
.ViewHolder(itemView)
var tvTitle: TextView = itemView.findViewById(R.id.tv_RVItem)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CompanyViewHolder
val inflater = LayoutInflater.from(mCtx)
val v = inflater.inflate(R.layout.recyclerview_item_textview, parent, false)
return CompanyViewHolder(v)
override fun onBindViewHolder(holder: CompanyViewHolder, position: Int)
val product = mCompanies[holder.adapterPosition]
holder.tvTitle.text = product.companyName
override fun getItemCount(): Int
return mCompanies.size
override fun getFilter(): Filter
return companyFilter
updated Fragment class
class MonFragment : androidx.fragment.app.Fragment()
private var mAdapter: MyListAdapter? = null
private lateinit var mRecyclerView: androidx.recyclerview.widget.RecyclerView
private var mTwoPane: Boolean = false
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
val view = inflater.inflate(R.layout.layout_recyclerview, container, false)
mTwoPane = (activity as androidx.fragment.app.FragmentActivity).findViewById<View>(R.id.detail_container) != null
mRecyclerView = view.findViewById<RecyclerView>(R.id.recyclerView_list)
mRecyclerView.setHasFixedSize(true)
mRecyclerView.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this.activity)
mRecyclerView.addItemDecoration(androidx.recyclerview.widget.DividerItemDecoration(Objects.requireNonNull<Context>(context), LinearLayout.VERTICAL))
mCompanies.add(Companies("GlaxoSmithKline plc", "GSK"))
mCompanies.add(Companies("Hiscox Ltd", "HSX"))
mCompanies.add(Companies("InterContinental Hotels Group plc", "IHG"))
mCompanies.add(Companies("Marks & Spencer Group plc", "MKS"))
mCompanies.add(Companies("FTSE 150", ""))
mCompanies.add(Companies("FTSE 250", ""))
mAdapter = MyListAdapter(activity!!, mCompanies, mTwoPane)
mRecyclerView.adapter = mAdapter
return view
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater)
val mInflater = Objects.requireNonNull<androidx.fragment.app.FragmentActivity>(activity).menuInflater
mInflater.inflate(R.menu.menu_search, menu)
val searchitem = menu.findItem(R.id.action_search)
val searchView = searchitem.actionView as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener
override fun onQueryTextSubmit(query: String): Boolean
return false
override fun onQueryTextChange(newText: String): Boolean
mAdapter!!.filter.filter(newText)
mAdapter!!.notifyDataSetChanged()
return false
)
super.onCreateOptionsMenu(menu, inflater)
edited Apr 11 at 15:50
MacaronLover
asked Mar 24 at 15:07
MacaronLoverMacaronLover
2,14642863
2,14642863
please share MyListAdapter and specifically it's filter property impl. You would have to do the magic there
– Jakub Licznerski
Mar 24 at 19:51
@JakubLicznerski Added :-)
– MacaronLover
Mar 24 at 20:38
Why don't you want to add one field with abbreviations and fill it with data? Then search for any of two fields.
– CoolMind
Mar 24 at 21:22
1
I didn't read the code, but I think you have a database with strings. So you can read data from two fields of the database.strings.xmlis intended for constant resources, it cannot be changed until application upgrade.
– CoolMind
Mar 25 at 7:08
1
As @CoolMind saidstrings.xmlmost problably is not great solution for you, but if you need to use it here is an answer to similar question.
– Jakub Licznerski
Mar 25 at 7:25
|
show 6 more comments
please share MyListAdapter and specifically it's filter property impl. You would have to do the magic there
– Jakub Licznerski
Mar 24 at 19:51
@JakubLicznerski Added :-)
– MacaronLover
Mar 24 at 20:38
Why don't you want to add one field with abbreviations and fill it with data? Then search for any of two fields.
– CoolMind
Mar 24 at 21:22
1
I didn't read the code, but I think you have a database with strings. So you can read data from two fields of the database.strings.xmlis intended for constant resources, it cannot be changed until application upgrade.
– CoolMind
Mar 25 at 7:08
1
As @CoolMind saidstrings.xmlmost problably is not great solution for you, but if you need to use it here is an answer to similar question.
– Jakub Licznerski
Mar 25 at 7:25
please share MyListAdapter and specifically it's filter property impl. You would have to do the magic there
– Jakub Licznerski
Mar 24 at 19:51
please share MyListAdapter and specifically it's filter property impl. You would have to do the magic there
– Jakub Licznerski
Mar 24 at 19:51
@JakubLicznerski Added :-)
– MacaronLover
Mar 24 at 20:38
@JakubLicznerski Added :-)
– MacaronLover
Mar 24 at 20:38
Why don't you want to add one field with abbreviations and fill it with data? Then search for any of two fields.
– CoolMind
Mar 24 at 21:22
Why don't you want to add one field with abbreviations and fill it with data? Then search for any of two fields.
– CoolMind
Mar 24 at 21:22
1
1
I didn't read the code, but I think you have a database with strings. So you can read data from two fields of the database.
strings.xml is intended for constant resources, it cannot be changed until application upgrade.– CoolMind
Mar 25 at 7:08
I didn't read the code, but I think you have a database with strings. So you can read data from two fields of the database.
strings.xml is intended for constant resources, it cannot be changed until application upgrade.– CoolMind
Mar 25 at 7:08
1
1
As @CoolMind said
strings.xml most problably is not great solution for you, but if you need to use it here is an answer to similar question.– Jakub Licznerski
Mar 25 at 7:25
As @CoolMind said
strings.xml most problably is not great solution for you, but if you need to use it here is an answer to similar question.– Jakub Licznerski
Mar 25 at 7:25
|
show 6 more comments
1 Answer
1
active
oldest
votes
Following the discussion in comments I'd suggest two approaches for storing the data:
- Device storage either with SharedPreferences or databases: SQLite, Room (they are lightweight local storage databases, Internet connection is not needed).
- Device memory (not recommended) as a List of hardcoded objects initialized either in
MainActivityorFragment'sonCreatemethod (dependent on the usage).
As @CoolMind suggested you should implement a custom model class CompanyName(id, fullName, abbreviation) and then filter by the alternative of fullName and abbreviation.
EDIT:
In MyListAdapter get rid of myListFull as it does nothing and put:
private val stockFilter = object : Filter()
override fun performFiltering(constraint: CharSequence?): Filter.FilterResults
Also add in the MyListAdapter class this extension function:
private fun String.matchesIgnoreCase(otherString: String): Boolean
return this.toLowerCase().contains(otherString.trim().toLowerCase())
I have tried the 1st suggestion but I still don't understand how to show inserted data in the RecyclerView.
– MacaronLover
Mar 28 at 23:22
@MacaronLover see my edited answer
– Jakub Licznerski
Mar 29 at 19:55
Unresolved reference 'fullName' and 'abbreviation'. See my updated code.
– MacaronLover
Mar 31 at 14:21
Because you use a class namedStockin filter, inDatabaseHandleryou haveTasksandCompaniesI believe it all should beCompanyNameor betterCompanywhich is the suggested model class... there is big mess in the code you posted, so it's hard to guess. I've posted a solution proposition which you should adjust to your code not copy-paste.
– Jakub Licznerski
Mar 31 at 15:32
What about the Code for concern section? Surely that code would need to change as I'm not using a string array but a database helper instead.
– MacaronLover
Mar 31 at 18:07
|
show 18 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55325193%2fhow-to-implement-abbreviation-search-to-searchview%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Following the discussion in comments I'd suggest two approaches for storing the data:
- Device storage either with SharedPreferences or databases: SQLite, Room (they are lightweight local storage databases, Internet connection is not needed).
- Device memory (not recommended) as a List of hardcoded objects initialized either in
MainActivityorFragment'sonCreatemethod (dependent on the usage).
As @CoolMind suggested you should implement a custom model class CompanyName(id, fullName, abbreviation) and then filter by the alternative of fullName and abbreviation.
EDIT:
In MyListAdapter get rid of myListFull as it does nothing and put:
private val stockFilter = object : Filter()
override fun performFiltering(constraint: CharSequence?): Filter.FilterResults
Also add in the MyListAdapter class this extension function:
private fun String.matchesIgnoreCase(otherString: String): Boolean
return this.toLowerCase().contains(otherString.trim().toLowerCase())
I have tried the 1st suggestion but I still don't understand how to show inserted data in the RecyclerView.
– MacaronLover
Mar 28 at 23:22
@MacaronLover see my edited answer
– Jakub Licznerski
Mar 29 at 19:55
Unresolved reference 'fullName' and 'abbreviation'. See my updated code.
– MacaronLover
Mar 31 at 14:21
Because you use a class namedStockin filter, inDatabaseHandleryou haveTasksandCompaniesI believe it all should beCompanyNameor betterCompanywhich is the suggested model class... there is big mess in the code you posted, so it's hard to guess. I've posted a solution proposition which you should adjust to your code not copy-paste.
– Jakub Licznerski
Mar 31 at 15:32
What about the Code for concern section? Surely that code would need to change as I'm not using a string array but a database helper instead.
– MacaronLover
Mar 31 at 18:07
|
show 18 more comments
Following the discussion in comments I'd suggest two approaches for storing the data:
- Device storage either with SharedPreferences or databases: SQLite, Room (they are lightweight local storage databases, Internet connection is not needed).
- Device memory (not recommended) as a List of hardcoded objects initialized either in
MainActivityorFragment'sonCreatemethod (dependent on the usage).
As @CoolMind suggested you should implement a custom model class CompanyName(id, fullName, abbreviation) and then filter by the alternative of fullName and abbreviation.
EDIT:
In MyListAdapter get rid of myListFull as it does nothing and put:
private val stockFilter = object : Filter()
override fun performFiltering(constraint: CharSequence?): Filter.FilterResults
Also add in the MyListAdapter class this extension function:
private fun String.matchesIgnoreCase(otherString: String): Boolean
return this.toLowerCase().contains(otherString.trim().toLowerCase())
I have tried the 1st suggestion but I still don't understand how to show inserted data in the RecyclerView.
– MacaronLover
Mar 28 at 23:22
@MacaronLover see my edited answer
– Jakub Licznerski
Mar 29 at 19:55
Unresolved reference 'fullName' and 'abbreviation'. See my updated code.
– MacaronLover
Mar 31 at 14:21
Because you use a class namedStockin filter, inDatabaseHandleryou haveTasksandCompaniesI believe it all should beCompanyNameor betterCompanywhich is the suggested model class... there is big mess in the code you posted, so it's hard to guess. I've posted a solution proposition which you should adjust to your code not copy-paste.
– Jakub Licznerski
Mar 31 at 15:32
What about the Code for concern section? Surely that code would need to change as I'm not using a string array but a database helper instead.
– MacaronLover
Mar 31 at 18:07
|
show 18 more comments
Following the discussion in comments I'd suggest two approaches for storing the data:
- Device storage either with SharedPreferences or databases: SQLite, Room (they are lightweight local storage databases, Internet connection is not needed).
- Device memory (not recommended) as a List of hardcoded objects initialized either in
MainActivityorFragment'sonCreatemethod (dependent on the usage).
As @CoolMind suggested you should implement a custom model class CompanyName(id, fullName, abbreviation) and then filter by the alternative of fullName and abbreviation.
EDIT:
In MyListAdapter get rid of myListFull as it does nothing and put:
private val stockFilter = object : Filter()
override fun performFiltering(constraint: CharSequence?): Filter.FilterResults
Also add in the MyListAdapter class this extension function:
private fun String.matchesIgnoreCase(otherString: String): Boolean
return this.toLowerCase().contains(otherString.trim().toLowerCase())
Following the discussion in comments I'd suggest two approaches for storing the data:
- Device storage either with SharedPreferences or databases: SQLite, Room (they are lightweight local storage databases, Internet connection is not needed).
- Device memory (not recommended) as a List of hardcoded objects initialized either in
MainActivityorFragment'sonCreatemethod (dependent on the usage).
As @CoolMind suggested you should implement a custom model class CompanyName(id, fullName, abbreviation) and then filter by the alternative of fullName and abbreviation.
EDIT:
In MyListAdapter get rid of myListFull as it does nothing and put:
private val stockFilter = object : Filter()
override fun performFiltering(constraint: CharSequence?): Filter.FilterResults
Also add in the MyListAdapter class this extension function:
private fun String.matchesIgnoreCase(otherString: String): Boolean
return this.toLowerCase().contains(otherString.trim().toLowerCase())
edited Mar 29 at 20:43
MacaronLover
2,14642863
2,14642863
answered Mar 25 at 14:38
Jakub LicznerskiJakub Licznerski
443515
443515
I have tried the 1st suggestion but I still don't understand how to show inserted data in the RecyclerView.
– MacaronLover
Mar 28 at 23:22
@MacaronLover see my edited answer
– Jakub Licznerski
Mar 29 at 19:55
Unresolved reference 'fullName' and 'abbreviation'. See my updated code.
– MacaronLover
Mar 31 at 14:21
Because you use a class namedStockin filter, inDatabaseHandleryou haveTasksandCompaniesI believe it all should beCompanyNameor betterCompanywhich is the suggested model class... there is big mess in the code you posted, so it's hard to guess. I've posted a solution proposition which you should adjust to your code not copy-paste.
– Jakub Licznerski
Mar 31 at 15:32
What about the Code for concern section? Surely that code would need to change as I'm not using a string array but a database helper instead.
– MacaronLover
Mar 31 at 18:07
|
show 18 more comments
I have tried the 1st suggestion but I still don't understand how to show inserted data in the RecyclerView.
– MacaronLover
Mar 28 at 23:22
@MacaronLover see my edited answer
– Jakub Licznerski
Mar 29 at 19:55
Unresolved reference 'fullName' and 'abbreviation'. See my updated code.
– MacaronLover
Mar 31 at 14:21
Because you use a class namedStockin filter, inDatabaseHandleryou haveTasksandCompaniesI believe it all should beCompanyNameor betterCompanywhich is the suggested model class... there is big mess in the code you posted, so it's hard to guess. I've posted a solution proposition which you should adjust to your code not copy-paste.
– Jakub Licznerski
Mar 31 at 15:32
What about the Code for concern section? Surely that code would need to change as I'm not using a string array but a database helper instead.
– MacaronLover
Mar 31 at 18:07
I have tried the 1st suggestion but I still don't understand how to show inserted data in the RecyclerView.
– MacaronLover
Mar 28 at 23:22
I have tried the 1st suggestion but I still don't understand how to show inserted data in the RecyclerView.
– MacaronLover
Mar 28 at 23:22
@MacaronLover see my edited answer
– Jakub Licznerski
Mar 29 at 19:55
@MacaronLover see my edited answer
– Jakub Licznerski
Mar 29 at 19:55
Unresolved reference 'fullName' and 'abbreviation'. See my updated code.
– MacaronLover
Mar 31 at 14:21
Unresolved reference 'fullName' and 'abbreviation'. See my updated code.
– MacaronLover
Mar 31 at 14:21
Because you use a class named
Stock in filter, in DatabaseHandler you have Tasks and Companies I believe it all should be CompanyName or better Company which is the suggested model class... there is big mess in the code you posted, so it's hard to guess. I've posted a solution proposition which you should adjust to your code not copy-paste.– Jakub Licznerski
Mar 31 at 15:32
Because you use a class named
Stock in filter, in DatabaseHandler you have Tasks and Companies I believe it all should be CompanyName or better Company which is the suggested model class... there is big mess in the code you posted, so it's hard to guess. I've posted a solution proposition which you should adjust to your code not copy-paste.– Jakub Licznerski
Mar 31 at 15:32
What about the Code for concern section? Surely that code would need to change as I'm not using a string array but a database helper instead.
– MacaronLover
Mar 31 at 18:07
What about the Code for concern section? Surely that code would need to change as I'm not using a string array but a database helper instead.
– MacaronLover
Mar 31 at 18:07
|
show 18 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55325193%2fhow-to-implement-abbreviation-search-to-searchview%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
please share MyListAdapter and specifically it's filter property impl. You would have to do the magic there
– Jakub Licznerski
Mar 24 at 19:51
@JakubLicznerski Added :-)
– MacaronLover
Mar 24 at 20:38
Why don't you want to add one field with abbreviations and fill it with data? Then search for any of two fields.
– CoolMind
Mar 24 at 21:22
1
I didn't read the code, but I think you have a database with strings. So you can read data from two fields of the database.
strings.xmlis intended for constant resources, it cannot be changed until application upgrade.– CoolMind
Mar 25 at 7:08
1
As @CoolMind said
strings.xmlmost problably is not great solution for you, but if you need to use it here is an answer to similar question.– Jakub Licznerski
Mar 25 at 7:25