アダプターを自作してオリジナルレイアウトのリストビューを作成しよう。
Q1
下部のフォームからデータを入力すると上部のリストビューに追加されていくアプリを作成せよ。
リストのビューはBaseAdapterを継承したクラスを使って作例のようにすること。
画像は以下から画像ダウンロードして使うこと。
[画像]


[実行例]
スタート画面

フォームに入力し登録ボタンを押すと・・・

リストビューに表示される。

性別で女を選ぶと下図のようになる。

●activity_main.xml
001 | <? xml version = "1.0" encoding = "utf-8" ?> |
002 | < android.support.constraint.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
003 | xmlns:app = "http://schemas.android.com/apk/res-auto" |
004 | android:layout_width = "match_parent" |
005 | android:layout_height = "match_parent" |
010 | android:layout_width = "368dp" |
011 | android:layout_height = "250dp" |
012 | android:layout_marginLeft = "8dp" |
013 | android:layout_marginRight = "8dp" |
014 | android:layout_marginTop = "8dp" |
015 | app:layout_constraintLeft_toLeftOf = "parent" |
016 | app:layout_constraintRight_toRightOf = "parent" |
017 | app:layout_constraintTop_toTopOf = "parent" /> |
020 | android:id = "@+id/textView" |
021 | android:layout_width = "wrap_content" |
022 | android:layout_height = "wrap_content" |
023 | android:layout_marginLeft = "8dp" |
025 | app:layout_constraintLeft_toLeftOf = "parent" |
026 | app:layout_constraintBaseline_toBaselineOf = "@+id/etName" /> |
029 | android:id = "@+id/etName" |
030 | android:layout_width = "0dp" |
031 | android:layout_height = "wrap_content" |
032 | android:layout_marginLeft = "8dp" |
033 | android:layout_marginTop = "8dp" |
035 | android:inputType = "textPersonName" |
036 | app:layout_constraintLeft_toRightOf = "@+id/textView" |
037 | app:layout_constraintTop_toBottomOf = "@+id/lv" |
038 | android:layout_marginRight = "8dp" |
039 | app:layout_constraintRight_toRightOf = "parent" |
040 | app:layout_constraintHorizontal_bias = "0.504" /> |
043 | android:id = "@+id/textView2" |
044 | android:layout_width = "wrap_content" |
045 | android:layout_height = "wrap_content" |
046 | android:layout_marginLeft = "8dp" |
048 | app:layout_constraintLeft_toLeftOf = "parent" |
049 | app:layout_constraintBaseline_toBaselineOf = "@+id/etAge" /> |
052 | android:id = "@+id/etAge" |
053 | android:layout_width = "0dp" |
054 | android:layout_height = "wrap_content" |
055 | android:layout_marginLeft = "8dp" |
056 | android:layout_marginRight = "8dp" |
057 | android:layout_marginTop = "8dp" |
059 | android:inputType = "number" |
060 | app:layout_constraintHorizontal_bias = "0.504" |
061 | app:layout_constraintLeft_toRightOf = "@+id/textView2" |
062 | app:layout_constraintRight_toRightOf = "parent" |
063 | app:layout_constraintTop_toBottomOf = "@+id/etName" /> |
066 | android:layout_width = "wrap_content" |
067 | android:layout_height = "wrap_content" |
068 | android:orientation = "horizontal" |
069 | android:layout_marginTop = "7dp" |
070 | app:layout_constraintTop_toBottomOf = "@+id/etAge" |
071 | android:layout_marginLeft = "8dp" |
072 | app:layout_constraintLeft_toLeftOf = "parent" |
073 | android:layout_marginRight = "8dp" |
074 | app:layout_constraintRight_toRightOf = "parent" |
076 | app:layout_constraintHorizontal_bias = "0.503" > |
079 | android:id = "@+id/rb1" |
080 | android:layout_width = "wrap_content" |
081 | android:layout_height = "wrap_content" |
082 | android:layout_marginRight = "20dp" |
083 | android:layout_weight = "1" |
087 | android:id = "@+id/rb2" |
088 | android:layout_width = "wrap_content" |
089 | android:layout_height = "wrap_content" |
090 | android:layout_weight = "1" |
095 | android:id = "@+id/button" |
096 | android:layout_width = "wrap_content" |
097 | android:layout_height = "wrap_content" |
098 | android:layout_marginLeft = "8dp" |
099 | android:layout_marginRight = "8dp" |
100 | android:layout_marginTop = "8dp" |
101 | android:onClick = "btClick" |
103 | app:layout_constraintLeft_toLeftOf = "parent" |
104 | app:layout_constraintRight_toRightOf = "parent" |
105 | app:layout_constraintTop_toBottomOf = "@+id/rg" /> |
106 | </ android.support.constraint.ConstraintLayout > |
●list_item.xml
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < android.support.constraint.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | xmlns:app = "http://schemas.android.com/apk/res-auto" |
04 | xmlns:tools = "http://schemas.android.com/tools" |
05 | android:layout_width = "match_parent" |
06 | android:layout_height = "wrap_content" |
07 | android:paddingBottom = "15dp" > |
11 | android:layout_width = "wrap_content" |
12 | android:layout_height = "wrap_content" |
13 | android:layout_marginLeft = "8dp" |
14 | android:layout_marginTop = "8dp" |
15 | app:layout_constraintLeft_toLeftOf = "parent" |
16 | app:layout_constraintTop_toTopOf = "parent" |
17 | app:srcCompat = "@drawable/man" /> |
20 | android:id = "@+id/tvName" |
21 | android:layout_width = "wrap_content" |
22 | android:layout_height = "wrap_content" |
23 | android:layout_marginLeft = "8dp" |
24 | android:gravity = "center_vertical" |
25 | android:text = "TextView" |
26 | app:layout_constraintLeft_toRightOf = "@+id/iv" |
27 | tools:layout_editor_absoluteY = "39dp" /> |
30 | android:id = "@+id/tvAge" |
31 | android:layout_width = "wrap_content" |
32 | android:layout_height = "wrap_content" |
33 | android:layout_marginLeft = "8dp" |
34 | android:gravity = "center_vertical" |
35 | android:text = "TextView" |
36 | app:layout_constraintLeft_toRightOf = "@+id/tvName" |
37 | tools:layout_editor_absoluteY = "39dp" /> |
38 | </ android.support.constraint.ConstraintLayout > |
●Person.java
05 | public Person(String name, int age, int resId){ |
11 | public int getResId() { |
15 | public String getName() { |
●MyAdapter.java
01 | import android.app.Activity; |
02 | import android.content.Context; |
03 | import android.view.LayoutInflater; |
04 | import android.view.View; |
05 | import android.view.ViewGroup; |
06 | import android.widget.BaseAdapter; |
07 | import android.widget.ImageView; |
08 | import android.widget.TextView; |
12 | public class MyAdapter extends BaseAdapter { |
14 | private List<Person> list; |
15 | private LayoutInflater inflater; |
16 | public MyAdapter(Context c, int layout,List<Person> list){ |
17 | inflater=((Activity)c).getLayoutInflater(); |
23 | public int getCount() { |
28 | public Person getItem( int position) { |
29 | return list.get(position); |
33 | public long getItemId( int position) { |
38 | public View getView( int position, View convertView, ViewGroup parent) { |
39 | Person p=list.get(position); |
41 | if (convertView == null ){ |
42 | convertView=inflater.inflate(layout, null ); |
43 | holder= new ViewHolder(convertView); |
44 | convertView.setTag(holder); |
46 | holder=(ViewHolder)convertView.getTag(); |
48 | holder.iv.setImageResource(p.getResId()); |
49 | holder.tvName.setText(p.getName()); |
50 | holder.tvAge.setText(String.valueOf(p.getAge())); |
54 | private static class ViewHolder{ |
58 | public ViewHolder(View v){ |
59 | iv=(ImageView)v.findViewById(R.id.iv); |
60 | tvName=(TextView)v.findViewById(R.id.tvName); |
61 | tvAge=(TextView)v.findViewById(R.id.tvAge); |
●MainActivity.java
01 | package com.example.mjpurin.viewholderlesson; |
03 | import android.os.Bundle; |
04 | import android.support.v7.app.AppCompatActivity; |
05 | import android.view.View; |
06 | import android.widget.BaseAdapter; |
07 | import android.widget.EditText; |
08 | import android.widget.ListView; |
09 | import android.widget.RadioGroup; |
11 | import java.util.ArrayList; |
14 | public class MainActivity extends AppCompatActivity { |
15 | private EditText etName,etAge; |
17 | private RadioGroup rg; |
18 | private List<Person> list= new ArrayList<>(); |
19 | private BaseAdapter adapter; |
21 | protected void onCreate(Bundle savedInstanceState) { |
22 | super .onCreate(savedInstanceState); |
23 | setContentView(R.layout.activity_main); |
24 | etName=(EditText)findViewById(R.id.etName); |
25 | etAge=(EditText)findViewById(R.id.etAge); |
26 | rg=(RadioGroup)findViewById(R.id.rg); |
27 | lv=(ListView)findViewById(R.id.lv); |
28 | adapter= new MyAdapter( this ,R.layout.list_item,list); |
29 | lv.setAdapter(adapter); |
31 | public void btClick(View v){ |
32 | String name=etName.getText().toString(); |
33 | int age=Integer.parseInt(etAge.getText().toString()); |
34 | int resId=rg.getCheckedRadioButtonId()==R.id.rb1? R.drawable.man:R.drawable.woman; |
35 | list.add( new Person(name,age,resId)); |
36 | adapter.notifyDataSetChanged(); |
コメント