繁体   English   中英

带有凌空库的回收器视图不显示项目(Kotlin)

[英]Recycler view with volley library in not showing items (Kotlin)

该应用程序运行良好 JSON 数据也已获取,但回收站视图未显示数据,即使回收站视图也未显示。

我在这里分享我的代码,请帮助我找出问题所在,

我的应用程序在编译时没有显示任何错误,所有应用程序都运行良好。

course_rv_item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
enter code herexmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
app:cardCornerRadius="5dp"
app:cardElevation="4dp">
    
<!--on below line we are creating a
linear layout for grid view item-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
    
<!--on below line we are creating a simple image view-->
<ImageView
android:id="@+id/idIVCourse"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_margin="8dp"
android:padding="5dp"
android:src="@mipmap/ic_launcher" />
    
<!--on below line we are creating a simple text view-->
<TextView
android:id="@+id/idTVCourse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:padding="4dp"
android:text="@string/app_name"
android:textAlignment="textStart"
android:textColor="@color/black"
android:textStyle="bold"
tools:ignore="RtlCompat" />
</LinearLayout>
</androidx.cardview.widget.CardView>

activity_contact.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ContactActivity">
    
<ProgressBar
android:id="@+id/idPBLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
    
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/idRVCourses"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
    
</RelativeLayout>

Contact_Activity.kt

import android.app.DownloadManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import org.json.JSONObject
import android.view.View
import android.widget.ProgressBar
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.toolbox.JsonArrayRequest
import com.denzcoskun.imageslider.models.SlideModel
    
import androidx.recyclerview.widget.LinearLayoutManager;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import org.json.JSONArray;
import org.json.JSONException;
import java.util.ArrayList;
    
class ContactActivity : AppCompatActivity() {
private val BASE_URL = "MyURL"
lateinit var courseRV: RecyclerView
lateinit var loadingPB: ProgressBar
lateinit var courseRVAdapter: CourseRVAdapter
lateinit var courseList: ArrayList<CourseRVModal>

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_contact)
    
// on below line we are initializing
// our variable with their ids.
courseRV = findViewById(R.id.idRVCourses)
loadingPB = findViewById(R.id.idPBLoading)
// on below line we are initializing our list
courseList = ArrayList()
    
// on below line we are initializing our adapter.
courseRVAdapter = CourseRVAdapter(courseList)
    
// on below line we are setting
// adapter to recycler view.
courseRV.adapter = courseRVAdapter
    
// on below line we are calling
// get data method to get data.
getData()
}
    
private fun getData() {
// on below line we are creating a variable for url
var url = BASE_URL + "getSlider.php"
    
// on below line we are creating a
// variable for our request queue
val queue = Volley.newRequestQueue(this@ContactActivity)
    
// on below line we are creating a request
// variable for making our json object request.
val request =
// as we are getting json object response and we are making a get request.
JsonArrayRequest(Request.Method.GET, url, null, { response ->
// this method is called when we get successful response from API.
loadingPB.visibility = View.GONE
try {
for (i in 0 until response.length()) {
// on below line we are extracting
// data from each json object
val respObj = response.getJSONObject(i)
val langName = respObj.getString("sliderTitle")
val langImg = respObj.getString("sliderImage")
    
// on below line we are adding data to our list
courseList.add(CourseRVModal(langName, langImg))
    
// on below line we are notifying
// our adapter that data has updated.
courseRVAdapter.notifyDataSetChanged()
}
    
// on below line we
// are handling exception
} catch (e: Exception) {
e.printStackTrace()
}
    
}, { error ->
// in this case we are simply displaying a toast message.
Toast.makeText(this@ContactActivity, "Fail to get response", Toast.LENGTH_SHORT)
.show()
})
// at last we are adding our
// request to our queue.
queue.add(request)
}
}

CourseRVModel.kt

data class CourseRVModal(
// on below line we are creating a two variable
// one for course name and other for course image.
var courseName: String,
var courseImg: String
)

CourseRVAdaptor.kt

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.squareup.picasso.Picasso
    
class CourseRVAdapter(
// on below line we are passing variables as course list and context
private var courseList: ArrayList<CourseRVModal>,
) : RecyclerView.Adapter<CourseRVAdapter.CourseViewHolder>() {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): CourseRVAdapter.CourseViewHolder {
// this method is use to inflate the layout file
// which we have created for our recycler view.
// on below line we are inflating our layout file.
val itemView = LayoutInflater.from(parent.context).inflate(
R.layout.course_rv_item,
parent, false
)
// at last we are returning our view
// holder class with our item View File.
return CourseViewHolder(itemView)
}
    
override fun onBindViewHolder(holder: CourseRVAdapter.CourseViewHolder, position: Int) {
// on below line we are setting data to our text view and our image view.
holder.courseNameTV.text = courseList.get(position).courseName
Picasso.get().load(courseList.get(position).courseImg).into(holder.courseIV)
}
    
override fun getItemCount(): Int {
// on below line we are returning
// our size of our list
return courseList.size
}
    
class CourseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
// on below line we are initializing our course name
// text view and our image view.
val courseNameTV: TextView = itemView.findViewById(R.id.idTVCourse)
val courseIV: ImageView = itemView.findViewById(R.id.idIVCourse)
}
}

build.gradle

implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.5.0'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.google.firebase:firebase-database:20.0.6'
implementation 'com.google.firebase:firebase-config-ktx:21.1.2'
implementation 'com.google.firebase:firebase-config:21.1.2'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.github.denzcoskun:ImageSlideshow:0.1.0'
implementation("com.android.volley:volley:1.2.1")
implementation 'com.squareup.picasso:picasso:2.71828'

activity_contact.xml您的回收站视图idRVCourses具有属性android:visibility="gone" 所以当屏幕第一次加载时它是隐藏的。 加载数据后,您需要使其可见。 为此,在getData function 中加载courseRV.visibility = View.GONE后添加 courseRV.visibility loadingPB.visibility = View.GONE View.GONE。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM