我在活动中有一个回收视图。我无法显示列表项。
在我的活动中,我将适配器与arrayList有利CityList绑定。
活动:
recyclerView.apply {
layoutManager = LinearLayoutManager(this@SearchFavrouiteActivity)
city_list.adapter = com.example.diyatest.view.CityAdapter()
(adapter as CityAdapter).updateItems(favCitesList);
}
这是我的适配器。我正在UpdateItems ListView中传递列表。在调试时,我可以看到编译器在那里,但它不可见。看起来上下文有一些问题。
class CityAdapter() : RecyclerView.Adapter<CityAdapter.MyViewHolder>() {
val mCityData = ArrayList<WeatherResponseData>()
fun updateItems(list: ArrayList<WeatherResponseData>){
mCityData.clear()
mCityData.addAll(list)
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.content_search_favrouite, parent, false)
return MyViewHolder(view)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
mCityData[position].let {
holder.bind(mCityData = it)
}
}
override fun getItemCount(): Int {
return mCityData.size
}
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(mCityData: WeatherResponseData) {
itemView.city_Country_txt.text = mCityData.name
itemView.date_txt.text = (mCityData.date)?.toLong()?.let { getDate(it) }
itemView.temp_txt.text=mCityData.main?.temp.toString()
}
private fun getDate(date: Long): String {
val timeFormatter = SimpleDateFormat("dd.MM.yyyy")
return timeFormatter.format(Date(date*1000L))
}
}
}
这是我的活动main_Xml。在这里,我称之为回收者视图。
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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="wrap_content"
android:background="@drawable/bg_gradient"
tools:context=".view.SearchFavrouiteActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Saved Cities"
android:textSize="40dp"
android:gravity="center"
android:layout_gravity="center"
android:textColor="@android:color/white"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/city_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_marginTop="30dp"
android:background="@android:color/darker_gray"
android:gravity="center_horizontal"
/>
</LinearLayout>
这是我的适配器视图。在这里我使用cardview。
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:layout_marginLeft="@dimen/main_padding"
android:layout_marginTop="@dimen/main_padding"
android:layout_marginRight="@dimen/main_padding">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:padding="@dimen/main_padding">
<TextView
android:id="@+id/city_Country_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
tools:text="No City Data" />
<TextView
android:id="@+id/temp_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/city_Country_txt"
android:layout_marginTop="10dp"
tools:text="Degrees" />
<TextView
android:id="@+id/date_txt"
android:layout_width="@dimen/list_weather_icon_size"
android:layout_height="@dimen/list_weather_icon_size"
android:layout_below="@+id/temp_txt"
android:layout_alignParentRight="true"
android:contentDescription="@string/weather_icon_description"
android:text="date" />
</RelativeLayout>
我有建议你检查,让我知道,如果工作更改xml中的回收视图代码
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/city_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layout_marginTop="30dp"
/>
这可能是问题所在
city_list.apply {
layoutManager = LinearLayoutManager(this@SearchFavrouiteActivity,LinearLayoutManager.VERTICAL, false)
val cityAdapter = CityAdapter()
adapter = cityAdapter
cityAdapter.updateItems(favCitesList);
}