我正在创建一个新闻应用程序,在这个应用程序中,我通过从 Volley 库到 URL 的 API 调用获取新闻数据。
我的应用正在启动,但在视图中没有显示任何内容。
这是MainActivity.kt
for f in $(git ls-files) ; do touch -d $(git log -1 --format='%aI' "$f") "$f" ; done
这是 NewsAdapter.kt
package com.arne.khabriapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.Request
import com.android.volley.toolbox.JsonObjectRequest
class MainActivity : AppCompatActivity(), NewsItemClicked {
private val mAdapter: NewsListAdapter by lazy{
NewsListAdapter(this)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val recyclerView = findViewById<RecyclerView>(R.id.rView)
recyclerView.layoutManager = LinearLayoutManager(this)
fetchData()
recyclerView.adapter = mAdapter
}
private fun fetchData() {
val url = "http://newsapi.org/v2/top-headlines?country=in&apiKey=9bb7bf6152d147ad8ba14cd0e7452f2f"
val jsonObjectRequest = JsonObjectRequest(
Request.Method.GET,
url,
null,
{
val newsJsonArray = it.getJSONArray("articles")
val newsArray = ArrayList<NewsClass>()
for (i in 0 until newsJsonArray.length()){
val newsJsonObject = newsJsonArray.getJSONObject(i)
val news =NewsClass(
newsJsonObject.getString("title"),
newsJsonObject.getString("author"),
newsJsonObject.getString("url"),
newsJsonObject.getString("urlToImage")
)
newsArray.add(news)
}
mAdapter.updateNews(newsArray)
},
{
}
)
MySingletonClass.getInstance(this).addRequestQueue(jsonObjectRequest)
}
override fun OnItemClick(item: NewsClass) {
}
}
这是 Logcat 没有显示任何错误,但我不知道为什么会出现此错误 -
package com.arne.khabriapp
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
//class Name: extendItForRecyclerView.Adapter<ViewHolderClass>
class NewsListAdapter( private val listener: NewsItemClicked): RecyclerView.Adapter<NewsViewHolder>()
{
private val items: ArrayList<NewsClass> = ArrayList()
// call me when each time one viewholder is created
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewsViewHolder {
//Layout Inflater - Converts XML into View
val view = LayoutInflater.from(parent.context).inflate(R.layout.itemnews, parent, false)
val viewHolder = NewsViewHolder(view)
view.setOnClickListener{
//this is a call back to main activity so the main activity knows that any item is clicked and then the mainn activity behave according to this.
listener.OnItemClick(items[viewHolder.adapterPosition])
}
return viewHolder
}
// I will bind the item with viewholder
override fun onBindViewHolder(holder: NewsViewHolder, position: Int) {
val currentItem = items[position]
holder.titleView.text = currentItem.title
}
// call me only one time i will give you how many item will be here in list
override fun getItemCount(): Int {
return items.size
}
fun updateNews(updatedNews: ArrayList<NewsClass>){
items.clear()
items.addAll(updatedNews)
notifyDataSetChanged()
}
}
class NewsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
val titleView: TextView = itemView.findViewById(R.id.title)
}
interface NewsItemClicked{
fun OnItemClick(item: NewsClass)
}