안드로이드는 재미있으면서도 너무 어려운거 같애요. ㅠ.ㅠ
MainActivity.java 에서 리스트뷰를 다이얼로그로 띄웠는데요.
다이얼로그에 있는 저장버튼을 클릭하면, 데이터베이스에 저장되고, 다이얼로그가 없어지게끔 하고 싶은데요.
데이터베이스에 저장은 되는데, 다이얼로그 닫는 방법을 모르겠어요.
Intent로 현재 MainActivity.java를 재호출해줘야할지....다른 방법이 있는지 잘 모르겠네요.
위 그림처럼, [등록]을 클릭하면 다이얼로그창이 나오구요.
다이얼로그창에서 저장을 하면, 데이터베이스에 저장까지는 되는데...
다이얼로그창을 어떻게 닫아야 할지 잘 모르겠습니다.
cancel 이나 dismiss 둘다 안되고... 방법을 잘 모르겠어요.
이틀동안 자료찾아보고, 책도 찾아보고 있는데....초보라서 잘 모르겠어요.
아시는 분 조언 부탁드릴께요. ^^
[MainActivity.java]
public void OpenDialog(View v) {
inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.sample,(ViewGroup) findViewById(R.id.sample));
dialog = new AlertDialog.Builder(this);
dialog.setTitle("신규학생등록");
dialog.setMessage("하단의 정보를 입력해주세요.");
dialog.setView(layout);
dialog.setNegativeButton("닫기", null);
dialog.create();
dialog.show();
}
public void SaveStudent(View v) {
String sql = "데이터베이스 저장 쿼리";
db.execSQL(sql);
//다이얼로그창 닫기 --- 어떤 코드를 넣어줘야 될까요?
// intent 로 현재 class 를 reload 해줘야하는가요?
// 별도로 다이얼로그 창을 닫는 코드가 있나요?
// cancel() , dismiss() -> 이것도 안되더라구요.
}
[sample.xml (다이얼로그 listview)]
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sample"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingTop="5dp">
<TextView
android:layout_width="80dp"
android:layout_height="match_parent"
android:text="이름"
android:gravity="center_vertical" />
<EditText
android:id="@+id/cname"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#dddddd" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingTop="5dp">
<TextView
android:layout_width="80dp"
android:layout_height="match_parent"
android:text="학번"
android:gravity="center_vertical"
/>
<EditText
android:id="@+id/cnumber"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#dddddd" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="10dp">
<Button
android:id="@+id/button"
android:layout_width="80dp"
android:layout_height="40dp"
android:text="저장"
android:onClick="SaveStudent" />
</LinearLayout>
</LinearLayout>
</ScrollView>