startActivityForResultを用いてデータの授受をしてみよう。
Q1
リストビューを使い英単語の一覧を表示する。
英単語の追加と編集の機能を実装すること。
詳しくは実行例を参照せよ。
[実行例]
スタート画面

登録を押すと画面遷移して入力フォームが表示される。

フォームに入力し、追加ボタンを押すと・・・

リストに表示される。

項目タップでカスタマイズされたトーストにて単語と意味が表示される。

リスト項目を長押しすると遷移して編集画面へ

編集し更新ボタンを押すと・・・

更新される。

●activity_main.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 | android:layout_width = "match_parent" |
05 | android:layout_height = "match_parent" |
09 | android:layout_width = "368dp" |
10 | android:layout_height = "400dp" |
11 | android:layout_marginLeft = "8dp" |
12 | android:layout_marginRight = "8dp" |
13 | android:layout_marginTop = "8dp" |
14 | app:layout_constraintLeft_toLeftOf = "parent" |
15 | app:layout_constraintRight_toRightOf = "parent" |
16 | app:layout_constraintTop_toTopOf = "parent" |
17 | android:layout_marginStart = "8dp" |
18 | android:layout_marginEnd = "8dp" |
19 | android:id = "@+id/lv" /> |
22 | android:id = "@+id/button" |
23 | android:layout_width = "0dp" |
24 | android:layout_height = "wrap_content" |
25 | android:layout_marginLeft = "16dp" |
26 | android:layout_marginRight = "8dp" |
27 | android:layout_marginTop = "12dp" |
28 | android:onClick = "btClick" |
30 | app:layout_constraintHorizontal_bias = "0.0" |
31 | app:layout_constraintLeft_toLeftOf = "parent" |
32 | app:layout_constraintRight_toRightOf = "parent" |
33 | app:layout_constraintTop_toBottomOf = "@+id/lv" /> |
34 | </ android.support.constraint.ConstraintLayout > |
●activity_sub.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 | android:layout_width = "match_parent" |
05 | android:layout_height = "match_parent" |
09 | android:id = "@+id/textView" |
10 | android:layout_width = "36dp" |
11 | android:layout_height = "wrap_content" |
13 | android:textSize = "18sp" |
14 | app:layout_constraintBaseline_toBaselineOf = "@+id/etWord" |
15 | android:layout_marginLeft = "16dp" |
16 | app:layout_constraintLeft_toLeftOf = "parent" /> |
19 | android:id = "@+id/etWord" |
20 | android:layout_width = "wrap_content" |
21 | android:layout_height = "wrap_content" |
22 | android:layout_marginLeft = "8dp" |
24 | android:inputType = "textPersonName" |
25 | app:layout_constraintLeft_toRightOf = "@+id/textView" |
26 | app:layout_constraintTop_toTopOf = "parent" |
27 | android:layout_marginTop = "16dp" /> |
30 | android:id = "@+id/textView2" |
31 | android:layout_width = "wrap_content" |
32 | android:layout_height = "wrap_content" |
34 | android:textSize = "18sp" |
35 | android:layout_marginLeft = "16dp" |
36 | app:layout_constraintLeft_toLeftOf = "parent" |
37 | app:layout_constraintBaseline_toBaselineOf = "@+id/etBody" /> |
40 | android:id = "@+id/etBody" |
41 | android:layout_width = "wrap_content" |
42 | android:layout_height = "wrap_content" |
43 | android:layout_marginLeft = "8dp" |
44 | android:layout_marginTop = "8dp" |
46 | android:inputType = "textMultiLine|textPersonName" |
49 | android:singleLine = "false" |
50 | app:layout_constraintLeft_toRightOf = "@+id/textView2" |
51 | app:layout_constraintTop_toBottomOf = "@+id/etWord" /> |
54 | android:id = "@+id/btAction" |
55 | android:layout_width = "wrap_content" |
56 | android:layout_height = "wrap_content" |
57 | android:layout_marginLeft = "8dp" |
58 | android:layout_marginRight = "8dp" |
59 | android:layout_marginTop = "8dp" |
60 | android:onClick = "btAction" |
62 | android:textSize = "18sp" |
63 | app:layout_constraintLeft_toLeftOf = "parent" |
64 | app:layout_constraintRight_toRightOf = "parent" |
65 | app:layout_constraintTop_toBottomOf = "@+id/etBody" /> |
66 | </ android.support.constraint.ConstraintLayout > |
●toast_view.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 | android:layout_width = "match_parent" |
05 | android:layout_height = "match_parent" |
06 | android:background = "@android:color/holo_orange_light" |
07 | android:paddingRight = "16dp" > |
10 | android:id = "@+id/textView3" |
11 | android:layout_width = "wrap_content" |
12 | android:layout_height = "wrap_content" |
13 | android:layout_marginLeft = "16dp" |
15 | android:textSize = "18sp" |
16 | app:layout_constraintBaseline_toBaselineOf = "@+id/tvWord" |
17 | app:layout_constraintLeft_toLeftOf = "parent" /> |
20 | android:id = "@+id/tvWord" |
21 | android:layout_width = "wrap_content" |
22 | android:layout_height = "wrap_content" |
23 | android:layout_marginLeft = "8dp" |
24 | android:layout_marginTop = "16dp" |
25 | android:text = "TextView" |
26 | android:textSize = "18sp" |
27 | app:layout_constraintLeft_toRightOf = "@+id/textView3" |
28 | app:layout_constraintTop_toTopOf = "parent" /> |
31 | android:id = "@+id/textView5" |
32 | android:layout_width = "wrap_content" |
33 | android:layout_height = "wrap_content" |
34 | android:layout_marginLeft = "16dp" |
35 | android:layout_marginTop = "8dp" |
37 | android:textSize = "18sp" |
38 | app:layout_constraintLeft_toLeftOf = "parent" |
39 | app:layout_constraintTop_toBottomOf = "@+id/textView3" /> |
42 | android:id = "@+id/tvBody" |
43 | android:layout_width = "wrap_content" |
44 | android:layout_height = "wrap_content" |
45 | android:layout_marginBottom = "16dp" |
46 | android:layout_marginLeft = "8dp" |
47 | android:layout_marginTop = "8dp" |
50 | android:text = "TextView" |
51 | android:textSize = "18sp" |
52 | app:layout_constraintBottom_toBottomOf = "parent" |
53 | app:layout_constraintLeft_toRightOf = "@+id/textView5" |
54 | app:layout_constraintTop_toBottomOf = "@+id/tvWord" /> |
55 | </ android.support.constraint.ConstraintLayout > |
●Word.java
01 | import java.io.Serializable; |
04 | public class Word implements Serializable{ |
09 | public Word(String word, String body) { |
15 | public String toString() { |
16 | return this .getWord(); |
18 | public void setWord(String word){ |
21 | public void setBody(String body){ |
25 | public String getBody() { |
28 | public String getWord(){ |
●MainActivity.java
01 | import android.content.Intent; |
02 | import android.os.Bundle; |
03 | import android.support.v7.app.AppCompatActivity; |
04 | import android.view.Gravity; |
05 | import android.view.LayoutInflater; |
06 | import android.view.View; |
07 | import android.widget.AdapterView; |
08 | import android.widget.ArrayAdapter; |
09 | import android.widget.ListView; |
10 | import android.widget.TextView; |
11 | import android.widget.Toast; |
13 | import java.util.ArrayList; |
16 | public class MainActivity extends AppCompatActivity { |
18 | private TextView tvWord,tvBody; |
19 | private ArrayAdapter<Word> adapter; |
20 | private List<Word> list= new ArrayList<>(); |
22 | public static final int WORD_ADD= 1 ,WORD_EDIT= 2 ; |
25 | protected void onCreate(Bundle savedInstanceState) { |
26 | super .onCreate(savedInstanceState); |
27 | setContentView(R.layout.activity_main); |
28 | lv=(ListView)findViewById(R.id.lv); |
30 | adapter= new ArrayAdapter<>( this ,android.R.layout.simple_list_item_1,list); |
31 | lv.setAdapter(adapter); |
32 | lv.setOnItemClickListener( new AdapterView.OnItemClickListener() { |
34 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
35 | Word w=list.get(position); |
36 | tvWord.setText(w.getWord()); |
37 | tvBody.setText(w.getBody()); |
42 | lv.setOnItemLongClickListener( new AdapterView.OnItemLongClickListener() { |
44 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
45 | Word w=list.get(position); |
46 | Intent i= new Intent(MainActivity. this ,SubActivity. class ); |
47 | i.putExtra( "index" ,position); |
49 | i.putExtra( "requestCode" ,WORD_EDIT); |
50 | startActivityForResult(i,WORD_EDIT); |
56 | private void createToast() { |
57 | LayoutInflater inflater=getLayoutInflater(); |
58 | View view=inflater.inflate(R.layout.toast_view, null ); |
59 | tvWord=(TextView)view.findViewById(R.id.tvWord); |
60 | tvBody=(TextView)view.findViewById(R.id.tvBody); |
61 | toast= new Toast( this ); |
63 | toast.setDuration(Toast.LENGTH_LONG); |
64 | toast.setGravity(Gravity.CENTER, 0 , 0 ); |
68 | public void btClick(View v){ |
69 | Intent i= new Intent( this ,SubActivity. class ); |
70 | i.putExtra( "requestCode" ,WORD_ADD); |
71 | startActivityForResult(i,WORD_ADD); |
75 | protected void onActivityResult( int requestCode, int resultCode, Intent data) { |
76 | super .onActivityResult(requestCode, resultCode, data); |
77 | if (resultCode==RESULT_OK) { |
78 | Word w = (Word) data.getSerializableExtra( "word" ); |
79 | switch (requestCode) { |
84 | int index = data.getIntExtra( "index" , - 1 ); |
88 | adapter.notifyDataSetChanged(); |
●SubActivity.java
01 | import android.content.Intent; |
02 | import android.os.Bundle; |
03 | import android.support.v7.app.AppCompatActivity; |
04 | import android.view.View; |
05 | import android.widget.Button; |
06 | import android.widget.EditText; |
08 | public class SubActivity extends AppCompatActivity { |
09 | private EditText etWord, etBody; |
10 | private Button btAction; |
11 | private int requestCode; |
16 | protected void onCreate(Bundle savedInstanceState) { |
17 | super .onCreate(savedInstanceState); |
18 | setContentView(R.layout.activity_sub); |
19 | etWord = (EditText) findViewById(R.id.etWord); |
20 | etBody = (EditText) findViewById(R.id.etBody); |
21 | btAction = (Button) findViewById(R.id.btAction); |
22 | Intent i = this .getIntent(); |
23 | requestCode = i.getIntExtra( "requestCode" , MainActivity.WORD_ADD); |
24 | switch (requestCode) { |
26 | case MainActivity.WORD_EDIT: |
28 | w = (Word) i.getSerializableExtra( "word" ); |
29 | index=i.getIntExtra( "index" ,- 1 ); |
30 | etWord.setText(w.getWord()); |
31 | etBody.setText(w.getBody()); |
32 | btAction.setText( "更新" ); |
38 | public void btAction(View v) { |
39 | String word=etWord.getText().toString(); |
40 | String body=etBody.getText().toString(); |
41 | Intent i= new Intent(); |
42 | switch ( this .requestCode) { |
43 | case MainActivity.WORD_ADD: |
44 | w= new Word(word,body); |
46 | case MainActivity.WORD_EDIT: |
49 | i.putExtra( "index" ,index); |
55 | setResult(RESULT_OK,i); |
コメント