Я пытаюсь выполнить поиск в массиве с текстом редактирования и кнопкой, но не могу получить текст из EditText. Например, если я изменяю val textSearch : String = search.text.toString()
на textSearch : String ="a"
, весь код работает, поэтому проблема должна быть здесь. Я перепробовал разные вещи, но по какой-то причине не могу заставить код работать.
class noticias : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.noticias, container, false) as View
val listView : ListView = view?.findViewById(R.id.news_list_view)
val btnSearch: Button = view?.findViewById(R.id.btnSearch)
val search : EditText = view?.findViewById(R.id.searchText)
val textSearch : String = search.text.toString()
var names: ArrayList<NewsObject> = arrayListOf(
NewsObject(R.drawable.bioron, "aaaa"),
NewsObject(R.drawable.behind, "dddd"),
NewsObject(R.drawable.bioron, "aaaa"),
NewsObject(R.drawable.behind, "dddd"),
NewsObject(R.drawable.bioron, "aaaa"),
NewsObject(R.drawable.behind, "dddd"),
NewsObject(R.drawable.juv, "jjjjkj")
)
listView.adapter = MyCustomAdapter(requireContext(),names)
fun searchNews(search: String): ArrayList<NewsObject> {
val namesTemp:ArrayList<NewsObject> = arrayListOf()
for(i in names.count()-1 downTo 0){
Log.i("sda", names[i].title)
if(names[i].title.contains(search.trim())){
namesTemp.add(names[i])
}
}
return namesTemp
}
btnSearch.setOnClickListener() {
if(textSearch.count() == 0){
Toast.makeText(requireContext(),textSearch, Toast.LENGTH_LONG).show()
listView.adapter = MyCustomAdapter(requireContext(),names)
}else{
Toast.makeText(requireContext(),"1", Toast.LENGTH_LONG).show()
listView.adapter = MyCustomAdapter(requireContext(), searchNews(textSearch))
}
}
return view
}
data class NewsObject(val img: Int, val title: String)
public class MyCustomAdapter(context: Context, namesTemp: ArrayList<NewsObject>): BaseAdapter() {
private val mContext: Context
val names:ArrayList<NewsObject> = namesTemp
init {
mContext = context
}
override fun getCount(): Int {
return names.size
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getItem(position: Int): Any {
return "TEST STRING"
}
override fun getView(position: Int, convertView: View?, viewGroup: ViewGroup?): View {
val layoutInflater = LayoutInflater.from(mContext)
val rowMain = layoutInflater.inflate(R.layout.news_row, viewGroup, false)
val nameTextView = rowMain.findViewById<TextView>(R.id.name_textView)
nameTextView.text = names.get(position).title
/*val positionTextView = rowMain.findViewById<TextView>(R.id.position_textview)
positionTextView.text = names.get(position).img*/
val img = rowMain.findViewById<ImageView>(R.id.img_new)
img.setImageResource(names.get(position).img)
return rowMain
}
}
}