android - RecyclerView:电影图像不显示
问题描述
我正在尝试使用 asynctaskloader 获取数据并使用 recyclerview 在片段中显示图像。以前我这样做过,然后我没有收到错误。我不确定为什么会这样。
但我的形象不是 daisplaying
当我使用毕加索时,这是我在 logcat 中得到的
D/Picasso: Main created [R4] Request{http://image.tmdb.org/t/p/w342/8UlWHLMpgZm9bx6QYh0NFoq67TZ.jpg}
D/Picasso: Dispatcher enqueued [R4]+1ms
D/Picasso: Hunter executing [R4]+2ms
D/Picasso: Main created [R5] Request{http://image.tmdb.org/t/p/w342/dWSnsAGTfc8U27bWsy2RfwZs0Bs.jpg}
D/Picasso: Dispatcher enqueued [R5]+1ms
D/Picasso: Hunter executing [R5]+1ms
D/EGL_emulation: eglMakeCurrent: 0xddc5e660: ver 2 0 (tinfo 0xe3e7ec40)
D/EGL_emulation: eglMakeCurrent: 0xddc5e660: ver 2 0 (tinfo 0xe3e7ec40)
D/Picasso: Dispatcher retrying [R4]+612ms
D/Picasso: Hunter executing [R4]+612ms
D/Picasso: Dispatcher retrying [R5]+609ms
D/Picasso: Hunter executing [R5]+609ms
D/Picasso: Dispatcher retrying [R5]+1167ms
D/Picasso: Hunter executing [R5]+1168ms
D/Picasso: Dispatcher retrying [R4]+1175ms
D/Picasso: Hunter executing [R4]+1176ms
D/Picasso: Dispatcher batched [R5]+1171ms for error
D/Picasso: Dispatcher batched [R4]+1179ms for error
D/Picasso: Dispatcher delivered [R5]+1413ms, [R4]+1419ms
D/Picasso: Main errored [R5]+1413ms HTTP 504
D/Picasso: Main errored [R4]+1419ms HTTP 504
片段类:
package com.example.srilakshmibomireddy.movies
import android.content.Context
import android.content.Context.INPUT_METHOD_SERVICE
import android.net.ConnectivityManager
import android.net.Uri
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.content.Loader
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import org.json.JSONException
import org.json.JSONObject
import java.util.ArrayList
class Popular : Fragment(),android.support.v4.app.LoaderManager.LoaderCallbacks<String>{
val popular:String = "popular"
lateinit var recyclerView: RecyclerView
lateinit var layoutManager:GridLayoutManager
val list = ArrayList<MoviesData>()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view: View = inflater.inflate(R.layout.fragment_popular, container, false)
val inputMethodManager = activity!!.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
val connectivityManager = activity!!.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val networkInfo = connectivityManager.activeNetworkInfo
if (networkInfo != null && networkInfo.isConnected && activity != null) {
GetMovies(context!!,popular)
} else {
Toast.makeText(activity, "please check network connectivity", Toast.LENGTH_SHORT).show()
}
recyclerView = view.findViewById(R.id.popular_posters)
layoutManager = GridLayoutManager(context, 2)
layoutManager.setOrientation(GridLayoutManager.VERTICAL)
recyclerView.layoutManager = layoutManager
recyclerView.setHasFixedSize(true)
val moviesadapter = MoviesAdapter(context!!)
moviesadapter.movieslist = list
recyclerView.adapter = moviesadapter
loaderManager.initLoader(0, null, this)
return view
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val connectivityManager = activity!!.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val networkInfo = connectivityManager.activeNetworkInfo
if (networkInfo != null && networkInfo.isConnected) {
GetMovies(context!!,popular)
} else {
Toast.makeText(activity, "please check network connectivity", Toast.LENGTH_SHORT).show()
}
}
override fun onCreateLoader(p0: Int, p1: Bundle?): Loader<String> {
return GetMovies(context!!,popular)
}
override fun onLoaderReset(p0: Loader<String>) {
}
override fun onLoadFinished(p0: Loader<String>, data: String?) {
val moviesadapter = MoviesAdapter(context!!)
if (data == null) {
Toast.makeText(context, "No Data Fetched", Toast.LENGTH_SHORT).show()
return
}
try {
val jsonObject = JSONObject(data)
val jsonArray1 = jsonObject.getJSONArray("results")
for (i in 0 until jsonArray1.length()) {
val jsonObject1 = jsonArray1.getJSONObject(i)
val title = jsonObject1.getString("title")
val popularity = jsonObject1.getString("popularity")
val poster = jsonObject1.optString("poster_path").toString()
val orgTitle = jsonObject1.getString("original_title")
val release = jsonObject1.getString("release_date")
val overview = jsonObject1.getString("overview")
val rating = jsonObject1.getString("vote_average")
val id = jsonObject1.getString("id")
val moviesdata = MoviesData(title, popularity, poster, orgTitle, release, overview, rating, id)
list.add(moviesdata)
}
moviesadapter.movieslist = list
recyclerView.adapter = moviesadapter
} catch (e: JSONException) {
e.printStackTrace()
}
}
}
片段资源文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context="com.example.srilakshmibomireddy.moviesdetailsapp.Fragments.Popular">
<android.support.v7.widget.RecyclerView
app:layoutManager="android.support.v7.widget.GridLayoutManager"
android:id="@+id/popular_posters"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
我的适配器:
package com.example.srilakshmibomireddy.movies
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import com.squareup.picasso.Picasso
class MoviesAdapter : RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {
var context: Context
var movieslist: List<MoviesData>? = null
set(value) {
field = value
}
constructor(context: Context) : super() {
this.context = context
}
override fun getItemCount(): Int {
return movieslist!!.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
var moviedata : MoviesData = movieslist!!.get(position)
var s : String ="http://image.tmdb.org/t/p/w342".plus(moviedata.poster)
Toast.makeText(context,s,Toast.LENGTH_SHORT).show()
Picasso.get().load(s).into(holder.movie_poster)
}
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MyViewHolder {
var view :View = LayoutInflater.from(p0.context).inflate(R.layout.movies_adapter,p0,false)
return MyViewHolder(view)
}
class MyViewHolder(view :View) :RecyclerView.ViewHolder(view) {
var movie_poster : ImageView = view.findViewById(R.id.movie_poster)
}
}
适配器资源文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/movie_poster"
android:layout_width="match_parent"
android:src="@mipmap/ic_launcher"
android:layout_height="300dp"
android:padding="2dp"
android:scaleType="fitXY"
android:contentDescription="@string/movie_poster"/>
</LinearLayout>
有人可以帮帮我吗 ?
解决方案
当我不得不在图像链接中使用“Https”时,我正在使用“Http”
改变
var s : String ="http://image.tmdb.org/t/p/w342".plus(moviedata.poster)
至
var s : String ="https://image.tmdb.org/t/p/w342".plus(moviedata.poster)
解决了我的问题
推荐阅读
- c++ - c++ 类中私有属性的问题
- javascript - 具有寻路网格生成的边缘情况的困难
- cmake - cmake3 继续使用旧的 CXXFLAGS 值
- angular - Angular:如何将服务连接到 Material RankTable?
- r - 如何在具有相同列表的 R 中使用嵌套的 for 循环?
- python - 自定义损失函数不等于用于训练模型的损失函数
- python - 使用 txt 文件检索问题和多个答案
- arrays - python数组中没有
- reactjs - React Grid Layout:如何让 React Grid Layout 与 React Bootstrap 4 组件一起使用
- python - While 循环倒计时到零