【发布时间】:2021-10-22 01:32:15
【问题描述】:
我不明白为什么列表没有在自定义适配器的帮助下填充。只有数组中的最后一个元素进入 textView (textHeroView)。事实证明,ListView 并没有作为一个列表,而只是从数组中取出一项。我试图将它放在一个单独的列表中,但无济于事。你能告诉我我犯了什么错误吗?
HeroesAdapter
class HeroesAdapter(context: Context, heroes: List<TestHero>): BaseAdapter() {
private val context = context
private val heroes = heroes
override fun getCount(): Int {
return heroes.count()
}
override fun getItem(position: Int): Any {
return heroes[position]
}
override fun getItemId(position: Int): Long {
return 0
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
// categoryView = LayoutInflater.from(context).inflate(R.layout.activity_heroes, null)
val listheroView = LayoutInflater.from(context).inflate(R.layout.list_hero_view, parent, false)
// val categoryImage: ImageView = categoryView.findViewById(R.id.heroesImageView)
val heroText: TextView = listheroView.findViewById(R.id.textHeroView)
val category = heroes[position]
// heroText.text = category.legends.all.keys.first()
for((key) in category.legends.all){
Log.d("HeroesActivity", key)
heroText.text = key
}
return listheroView
}
}
list_hero_view
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
>
<ImageView
android:id="@+id/heroesImageView"
android:layout_width="426dp"
android:layout_height="180dp"
android:layout_marginTop="24dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.533"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@mipmap/wraith_apex_legends" />
<TextView
android:id="@+id/textHeroView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wraith"
android:textColor="@android:color/darker_gray"
android:textSize="54sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/heroesImageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.12"
app:layout_constraintStart_toStartOf="@+id/heroesImageView"
app:layout_constraintTop_toTopOf="@+id/heroesImageView"
app:layout_constraintVertical_bias="0.04000002" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_heroes
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".HeroesActivity">
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="24dp"
app:layout_constraintBottom_toTopOf="@+id/heroesListView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher_round" />
<ListView
android:id="@+id/heroesListView"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="1dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textNickname"
android:layout_width="251dp"
android:layout_height="29dp"
android:layout_marginStart="40dp"
android:layout_marginEnd="279dp"
android:layout_marginBottom="60dp"
android:text="Nickname"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/heroesListView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.145"
app:layout_constraintStart_toEndOf="@+id/imageView2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.272" />
<TextView
android:id="@+id/textLvl"
android:layout_width="111dp"
android:layout_height="19dp"
android:layout_marginEnd="28dp"
android:layout_marginBottom="42dp"
android:text="Lvl"
app:layout_constraintBottom_toTopOf="@+id/heroesListView"
app:layout_constraintEnd_toEndOf="@+id/textNickname"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/textNickname"
app:layout_constraintTop_toBottomOf="@+id/textNickname"
app:layout_constraintVertical_bias="0.562" />
</androidx.constraintlayout.widget.ConstraintLayout>
HeroesActivity
class HeroesActivity : AppCompatActivity() {
lateinit var heroesAdapt : HeroesAdapter
val listHero = ArrayList<TestHero>()
private val TAG = "HeroesActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_heroes)
getCurrentData()
val lisView : ListView = findViewById(R.id.heroesListView)
heroesAdapt = HeroesAdapter(this, listHero)
lisView.adapter = heroesAdapt
//heroesListView.adapter = heroesAdapt
// heroesAdapt = HeroesAdapter(this, arrayListOf("a","b","c","d","e","f"));
// lisView.adapter = heroesAdapt
}
private fun getCurrentData() {
val api = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(ApiRequest::class.java)
GlobalScope.launch(Dispatchers.IO) {
val response = api.herList().awaitResponse()
if (response.isSuccessful) {
val data = response.body()!!
Log.d(TAG, data.toString())
withContext(Dispatchers.Main){
// adapter.add(data.global.name)
// adapt.add(data.global.platform)
listHero.add(data)
heroesAdapt.notifyDataSetChanged()
textNickname.text = "${data.global.name} (${data.global.rank.rankDiv} divisions)"
textLvl.text = "Level: ${data.global.level.toString()}"
when(data.global.rank.rankName){
"Silver" -> textNickname.setTextColor(Color.parseColor("#7A7A79"))
"Gold" -> textNickname.setTextColor(Color.parseColor("#E6D600"))
"Platinum" -> textNickname.setTextColor(Color.parseColor("#36BBCE"))
}
}
}
}
}
}
我认为问题出在这段代码中
for((key) in category.legends.all){
Log.d("HeroesActivity", key)
heroText.text = key
}
和日志屏幕
TestHero.kt
data class TestHero (@SerializedName("global") val global: PlayerInf,
@SerializedName("legends")val legends: AllLegends)
data class PlayerInf (val name: String, val uid: Long, val avatar: String, val platform: String,
val level: Int, val toNextLevelPercent: Int, val internalUpdateCount: Int, val bans: BanInf, val rank: RankInf)
data class BanInf (val isActive: Boolean, val remainingSeconds: Int)
data class RankInf (val rankScore: Int, val rankName: String, val rankDiv: Int, val rankImg: String)
data class AllLegends (@SerializedName("all") val all: Map<String, LegendWrapper> = emptyMap())
data class LegendWrapper(
val data: List<PlayerPerformance>? = emptyList()
)
data class PlayerPerformance(val name: String, val value: Int, val key: String)
【问题讨论】:
-
@Tenfour04 我能做到
return position.toLong()吗?因为如果我没有定义这个参数,我会得到错误 -
@Tenfour04 我希望这不会影响我的适配器的性能?
-
关于您新共享的代码:您只在列表中放入了一条数据,因此它当然只有一个行项目。另外,不要在适配器中使用常量可变列表,因为当您调用
notifyDataSetChanged()时,它无法在新旧列表之间进行比较。此外,如果您在不调用notifyDataSetChanged()的情况下修改列表,它可能会崩溃或显示重复项目或跳过项目,因为它不知道列表大小已更改。并且永远不要使用 GlobalScope(你可以谷歌为什么不使用)。您应该在 ViewModel 中执行此操作并使用viewModelScope。 -
@Tenfour04 非常感谢您,我一定会阅读并应用您的所有建议,在学习阶段,像您这样的专家的任何建议对我都有帮助
-
我收回我所说的关于 NO_ID 的话。我没有意识到您使用的是 ArrayAdapter 和 ListView 而不是 Adapter 和 RecyclerView。只需返回位置即可。
标签: android kotlin android-activity