리스트 다이얼로그 (List Dialog) 예제



java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.ghj.blog_040;
 
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Toast;
 
import org.json.JSONException;
 
public class MainActivity extends AppCompatActivity {
 
    //List Dialog Adapter
    ArrayAdapter<String> adapter;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //Adapter
        adapter = new ArrayAdapter<>(this, android.R.layout.select_dialog_singlechoice);
        adapter.addAll("짜장면""짬뽕""볶음밥""잡채밥""짬짜면""울면");
        adapter.notifyDataSetChanged();
    }
 
    //버튼클릭
    public void mOnMenu(View v){
        CreateListDialog();
    }
 
    //리스트 다이얼로그 생성
    public void CreateListDialog(){
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("사당반점");     //타이틀
        alert.setIcon(R.drawable.icon); //아이콘
 
        //어답터 , 클릭이벤트 설정
        alert.setAdapter(adapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String menu = adapter.getItem(which);
                Toast.makeText(MainActivity.this"선택한 메뉴 : "+menu, Toast.LENGTH_SHORT).show();
            }
        });
        alert.show();
    }
}
 
cs



xml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <Button
        android:text="메뉴선택"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="mOnMenu"/>
 
</LinearLayout>
 
cs



결과

 


+ Recent posts