LoginSignup
0
1

More than 3 years have passed since last update.

AndroidのListViewに文字が表示されないとお怒りのあなたへ 続編(もう私は焦らない)==>SimpleAdapterの使い方

Last updated at Posted at 2020-10-15

1.きっかけ

 斎藤新三著『Androidアプリ開発の教書(Kotlin版)初版第2刷』のp150の「2行のリストとSimpleAdapter]を写経していた際、ListViewに文字が表示されない(正確には、表示されているが、バックグラウンドと(ほぼ)同色で見えない)事象を発見したので報告します。この事象は先にご紹介したArrayAdapterの使い方の場合と同様の原因と考えます。
 下の写真は、エミュレータAPIレベル19で動作させた状態です。
image.png

2.対策

対策は本書のリスト7.3のSimpleAdapterの第1引数をapplicationContextからthisに変更します。

//リスト7.3抜粋
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        ~省略~
        //SimpleAdapterを生成
        // (元)val adapter = SimpleAdapter(applicationContext, menuList, android.R.layout.simple_list_item_2, from, to)
        val adapter = SimpleAdapter(this, menuList, android.R.layout.simple_list_item_2, from, to)
        //applicationContextからthisに変更

        //アダプタの登録
        lvMenu.adapter = adapter

    }
        ~省略~
}

3.結果

見事に思い通り、可読できる表示になりました。
image.png

4.動作環境

Android Studio 4.1
Build #AI-201.8743.12.41.6858069, built on September 24, 2020
Runtime version: 1.8.0_242-release-1644-b01 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
GC: ParNew, ConcurrentMarkSweep
Memory: 1237M
Cores: 8
Registry: ide.new.welcome.screen.force=true
Non-Bundled Plugins: com.thoughtworks.gauge

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1