前提・実現したいこと
AndroidstudioでListViewを使ってAssets内の画像と名前のリストを出したいのですが、画像が表示されません
有識者の方何かご助力お願いいたします
ソースコード
MakeQuizMenu.java
public class MakeQuizMenu extends AppCompatActivity { private ArrayList<String> Filename ; public int Difficult = 0;//0=ひら 1=カナ 2=漢字 public String[] DifData = {"ひらがな","カタカナ","漢字"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_make_menu); getFilename(); // Mapのキー final String[] FROM = {"title","name","image"}; // リソースのコントロールID final int[] TO = {R.id.textView, R.id.textView2,R.id.imageView}; ListView listview = findViewById(R.id.list_view); AssetManager assets = getResources().getAssets(); // リストデータの生成 List<Map<String,Object>> list = new ArrayList<>(); Map<String,Object> map; for(int i = 0;i < Filename.size();i++ ) { map = new HashMap<>(); map.put("title", "NO." + i); try { map.put("name", Filename.get(i)); map.put("image",assets.open(DifData[Difficult]+"/Picture/" + Filename.get(i)+".png")); } catch (IOException e) { map.put("name", "読み込みエラー"); } list.add(map); } // アダプターの設定 SimpleAdapter adapter = new SimpleAdapter(MakeQuizMenu.this,list,R.layout.list,FROM,TO); listview.setAdapter(adapter); // リストクリック時のイベント listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } }); }; public void getFilename(){ AssetManager assetMgr = getResources().getAssets(); String[] getFilelist = new String[100]; Filename = new ArrayList<String>(); int x = 0; //フォルダ内のファイルの名前をgetFilelistに読み込み try { String files[] = assetMgr.list(DifData[Difficult]+"/Picture"); for (int i = 0; i < files.length; i++) { getFilelist[i] = files[i]; x++; } } catch (IOException e) { } //拡張子を削除したのをFilenameに代入 for (int i = 0; i < x; i++) { Filename.add(getFilelist[i].substring(0, getFilelist[i].lastIndexOf('.'))); } Filename.removeAll(Collections.singleton(null));//null削除 } }
activity_make_menu.xml
<?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=".MakeQuizMenu"> <ListView android:id="@+id/list_view" android:layout_width="390dp" android:layout_height="462dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.027" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.978" /> </androidx.constraintlayout.widget.ConstraintLayout>
list.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/imageView" android:layout_width="100dp" android:layout_height="100dp" android:layout_weight="1" app:srcCompat="@android:drawable/btn_star_big_on" tools:srcCompat="@tools:sample/avatars" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="6dp" android:layout_weight="1" android:text="TextView" /> </LinearLayout> </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
ここにより詳細な情報を記載してください。
まだ回答がついていません
会員登録して回答してみよう