ViewModel没有出现

时间:2019-10-01 09:07:00

标签: android android-viewmodel

我是Kotlin的新手,并试图将ViewModel与改造一起使用来调用响应,问题是改造不会调用任何东西,因此什么也没有出现。

试图弄乱名为ViewModels的地方

试图从GSON生成新的POJO

在AndroidManifest中放入使用权限互联网

class MovieView(context: Context) : ViewModel() {

private val API_KEY: String = context.getString(R.string.api)
private val TAG = MovieView::class.java!!.getSimpleName()

private var resultsItem: MutableLiveData<List<ResultsItem>>? = null


fun getData(): LiveData<List<ResultsItem>> {
    if (resultsItem == null) {
        resultsItem = MutableLiveData()
        loadData()
    }
    return resultsItem as MutableLiveData<List<ResultsItem>>
}

fun loadData() {
    val apiInterface = ApiClient.getList().create(ApiInterface::class.java)

    val responseCall = apiInterface.getMovieList(API_KEY)
    responseCall.enqueue(object : Callback<Response> {
        override fun onResponse(call: Call<Response>, response: retrofit2.Response<Response>) {
            val resultsItemList = response.body()!!.results
            resultsItem?.value = resultsItemList
            Log.d(TAG,"response")
        }

        override fun onFailure(call: Call<Response>, t: Throwable) {
            Log.d(TAG, "null")
        }
    })
}

}

翻新的预期响应,但没有找到,也不存在null意味着甚至没有执行。

编辑:

class MovieFragment : Fragment() {

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

private lateinit var viewModel: MovieView

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {

    recycler.setHasFixedSize(true)
    recycler.layoutManager = LinearLayoutManager(context)
    recycler.adapter = MovieAdapter(viewModel.getData(), this.context!!)
    viewModel = ViewModelProviders.of(this).get(MovieView::class.java)

    return inflater.inflate(R.layout.movie_fragment, container, false)
}

}

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    MovieFragment movieFragment = new MovieFragment();

    getSupportFragmentManager().beginTransaction().add(R.id.framelayout, movieFragment);

1 个答案:

答案 0 :(得分:1)

resultsItem必须是活动/片段观察到的不可变属性,然后您只需要调用loadData()。这里甚至不需要fun getData()

对于未执行的呼叫。您确定什么都没有真正执行吗? onResponse还是onFailure都没有?在我看来,这是不可能的。也许将t.printStackTrace()放在onFailure内,然后在LogCat中查看是否有此堆栈跟踪。

编辑:

commit添加到MainActivity中的片段事务中,如下所示:

    getSupportFragmentManager()
        .beginTransaction()
        .add(R.id.framelayout, movieFragment)
        .commit();

另外,您还应该观察LiveData片段中的内容:

val adapter = MovieAdapter(this.context!!)
recycler.adapter = adapter
viewModel = ViewModelProviders.of(this).get(MovieView::class.java)
viewModel.resultsItem.observe(this, Observer { adapter.update(it) })
viewModel.loadData()

并为ViewModel添加工厂:

private val factory = object: ViewModelProvider.Factory {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return MovieView(this@MovieFragment.context!!) as T
    }
}

viewModel = ViewModelProviders.of(this, factory).get(MovieView::class.java)