前提・実現したいこと
AndroidstudioでListViewを使ってAssets内の画像と名前のリストを出したいのですが、画像が表示されません
有識者の方何かご助力お願いいたします
ソースコード
MakeQuizMenu.java
1public class MakeQuizMenu extends AppCompatActivity { 2 3 private ArrayList<String> Filename ; 4 public int Difficult = 0;//0=ひら 1=カナ 2=漢字 5 public String[] DifData = {"ひらがな","カタカナ","漢字"}; 6 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_make_menu); 12 13 getFilename(); 14 15 // Mapのキー 16 final String[] FROM = {"title","name","image"}; 17 // リソースのコントロールID 18 final int[] TO = {R.id.textView, R.id.textView2,R.id.imageView}; 19 20 ListView listview = findViewById(R.id.list_view); 21 AssetManager assets = getResources().getAssets(); 22 23 // リストデータの生成 24 List<Map<String,Object>> list = new ArrayList<>(); 25 Map<String,Object> map; 26 27 for(int i = 0;i < Filename.size();i++ ) { 28 map = new HashMap<>(); 29 map.put("title", "NO." + i); 30 try { 31 map.put("name", Filename.get(i)); 32 map.put("image",assets.open(DifData[Difficult]+"/Picture/" + Filename.get(i)+".png")); 33 } catch (IOException e) { 34 map.put("name", "読み込みエラー"); 35 } 36 list.add(map); 37 } 38 39 // アダプターの設定 40 SimpleAdapter adapter = new SimpleAdapter(MakeQuizMenu.this,list,R.layout.list,FROM,TO); 41 listview.setAdapter(adapter); 42 43 // リストクリック時のイベント 44 listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 45 @Override 46 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 47 48 } 49 }); 50 }; 51 52 public void getFilename(){ 53 54 AssetManager assetMgr = getResources().getAssets(); 55 String[] getFilelist = new String[100]; 56 Filename = new ArrayList<String>(); 57 int x = 0; 58 59 //フォルダ内のファイルの名前をgetFilelistに読み込み 60 try { 61 String files[] = assetMgr.list(DifData[Difficult]+"/Picture"); 62 63 for (int i = 0; i < files.length; i++) { 64 getFilelist[i] = files[i]; 65 x++; 66 } 67 } catch (IOException e) { 68 } 69 70 //拡張子を削除したのをFilenameに代入 71 for (int i = 0; i < x; i++) { 72 Filename.add(getFilelist[i].substring(0, getFilelist[i].lastIndexOf('.'))); 73 } 74 Filename.removeAll(Collections.singleton(null));//null削除 75 } 76}
activity_make_menu.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 tools:context=".MakeQuizMenu"> 8 9 <ListView 10 android:id="@+id/list_view" 11 android:layout_width="390dp" 12 android:layout_height="462dp" 13 app:layout_constraintBottom_toBottomOf="parent" 14 app:layout_constraintHorizontal_bias="0.027" 15 app:layout_constraintLeft_toLeftOf="parent" 16 app:layout_constraintRight_toRightOf="parent" 17 app:layout_constraintTop_toTopOf="parent" 18 app:layout_constraintVertical_bias="0.978" /> 19 20</androidx.constraintlayout.widget.ConstraintLayout>
list.xml
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout 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:orientation="vertical" android:layout_width="match_parent" 6 android:layout_height="match_parent"> 7 8 <TextView 9 android:id="@+id/textView" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:text="TextView" /> 13 14 <LinearLayout 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:orientation="horizontal"> 18 19 <ImageView 20 android:id="@+id/imageView" 21 android:layout_width="100dp" 22 android:layout_height="100dp" 23 android:layout_weight="1" 24 app:srcCompat="@android:drawable/btn_star_big_on" 25 tools:srcCompat="@tools:sample/avatars" /> 26 27 <TextView 28 android:id="@+id/textView2" 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" 31 android:layout_marginTop="6dp" 32 android:layout_weight="1" 33 android:text="TextView" /> 34 </LinearLayout> 35</LinearLayout>
補足情報(FW/ツールのバージョンなど)
Androidstudioのバージョン
Android Studio Arctic Fox | 2020.3.1 Patch 4
Build #AI-203.7717.56.2031.7935034, built on November 21, 2021
ランタイム・バージョン: 11.0.10+0-b96-7249189 amd64
VM: OpenJDK 64-Bit Server VM by Oracle Corporation
Windows 10 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 1280M
Cores: 6
Registry: external.system.auto.import.disabled=true
Non-Bundled Plugins: org.jetbrains.kotlin
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー