gpt4 book ai didi

android - 在提交 fragment 时显示API响应

转载 作者:行者123 更新时间:2023-12-02 13:19:38 27 4
gpt4 key购买 nike

当我在searchview上搜索时,我想将搜索值发送到api,它将返回一个我想在片段上显示的响应。因此,当我提交搜索时,我想显示带有响应的片段!我试图制作一个函数来呈现片段,但我认为即时通讯完全错误...
我是乞gg,这是一个学校项目,谢谢您的帮助!

SearchView

 override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)

val manager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
val searchItem = menu.findItem(R.id.action_search)
val searchView = searchItem?.actionView as SearchView

searchView.setSearchableInfo(manager.getSearchableInfo(componentName))

searchView.setOnQueryTextListener(object: SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String?): Boolean {
searchView.clearFocus()
searchView.setQuery("",false)
searchItem.collapseActionView()
return true
}

override fun onQueryTextChange(newText: String?): Boolean {
return false
}
})
return true
}

数据类
 data class SearchPost(val searchKey: String)

片段
  class SendFragment : Fragment() {

var newList: MutableList<News> = mutableListOf<News>()

companion object {
fun newInstance() = SendFragment()
}

private lateinit var viewModel: SendViewModel

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_searched, container, false)
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(this).get(SendViewModel::class.java)



recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(this.context)


// TODO: Use the ViewModel

val searchedObserver = Observer<List<News>>
{
// Access the RecyclerView Adapter and load the data into it
newList -> recyclerView.adapter = NewsAdapter(newList,this.context!!)

}

viewModel.getNewSearched().observe(this, searchedObserver)

}
}

片段 View 模型
 class SendViewModel : ViewModel() {

// TODO: Implement the ViewModel

private var newList: MutableLiveData<List<News>> = MutableLiveData()



fun getNewSearched(): MutableLiveData<List<News>>
{

searchedNew()

return newList;

}

private fun searchedNew()
{
val retrofit = Retrofit.Builder()
.baseUrl("http://192.168.1.78:3000")
.addConverterFactory(GsonConverterFactory.create())
.build()

val api = retrofit.create(ApiService::class.java)
val searchPost = SearchPost("this is want to be the query")
api.sendSearch(searchPost).enqueue(object : Callback<List<News>> {
override fun onResponse(call: Call<List<News>>, response: Response<List<News>>) {
newList.value=(response.body()!!)
}

override fun onFailure(call: Call<List<News>>, t: Throwable) {
Log.d("fail", "onFailure:")
}
})

}
}

Api界面
interface ApiService {
@POST("/search")
fun sendSearch(@Body searchPost: SearchPost): Call<List<News>>


}

最佳答案

观察这样的 View 模型:

 viewModel.getNewSearched().observe(this, Observer<MutableList<List<News>>> {
myNewsData ->
Log.d("print my data", myNewsData) // first try to print this data whether data is coming or not
recyclerView.adapter = NewsAdapter( myNewsData ,this.context!!)
})

初始化您的searchView:
private fun initSearchView() {
search_view.setOnQueryTextListener(object :
androidx.appcompat.widget.SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(newText: String?): Boolean {
viewMode.getNewSearched() // call your api here

return false // don,t change it to true
}

override fun onQueryTextChange(query: String?): Boolean {
return false
}
})
}

最后,我应该将文本传递给要搜索的viewmodel
override fun onQueryTextSubmit(query: String?): Boolean {

viewMode.getNewSearched(query)

在侧 View 模型中:
fun getNewSearched(textYouWantToSearch :String): MutableLiveData<List<News>>
{
searchedNew(textYouWantToSearch) // same pass in searchedNew() else your data class is always blank
return newList;
}

据我了解,您要在改造调用中传递的数据类始终为空,因为您没有从任何地方传递任何值,您应该使用搜索方法的查询字符串并尝试将其作为参数传递。

关于android - 在提交 fragment 时显示API响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59596592/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com