Я пишу свое первое приложение для планирования Android. У меня есть навигационный ящик и WeekListFragment. Во фрагменте я создаю массив объектов WeekDay и пытаюсь отобразить их через WeekAdapter. Но при запуске эмулятора Android ничего не отображается, хотя открывается фрагмент. Можете ли вы объяснить мне, что я должен делать?
Вот код этого фрагмента:
package itshpit.com.myapplication
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import itshpit.com.myapplication.classes.WeekDay
class WeekListFragment() : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view: View? = inflater.inflate(R.layout.fragment_week, container, false)
(activity as MainActivity).supportActionBar!!.title = "Week"
val week: Array<String> = resources.getStringArray(R.array.week)
val weekList: ArrayList<WeekDay> = ArrayList()
weekList.add(WeekDay(text = "some Text", image = R.drawable.mon, id = 1))
weekList.add(WeekDay(text = "some Text", image = R.drawable.mon, id = 2))
weekList.add(WeekDay(text = "some Text", image = R.drawable.mon, id = 3))
weekList.add(WeekDay(text = "some Text", image = R.drawable.mon, id = 4))
weekList.add(WeekDay(text = "some Text", image = R.drawable.mon, id = 5))
weekList.add(WeekDay(text = "some Text", image = R.drawable.mon, id = 6))
weekList.add(WeekDay(text = "some Text", image = R.drawable.mon, id = 7))
val adapter = WeekAdapter((activity as MainActivity), weekList)
return view
}
class WeekAdapter(context: Context, val weekList: List<WeekDay>) : ArrayAdapter<WeekDay>(context, 0, weekList) {
private class ViewHolder(row: View?) {
var txtName: TextView
var ivImage: ImageView
init {
this.txtName = row?.findViewById(R.id.txtWeekDay) as TextView
this.ivImage = row.findViewById(R.id.imgWeekDay) as ImageView
}
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view: View?
val viewHolder: ViewHolder
if (convertView == null) {
val layout = LayoutInflater.from(context)
view = layout.inflate(R.layout.week_item, parent, false)
viewHolder = ViewHolder(view)
view.tag = viewHolder
} else {
view = convertView
viewHolder = view.tag as ViewHolder
}
val weekDay: WeekDay = getItem(position) as WeekDay
viewHolder.txtName.text = weekDay.text
viewHolder.ivImage.setImageResource(weekDay.image)
return view as View
}
override fun getItemId(position: Int): Long {
return weekList[position].id
}
override fun getItem(position: Int): WeekDay? {
return weekList.get(position)
}
override fun getCount(): Int {
return 7
}
}
}
Код класса WeekDay приведен ниже:
data class WeekDay(var id: Long = -1,
var date: Date? = null,
var text: String,
var image: Int
) {}
"frag_week.xml"файл:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".WeekListFragment">
<ListView
android:id="@+id/weekList"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>