前提・実現したいこと
AndroidStudioで ListViewにアダプタクラスでリストデータを表示させ、各リストの行をクリックするとその行の余白にチェック(画像)が表示されるようにしたいです。
発生している問題・エラーメッセージ
SimpleAdapterを使って独自にレイアウトしたファイルを指定しています。
個人的にはタップした行にsetImageResoure()を使って表示させようとしたのですが、それではタップした行を認識できず、タップしたらその分上から画像が表示されていきます。おまけに、ほかの行をタップした時、消す手段も思いつかないので、ほかの方法を模索しています。。
該当のソースコード
Java
1public class MainActivity extends AppCompatActivity { 2 3 private List<Map<String,String>> TimeZoneList; 4 private static final String[] FROM={"minTime","maxTime"}; 5 private static final int[] TO={R.id.tvMinTime,R.id.tvMaxTime}; 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_main); 11 12 //TimeZone(リスト画面上側)を表示させる 13 ListView lvTimeZone=findViewById(R.id.lvTimeZone); 14 TimeZoneList=createTimeZoneList(); 15 SimpleAdapter adapter=new SimpleAdapter(MainActivity.this,TimeZoneList,R.layout.timezone_list,FROM,TO); 16 lvTimeZone.setAdapter(adapter); 17 18 //TimeZoneリストをタップした時の処理 19 lvTimeZone.setOnItemClickListener(new ListTimeZoneClickListener()); 20 } 21 22 private List<Map<String,String>> createTimeZoneList(){ 23 24 List<Map<String,String>> timeZoneList=new ArrayList<>(); 25 //HashMapでMaxTime〜MinTimeを作成 26 Map<String,String> timeZone=new HashMap<>(); 27 timeZone.put("minTime","12:00"); 28 timeZone.put("maxTime","12:30"); 29 timeZoneList.add(timeZone); 30 31 timeZone=new HashMap<>(); 32 timeZone.put("minTime","12:31"); 33 timeZone.put("maxTime","13:00"); 34 timeZoneList.add(timeZone); 35 36 timeZone=new HashMap<>(); 37 timeZone.put("minTime","13:01"); 38 timeZone.put("maxTime","13:30"); 39 timeZoneList.add(timeZone); 40 41 timeZone=new HashMap<>(); 42 timeZone.put("minTime","13:31"); 43 timeZone.put("maxTime","14:00"); 44 timeZoneList.add(timeZone); 45 46 timeZone=new HashMap<>(); 47 timeZone.put("minTime","14:01"); 48 timeZone.put("maxTime","14:30"); 49 timeZoneList.add(timeZone); 50 51 timeZone=new HashMap<>(); 52 timeZone.put("minTime","14:31"); 53 timeZone.put("maxTime","15:00"); 54 timeZoneList.add(timeZone); 55 56 timeZone=new HashMap<>(); 57 timeZone.put("minTime","15:01"); 58 timeZone.put("maxTime","15:30"); 59 timeZoneList.add(timeZone); 60 61 timeZone=new HashMap<>(); 62 timeZone.put("minTime","15:31"); 63 timeZone.put("maxTime","16:00"); 64 timeZoneList.add(timeZone); 65 66 //ArrayList(MaxTime.MinTime)を返す 67 return timeZoneList; 68 } 69 70 private class ListTimeZoneClickListener implements AdapterView.OnItemClickListener{ 71 72 @Override 73 public void onItemClick(AdapterView<?> parent, View view, int position, long id){ 74 Map<String,String> timeZone=(Map<String, String>) parent.getItemAtPosition(position); 75 String minTime=timeZone.get("minTime"); 76 String maxTime=timeZone.get("maxTime"); 77 78 ImageView ivcheck=findViewById(R.id.ivcheck); 79 ivcheck.setImageResource(R.drawable.check); 80 81 Log.i("timezone_position","ポジションは"+position); 82 Log.i("timezone_parent","親要素は"+view); 83 } 84 } 85 86}
レイアウトのソースコード
xml
1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.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 8 <ImageView 9 android:id="@+id/ivcheck" 10 android:layout_width="57dp" 11 android:layout_height="72dp" 12 android:layout_marginStart="8dp" 13 android:layout_marginLeft="8dp" 14 android:layout_marginTop="8dp" 15 android:src="@drawable/check" 16 android:visibility="invisible" 17 app:layout_constraintBottom_toTopOf="@+id/guideline" 18 app:layout_constraintStart_toStartOf="parent" 19 app:layout_constraintTop_toTopOf="@+id/guideline" /> 20 21 <TextView 22 android:id="@+id/tvMinTime" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:layout_marginStart="8dp" 26 android:layout_marginLeft="8dp" 27 android:layout_marginTop="8dp" 28 android:textSize="20sp" 29 app:layout_constraintBottom_toTopOf="@+id/guideline" 30 app:layout_constraintStart_toEndOf="@+id/ivcheck" 31 app:layout_constraintTop_toTopOf="@+id/guideline" 32 app:layout_constraintVertical_bias="0.828" /> 33 34 <TextView 35 android:id="@+id/textView2" 36 android:layout_width="wrap_content" 37 android:layout_height="wrap_content" 38 android:layout_marginStart="4dp" 39 android:layout_marginLeft="4dp" 40 android:layout_marginTop="8dp" 41 android:text="@string/timezone_linear" 42 android:textSize="20sp" 43 app:layout_constraintBottom_toTopOf="@+id/guideline" 44 app:layout_constraintStart_toEndOf="@+id/tvMinTime" 45 app:layout_constraintTop_toTopOf="@+id/guideline" 46 app:layout_constraintVertical_bias="0.828" /> 47 48 <TextView 49 android:id="@+id/tvMaxTime" 50 android:layout_width="wrap_content" 51 android:layout_height="wrap_content" 52 android:layout_marginStart="8dp" 53 android:layout_marginLeft="8dp" 54 android:layout_marginTop="8dp" 55 android:textSize="20sp" 56 app:layout_constraintBottom_toTopOf="@+id/guideline" 57 app:layout_constraintStart_toEndOf="@+id/textView2" 58 app:layout_constraintTop_toTopOf="@+id/guideline" 59 app:layout_constraintVertical_bias="0.828" /> 60 61 <android.support.constraint.Guideline 62 android:id="@+id/guideline" 63 android:layout_width="wrap_content" 64 android:layout_height="wrap_content" 65 android:orientation="horizontal" 66 app:layout_constraintGuide_begin="37dp" /> 67 68</android.support.constraint.ConstraintLayout>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/13 14:43
2018/12/14 01:54