ListViewのデータ登録と削除。DialogFragmentとActivity間でのデータの受け渡し練習。
Q1
フォームよりリストビューにデータを登録できるようにする。
リストビューに登録されたデータは、クリックすると下部にその単語の意味を表示。
ロングクリックで項目を削除できるようにせよ。
削除の際には確認のダイアログを表示すること。
[実行例]
スタート画面

フォームにデータを入力する。

登録ボタンを押すとリストに登録される。

項目をクリックすると下部にその単語の意味が表示される。
(下図はword2をクリックした場合)

項目を長押しすると確認のダイアログを表示する
(下図はword2を長押しした場合)

はいを押すと項目が削除される

●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 = "0dp" |
10 | android:layout_height = "200dp" |
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:id = "@+id/lv" /> |
20 | android:id = "@+id/tvResult" |
21 | android:layout_width = "wrap_content" |
22 | android:layout_height = "wrap_content" |
23 | android:layout_marginLeft = "8dp" |
24 | android:layout_marginRight = "8dp" |
25 | android:layout_marginTop = "16dp" |
26 | android:textSize = "18sp" |
27 | app:layout_constraintLeft_toLeftOf = "parent" |
28 | app:layout_constraintRight_toRightOf = "parent" |
29 | app:layout_constraintTop_toBottomOf = "@+id/lv" /> |
32 | android:id = "@+id/textView2" |
33 | android:layout_width = "wrap_content" |
34 | android:layout_height = "wrap_content" |
35 | android:layout_marginLeft = "8dp" |
37 | app:layout_constraintLeft_toLeftOf = "parent" |
38 | app:layout_constraintBaseline_toBaselineOf = "@+id/etWord" /> |
41 | android:id = "@+id/etWord" |
42 | android:layout_width = "wrap_content" |
43 | android:layout_height = "wrap_content" |
45 | android:inputType = "text" |
47 | app:layout_constraintLeft_toRightOf = "@+id/textView2" |
48 | android:layout_marginLeft = "8dp" |
49 | android:layout_marginTop = "8dp" |
50 | app:layout_constraintTop_toBottomOf = "@+id/tvResult" /> |
53 | android:id = "@+id/textView3" |
54 | android:layout_width = "wrap_content" |
55 | android:layout_height = "wrap_content" |
56 | android:layout_marginLeft = "8dp" |
58 | app:layout_constraintLeft_toLeftOf = "parent" |
59 | app:layout_constraintBaseline_toBaselineOf = "@+id/etBody" /> |
62 | android:id = "@+id/etBody" |
63 | android:layout_width = "wrap_content" |
64 | android:layout_height = "wrap_content" |
66 | android:inputType = "text" |
68 | android:layout_marginTop = "8dp" |
69 | app:layout_constraintTop_toBottomOf = "@+id/etWord" |
70 | app:layout_constraintLeft_toRightOf = "@+id/textView3" |
71 | android:layout_marginLeft = "8dp" /> |
74 | android:id = "@+id/button" |
75 | android:layout_width = "wrap_content" |
76 | android:layout_height = "wrap_content" |
77 | android:layout_marginLeft = "8dp" |
78 | android:layout_marginRight = "8dp" |
79 | android:layout_marginTop = "8dp" |
80 | android:onClick = "btRegister" |
82 | app:layout_constraintLeft_toLeftOf = "parent" |
83 | app:layout_constraintRight_toRightOf = "parent" |
84 | app:layout_constraintTop_toBottomOf = "@+id/etBody" /> |
85 | </ android.support.constraint.ConstraintLayout > |
●MainActivity.java
001 | import android.app.Dialog; |
002 | import android.app.DialogFragment; |
003 | import android.content.DialogInterface; |
004 | import android.os.Bundle; |
005 | import android.support.v7.app.AlertDialog; |
006 | import android.support.v7.app.AppCompatActivity; |
007 | import android.view.View; |
008 | import android.widget.AdapterView; |
009 | import android.widget.ArrayAdapter; |
010 | import android.widget.EditText; |
011 | import android.widget.ListView; |
012 | import android.widget.TextView; |
013 | import android.widget.Toast; |
015 | import java.io.Serializable; |
016 | import java.util.ArrayList; |
017 | import java.util.List; |
019 | public class MainActivity extends AppCompatActivity { |
021 | private EditText etWord,etBody; |
022 | private TextView tvResult; |
023 | private List<Word> list= new ArrayList<>(); |
024 | private ArrayAdapter<Word> adapter; |
027 | protected void onCreate(Bundle savedInstanceState) { |
028 | super .onCreate(savedInstanceState); |
029 | setContentView(R.layout.activity_main); |
031 | lv=(ListView)findViewById(R.id.lv); |
032 | etWord=(EditText)findViewById(R.id.etWord); |
033 | etBody=(EditText)findViewById(R.id.etBody); |
034 | tvResult=(TextView)findViewById(R.id.tvResult); |
036 | adapter= new ArrayAdapter<>( this ,android.R.layout.simple_list_item_1,list); |
038 | lv.setAdapter(adapter); |
040 | lv.setOnItemClickListener( new AdapterView.OnItemClickListener() { |
042 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
044 | Word w=list.get(position); |
046 | tvResult.setText(w.getBody()); |
050 | lv.setOnItemLongClickListener( new AdapterView.OnItemLongClickListener() { |
052 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
054 | Word word=list.get(position); |
056 | DialogFragment dialog= new MyDialog(); |
058 | Bundle b= new Bundle(); |
060 | b.putSerializable( "word" ,word); |
062 | dialog.setArguments(b); |
064 | dialog.show(getFragmentManager(), "tag" ); |
072 | public void btRegister(View v){ |
074 | String word=etWord.getText().toString(); |
075 | String body=etBody.getText().toString(); |
077 | list.add( new Word(word,body)); |
079 | adapter.notifyDataSetChanged(); |
084 | etWord.requestFocus(); |
088 | public void removeList(Word word){ |
090 | adapter.remove(word); |
092 | tvResult.setText( "" ); |
094 | Toast.makeText( this ,word+ "を削除しました" ,Toast.LENGTH_SHORT).show(); |
097 | public static class Word implements Serializable{ |
100 | public Word(String word,String body){ |
106 | public String toString() { |
109 | public String getBody(){ |
114 | public static class MyDialog extends android.app.DialogFragment{ |
117 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
120 | final Word word=(Word)(getArguments().getSerializable( "word" )); |
127 | AlertDialog.Builder builder= new AlertDialog.Builder(getActivity()); |
129 | builder.setTitle( "確認" ) |
130 | .setMessage( "[" +word+ "]を本当に削除してよろしいですか?" ) |
131 | .setPositiveButton( "yes" , new DialogInterface.OnClickListener() { |
133 | public void onClick(DialogInterface ialog, int which) { |
135 | MainActivity activity=(MainActivity)getActivity(); |
137 | activity.removeList(word); |
140 | .setNegativeButton( "no" , new DialogInterface.OnClickListener() { |
142 | public void onClick(DialogInterface dialog, int which) { |
147 | return builder.create(); |
コメント