【发布时间】:2019-04-22 20:15:13
【问题描述】:
我已经创建了一个列表视图并且代码工作正常。还创建了一个搜索框。我想根据列表的特定字段实现搜索功能。 l 在分离类中使用的列表适配器不在主要活动类中。我想在类列表适配器中使用搜索栏过滤器。
主要活动类
package com.iraqairoirt.iraqairports
import android.app.AlertDialog
import android.os.AsyncTask
import android.support.design.widget.TabLayout
import android.support.v7.app.AppCompatActivity
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_arrivel.*
import kotlinx.android.synthetic.main.fragment_dep.*
import org.json.JSONArray
import org.json.JSONObject
import java.net.HttpURLConnection
import java.net.URL
import android.widget.TextView
import com.iraqairoirt.iraqairports.flightsDep.ListAdapteDep
import com.iraqairoirt.iraqairports.flightsArr.ListAdapteArr
class MainActivity : AppCompatActivity() {
private var mSectionsPagerAdapter: SectionsPagerAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val url = "xxxxxxxxxxxxxxxxxxxxxxx"
Arr().execute(url)
Dep().execute(url)
setSupportActionBar(toolbar)
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager)
// Set up the ViewPager with the sections adapter.
container.adapter = mSectionsPagerAdapter
container.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabs))
tabs.addOnTabSelectedListener(TabLayout.ViewPagerOnTabSelectedListener(container))
}
// full class for json api
inner class Arr : AsyncTask<String, String, String>(){
val progressDialog = AlertDialog.Builder(this@MainActivity)
val dialogView = layoutInflater.inflate(R.layout.progress_dialog,null)
val message = dialogView.findViewById<TextView>(R.id.message_id)
val dialog = progressDialog.create()
override fun onPreExecute() {
super.onPreExecute()
dialog.setMessage("loading")
dialog.setCancelable(false)
dialog.show()
}
// for build connection
override fun doInBackground(vararg url: String?): String{
var text : String
val connection = URL(url[0]).openConnection() as HttpURLConnection
try {
connection.connect()
text = connection.inputStream.use { it.reader().use{reader -> reader.readText()} }
} finally{
connection.disconnect()
}
return text
}
override fun onPostExecute(result: String?) {
super.onPostExecute(result)
handleJson(result)
dialog.dismiss();
}
override fun onProgressUpdate(vararg text: String?) {
}
private fun handleJson (jsonString: String?){
val schedule = pluginData.getJSONObject("schedule")
val arrivals = schedule.getJSONObject("arrivals")
// val data = arrivals.getJSONObject("data")
val jsonArray = JSONArray(arrivals.get("data").toString())
val list = ArrayList<FlightShdu>()
var x = 0
while (x < jsonArray.length()){
val jsonObject = jsonArray.getJSONObject(x)
list.add(FlightShdu(
jsonObject.getJSONObject("flight").getJSONObject("identification").getJSONObject("number").getString("default"),
jsonObject.getJSONObject("flight").getJSONObject("airline").getString("short"),
jsonObject.getJSONObject("flight").getJSONObject("status").getJSONObject("generic").getJSONObject("status").getString("text"),
jsonObject.getJSONObject("flight").getJSONObject("airline").getJSONObject("code").getString("icao"),
jsonObject.getJSONObject("flight").getJSONObject("time").getJSONObject("scheduled").getString("arrival"),
jsonObject.getJSONObject("flight").getJSONObject("airport").getJSONObject("origin").getJSONObject("code").getString("iata"),
jsonObject.getJSONObject("flight").getJSONObject("aircraft").getJSONObject("model").getString("text"),
// for more information
jsonObject.getJSONObject("flight").getJSONObject("time").getJSONObject("real").getString("departure"),
jsonObject.getJSONObject("flight").getJSONObject("time").getJSONObject("estimated").getString("arrival"),
// jsonObject.getJSONObject("flight").getJSONObject("time").getJSONObject("estimated").getString("arrival"),
jsonObject.getJSONObject("flight").getJSONObject("aircraft").getString("registration"),
jsonObject.getJSONObject("flight").getJSONObject("status").getJSONObject("generic").getJSONObject("status").getString("diverted"),
arrivals.getString("timestamp"),
jsonObject.getJSONObject("flight").getJSONObject("status").getString("icon")
))
x++
}
list.forEach(::println)
val adapter = ListAdapteArr(this@MainActivity, list)
flight_arrivel_list.adapter = adapter
}
}
/**
* A [FragmentPagerAdapter] that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
inner class SectionsPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
override fun getItem(position: Int): Fragment? {
return when (position) {
0 -> {
fragment_Arr()
}
1 ->{
fragment_Dep()
}
2->{
fragment_Weather()
}
else-> {
return null
}
}
}
override fun getCount(): Int {
// Show 3 total pages.
return 2
}
}
}
列表适配器
class ListAdapteArr (val context: MainActivity, val list: ArrayList<FlightShdu>): BaseAdapter() {
@SuppressLint("ViewHolder", "NewApi")
override fun getView(p0: Int, convertView: View?, parent: ViewGroup?): View {
val view : View = LayoutInflater.from(context).inflate(R.layout.arr_list,parent,false)
val searchBar = view.findViewById(R.id.searchFlightId) as MaterialSearchBar
Airport.text= list.Airport
return view
}
override fun getItem(p0: Int): Any {
return list [p0]
}
override fun getItemId(p0: Int): Long {
return p0.toLong()
}
override fun getCount(): Int {
return list.size
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/flight_arrivel_list"
style="@style/Widget.AppCompat.ListView"
android:paddingStart="10sp"
android:paddingEnd="10sp"
android:paddingTop="0dp" android:layout_alignParentStart="true" android:layout_alignParentTop="true"
android:layout_marginTop="58dp" android:layout_marginStart="0dp"/>
<com.mancj.materialsearchbar.MaterialSearchBar
style="@style/MaterialSearchBarLight"
app:mt_speechMode="true"
app:mt_hint="Custom hint"
app:mt_maxSuggestionsCount="10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/searchBar" />
</RelativeLayout>
我添加了搜索栏过滤器来搜索列表视图中的任何项目,但仍然没有功能可以做到这一点
【问题讨论】:
-
您要搜索并将搜索结果应用到
ListView吗? -
是的。我想从列表视图示例中搜索项目,例如 youtu.be/ZuFC3C3NpX0 。但我想在我上面的代码上应用这个步骤。
-
请问有答案码吗
-
对不起Pablo,昨天太忙了,现在发布答案
-
我不知道如何在 Kotlin 中编码。但是,如果您了解 Java,那么也许这可能会对您有所帮助 stackoverflow.com/questions/27378981/…