我需要一些帮助。我一直在尝试实现SearchView,但是当我使用过滤器时,数据不会更改。
我的项目包含一个带有适配器的列表视图。
为什么在我调用notifyDataSetChanged时ListView不显示新数据?
让我知道您是否需要特定的代码段或更多信息。 请帮我。预先感谢
class FragmentDiagnosticsTableViewController : Fragment(), SwipeRefreshLayout.OnRefreshListener {
lateinit var mSwipeRefreshLayout: SwipeRefreshLayout
lateinit var elementList: ListView
lateinit var elementListAdapter: ElementAdapter
override fun onRefresh() {
try {
if (connected) {
MaintenanceTableViewController().diagnosticSend()
while (!csvOkey) {
}
saveFile()
elementListAdapter = ElementAdapter(context!!, totalDiagnosticEntries)
elementList.adapter = elementListAdapter
csvOkey = false
} else {
try {
DiagnosticsTableViewController().loadFileCsv(mContext)
mSwipeRefreshLayout.setRefreshing(false)
elementListAdapter = ElementAdapter(context!!, totalDiagnosticEntries)
elementList.adapter = elementListAdapter
} catch (e: Exception) {
Toast.makeText(activity, "Impossible to refresh", Toast.LENGTH_SHORT).show()
mSwipeRefreshLayout.setRefreshing(false)
}
}
} catch (e: Exception) {
Toast.makeText(activity, "Impossible to refresh", Toast.LENGTH_SHORT).show()
mSwipeRefreshLayout.setRefreshing(false)
}
}
companion object {
fun newInstance(): Fragment {
var fb: FragmentDiagnosticsTableViewController = FragmentDiagnosticsTableViewController()
return fb
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val rootView = inflater!!.inflate(R.layout.fragment_diagnostic, container,
false)
var builder = StrictMode.VmPolicy.Builder()
StrictMode.setVmPolicy(builder.build())
mContext = activity as MaintenanceTableViewController
mSwipeRefreshLayout = rootView.findViewById<SwipeRefreshLayout>(R.id.swipeRefresh)
mSwipeRefreshLayout.setOnRefreshListener(this)
elementList = rootView.findViewById<ListView>(R.id.elementList)
(activity as AppCompatActivity).supportActionBar?.title = "Diagnostics"
setHasOptionsMenu(true)
return rootView
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.men_search, menu)
val searchItem = menu.findItem(R.id.action_search)
val searchView = searchItem.actionView as SearchView
searchView.setQueryHint("Search View Hint")
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean {
//Task HERE
return false
}
override fun onQueryTextChange(newText: String): Boolean {
if (TextUtils.isEmpty(newText)) {
elementListAdapter.filter("")
elementList.clearTextFilter()
} else {
elementListAdapter.filter(newText)
}
return true
}
})
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
try {
if (item.itemId == R.id.shareButton) {
var intentShareFile = Intent(Intent.ACTION_SEND)
val fileWithinMyDir = filePathMessageLogger
try {
if (fileWithinMyDir != null) {
if (fileWithinMyDir.exists()) {
intentShareFile.setType("text/plain")
val fileURI = FileProvider.getUriForFile(activity!!, "com.example.saradalmonte.pocketserviceandroid", fileWithinMyDir!!)
intentShareFile.putExtra(Intent.EXTRA_STREAM, fileURI)
intentShareFile.putExtra(Intent.EXTRA_TEXT, "File Csv")
startActivity(Intent.createChooser(intentShareFile, "Share file"))
return true
} else {
Toast.makeText(activity, "Error File not Exist", Toast.LENGTH_SHORT).show()
}
}
} catch (e: Exception) {
Toast.makeText(activity, "Error File not Exist", Toast.LENGTH_SHORT).show()
}
}
} catch (e: Exception) {
Toast.makeText(activity, "Error File not present", Toast.LENGTH_SHORT).show()
}
return true
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
if (view != null) {
super.onViewCreated(view, savedInstanceState)
elementListAdapter = ElementAdapter(context!!, totalDiagnosticEntries)
elementList.adapter = elementListAdapter
}
}
fun saveFile() {
val ma = activity as MaintenanceTableViewController
ma.saveFileDiagnostic()
}
class ElementAdapter : BaseAdapter {
var totalDiagnosticEntries = mutableListOf<DiagnosticsEntry>()
var filteredDiagnosticsEntry = arrayListOf<DiagnosticsEntry>()
var mcontext: Context? = null
lateinit var inflater: LayoutInflater
constructor(context: Context, totalDiagnosticEntries: MutableList<DiagnosticsEntry>) : super() {
this.totalDiagnosticEntries = totalDiagnosticEntries
this.mcontext = context
}
fun listViewAdapter(context: Context, totalDiagnosticEntries: MutableList<DiagnosticsEntry>) {
mcontext = context
this.totalDiagnosticEntries = totalDiagnosticEntries
inflater = LayoutInflater.from(context)
this.filteredDiagnosticsEntry.addAll(totalDiagnosticEntries)
}
override fun getItem(p0: Int): Any {
return totalDiagnosticEntries[p0]
}
override fun getItemId(p0: Int): Long {
return p0.toLong()
}
override fun getCount(): Int {
return totalDiagnosticEntries.size
}
override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
val element = totalDiagnosticEntries[p0]
var inflator = mcontext!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
var myView = inflator.inflate(R.layout.element_diagnostic, null)
myView.text_No.text = element.No!!
myView.text_TimeStamp.text = element.Timestamp!!
myView.text_errorCode.text = element.Code!!
if (element.Message1 == "") {
myView.text_Message1.visibility = View.INVISIBLE
} else {
myView.text_Message1.visibility = (View.VISIBLE)
myView.text_Message1.text = element.Message1!!
}
if (element.Message2 == "") {
myView.text_Message2.visibility = View.INVISIBLE
} else {
myView.text_Message2.visibility = (View.VISIBLE)
myView.text_Message2.text = element.Message2!!
}
val type = element.Type
if (type == "8002") {
val colorBar = myView.findViewById<View>(R.id.color_bar)
val color = ContextCompat.getColor(mcontext!!, R.color.greenSE)
colorBar.setBackgroundColor(color)
} else if (type == "2") {
val colorBar = myView.findViewById<View>(R.id.color_bar)
val color = ContextCompat.getColor(mcontext!!, R.color.Yellow)
colorBar.setBackgroundColor(color)
} else if (type == "3") {
val colorBar = myView.findViewById<View>(R.id.color_bar)
val color = ContextCompat.getColor(mcontext!!, R.color.Orange)
colorBar.setBackgroundColor(color)
} else if (type == "4") {
val colorBar = myView.findViewById<View>(R.id.color_bar)
val color = ContextCompat.getColor(mcontext!!, R.color.Red)
colorBar.setBackgroundColor(color)
} else {
val colorBar = myView.findViewById<View>(R.id.color_bar)
val color = ContextCompat.getColor(mcontext!!, R.color.Grey)
colorBar.setBackgroundColor(color)
}
myView.color_bar.setOnClickListener {
val intent = Intent(mcontext!!, DiagnosticsEntryDetailTableViewCell::class.java)
intent.putExtra("text_No", element.No)
intent.putExtra("text_TimeStamp", element.Timestamp)
intent.putExtra("text_Type", element.Type)
intent.putExtra("text_Object", element.Object)
intent.putExtra("text_Istance", element.Instance)
intent.putExtra("text_Object", element.Object)
intent.putExtra("text_Code", element.Code)
intent.putExtra("text_Message1", element.Message1)
intent.putExtra("text_Message2", element.Message2)
mContext!!.startActivity(intent)
}
return myView
}
fun filter(charText: String) {
var charText = charText
charText = charText.toLowerCase(Locale.getDefault())
filteredDiagnosticsEntry.clear()
if (charText.length == 0) {
filteredDiagnosticsEntry.addAll(totalDiagnosticEntries)
} else {
for (wp: DiagnosticsEntry in totalDiagnosticEntries) {
if (wp.No!!.contains(charText) || wp.Object!!.contains(charText) || wp.Instance!!.contains(charText) || wp.Message1!!.contains(charText)
|| wp.Message2!!.contains(charText)) {
filteredDiagnosticsEntry.add(wp)
}
}
}
this.notifyDataSetChanged()
}
}
答案 0 :(得分:0)
您过滤诊断条目并将其放入filteredDiagnosticsEntry
,但是在方法getView(...)
的适配器中,您仍然使用totalDiagnosticEntries
。
在方法totalDiagnosticEntries
,filteredDiagnosticsEntry
,getItem()
中以及在代码的其他片段中,尝试用getCount()
替换getView()
。请检查BaseAdapter的文档。
例如:
getCount()
此适配器表示的数据集中有多少个项目。
这意味着您要通过此适配器在ListView上显示多少个元素。如果您提供totalDiagnosticEntries
的大小,则适配器将尝试显示与totalDiagnosticEntries
包含的项目一样多-为每个项目调用getView
。