Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 提交时在片段上显示API响应_Android_Kotlin - Fatal编程技术网

Android 提交时在片段上显示API响应

Android 提交时在片段上显示API响应,android,kotlin,Android,Kotlin,当我在searchview上搜索时,我希望将搜索值发送到api,api将返回我希望在片段上显示的响应。因此,当我提交搜索时,我想显示带有响应的片段!我试图制作一个函数来渲染片段,但我认为我做得完全错误。。。 我是乞丐,这是学校的一个项目,谢谢你的帮助 搜索视图 override fun onCreateOptionsMenu(menu: Menu): Boolean { // Inflate the menu; this adds items to the action bar if

当我在searchview上搜索时,我希望将搜索值发送到api,api将返回我希望在片段上显示的响应。因此,当我提交搜索时,我想显示带有响应的片段!我试图制作一个函数来渲染片段,但我认为我做得完全错误。。。 我是乞丐,这是学校的一个项目,谢谢你的帮助

搜索视图

 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)

}
 }
类SendFragment:Fragment(){
var newList:MutableList=mutableListOf()
伴星{
fun newInstance()=SendFragment()
}
私有lateinit变量viewModel:SendViewModel
覆盖创建视图(
充气机,
容器:视图组?,
savedInstanceState:捆绑?
):查看{
返回充气机。充气(R.layout.fragment_搜索,容器,错误)
}
覆盖活动创建的乐趣(savedInstanceState:Bundle?){
super.onActivityCreated(savedInstanceState)
viewModel=ViewModelProviders.of(this.get)(SendViewModel::class.java)
recyclerView.setHasFixedSize(真)
recyclerView.layoutManager=LinearLayoutManager(this.context)
//TODO:使用ViewModel
val searchedObserver=观察者
{
//访问RecyclerView适配器并将数据加载到其中
newList->recyclerView.adapter=NewsAdapter(newList,this.context!!)
}
viewModel.getNewSearched().observe(此为searchedObserver)
}
}
片段视图模型

 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:")
        }
    })

}
 }
类SendViewModel:ViewModel(){
//TODO:实现ViewModel
私有变量newList:MutableLiveData=MutableLiveData()
fun getNewSearched():MutableLiveData
{
searchedNew()
返回newList;
}
私人娱乐搜索新()
{
val reformation=reformation.Builder()
.baseUrl(“http://192.168.1.78:3000")
.addConverterFactory(GsonConverterFactory.create())
.build()
VALAPI=reformation.create(ApiService::class.java)
val searchPost=searchPost(“这是您想要的查询”)
sendSearch(searchPost).enqueue(对象:回调{
覆盖fun onResponse(调用:调用,响应:响应){
newList.value=(response.body()!!)
}
覆盖失效时的乐趣(调用:调用,t:可丢弃){
Log.d(“fail”,“onFailure:”)
}
})
}
}
Api接口

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


  }
接口服务{
@帖子(“/搜索”)
趣味sendSearch(@Body searchPost:searchPost):呼叫
}

观察如下视图模型:

 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!!)
    })
最后,我建议您将文本传递给要搜索的viewmodel

override fun onQueryTextSubmit(query: String?): Boolean {

  viewMode.getNewSearched(query)  
在侧视图模型中:

fun getNewSearched(textYouWantToSearch :String): MutableLiveData<List<News>>
{
    searchedNew(textYouWantToSearch) // same pass in searchedNew() else your data class is always blank
    return newList;
}
fun getnewsearch(textYouwantSearch:String):MutableLiveData
{
searchedNew(textYouWantSearch)//在searchedNew()中执行相同的过程,否则您的数据类始终为空
返回newList;
}

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

观察如下视图模型:

 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!!)
    })
最后,我建议您将文本传递给要搜索的viewmodel

override fun onQueryTextSubmit(query: String?): Boolean {

  viewMode.getNewSearched(query)  
在侧视图模型中:

fun getNewSearched(textYouWantToSearch :String): MutableLiveData<List<News>>
{
    searchedNew(textYouWantToSearch) // same pass in searchedNew() else your data class is always blank
    return newList;
}
fun getnewsearch(textYouwantSearch:String):MutableLiveData
{
searchedNew(textYouWantSearch)//在searchedNew()中执行相同的过程,否则您的数据类始终为空
返回newList;
}

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

在不说明错误或当前行为的情况下发布所有代码是没有帮助的。但从我看到的情况来看,您似乎没有在
onquerytexsubmit(query:String?
中使用
query
。我解释过了!什么是错误的,当我提交我没有得到来自搜索的回应。。。这就是为什么我问如何将查询发送到改型,并显示当我提交时将显示api响应的片段。您的搜索视图处于片段或活动中?在不说明错误或当前行为的情况下发布所有代码是没有帮助的。但从我看到的情况来看,您似乎没有在
onquerytexsubmit(query:String?
中使用
query
。我解释过了!什么是错误的,当我提交我没有得到来自搜索的回应。。。这就是为什么我问如何将查询发送到改型,并显示当我提交时将显示api响应的片段。您的搜索视图处于片段或活动中?为什么是红色?-检查这一个太-只要尝试重建项目也让我知道,当你们运行项目你们得到编译时间错误?。如果我的回答有助于你投票,也请接受。如果仍然卡住,告诉我我会帮你的我做到了。但仍然是红色的表示未知引用。您能根据其studio错误向我展示全部viewmodel和片段吗,但让我看看您没有实例化viewmodel并直接尝试从viewmodel访问方法:Dothis viewmodel=ViewModelProviders.of(this.get)(SendViewModel::class.java)然后调用方法viewModel.getNewSearched(),它肯定会工作。为什么是红色检查这一个太-只要尝试重建项目也让我知道,当你们运行项目你们得到编译时间错误?。如果我的回答有助于你投票,也请接受。如果仍然卡住,告诉我我会帮你的我做到了。但仍然是红色的表示未知引用。您能根据其studio错误向我展示全部viewmodel和片段吗,但让我看看您没有实例化viewmodel并直接尝试从viewmodel访问方法:Dothis viewmodel=ViewModelProviders.of(this.get)(SendViewModel::class.java)然后调用方法viewModel.getNewSearched()