前提
初めてアプリ開発を行っており、Java初心者です。
一日がかりで調べたり格闘しましたがどうにもならなかったので皆様の知恵をお借りできればと思います。
ListViewを使って画像付きリストを表示し、リスト内をタップすれば詳細を表示できるアプリを作成しています。
文字のリストは出来たのですが、左側に画像を入れることができず困っています。
実現したいこと
リストの各項目左側に対応する画像を表示
以下のイメージでは1枚だけ画像を入れる事が出来たのですが、これをリスト1行ずつにそれぞれ入力したいです。
発生している問題・エラーメッセージ
Javaコードの "//画像" ~ "//リストの表示" について、
int[]で画像配列を指定すればArrayAdapter作成時にエラーとなり、
Integer[] で画像配列を指定すればsetImageResource作成時にエラーとなるため、
どのように配列内すべてを処理させるのかわかりませんでした。
該当のソースコード
MainActivity.java
1package com.example.myapptesut; 2 3import androidx.appcompat.app.AppCompatActivity; 4import android.app.AlertDialog; 5import android.media.Image; 6import android.os.Bundle; 7import android.view.View; 8import android.view.ViewGroup; 9import android.widget.AdapterView; 10import android.widget.ArrayAdapter; 11import android.widget.ListView; 12import android.widget.ImageView; 13 14import java.util.Collections; 15 16public class MainActivity extends AppCompatActivity { 17 //データ 18 String[] str = { 19 "ライオン", 20 "ネコ", 21 "シマウマ" 22 }; 23 24 //データ2 25 String[] context = { 26 "ライオンの説明", 27 "ネコの説明", 28 "シマウマの説明" 29 }; 30 31 //画像 32 Integer[] images = { 33 R.drawable.img25, 34 R.drawable.img16, 35 R.drawable.img61, 36 }; 37 38 @Override 39 protected void onCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 setContentView(R.layout.activity_main); 42 43 //アダプター 44 ArrayAdapter adapter = new ArrayAdapter( 45 this, 46 R.layout.list_item, 47 str 48 ); 49 50 ArrayAdapter adapter2 = new ArrayAdapter( 51 this, 52 R.layout.list_item, 53 images 54 ); 55 56 57 //リストの表示 58 ((ListView)findViewById(R.id.lv)).setAdapter(adapter); 59 ((ImageView)findViewById(R.id.ig)).setImageResource(adapter2); 60 61 //クリック処理 62 ((ListView)findViewById(R.id.lv)).setOnItemClickListener( 63 new AdapterView.OnItemClickListener() { 64 @Override 65 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 66 //クリックしたときのプログラム 67 new AlertDialog.Builder(MainActivity.this) 68 .setTitle(str[position]) 69 .setMessage(context[position]) 70 .setPositiveButton("yes",null) 71 .show(); 72 } 73 } 74 ); 75 } 76}
activity_main.xml
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:padding="6dp"> 8 9 <ImageView 10 android:id="@+id/ig" 11 android:layout_width="50dp" 12 android:layout_height="50dp" 13 tools:src="@tools:sample/avatars" 14 app:layout_constraintStart_toStartOf="parent" 15 app:layout_constraintTop_toTopOf="parent" /> 16 17 <ListView 18 android:id="@+id/lv" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 tools:text="動物名" 22 android:textSize="18sp" 23 android:layout_marginStart="6dp" 24 android:layout_marginLeft="6dp" 25 app:layout_constraintStart_toEndOf="@+id/ig" 26 app:layout_constraintTop_toTopOf="@+id/ig" /> 27 28</androidx.constraintlayout.widget.ConstraintLayout>
list_item.xml
1<?xml version="1.0" encoding="utf-8"?> 2<TextView xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:padding="16dp"> 6</TextView> 7
strings.xml
1<resources> 2 <string name="app_name">My Apptesut</string> 3</resources>
試したこと
そもそもリストに表示させる方法としてあっているのかもわからず、
strとimagesを1セットにまとめた方がいいのかと考えたり色々試行錯誤しましたが、
解決には至りませんでした。
よろしくお願いいたします。

回答2件
あなたの回答
tips
プレビュー