Help us understand the problem. What is going on with this article?

ダイアログにListView表示

More than 5 years have passed since last update.

AlertDialog内にListviewを表示してみる

AlertDialogには、setView(View view)メソッドがあります。
これを使用して、Dialog内にListViewを表示してみます。
もちろん自作で作成したListViewをsetView(View view)に渡す事も可能です。

public class MainActivity2 extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        ArrayList<Item> itemList = new ArrayList<Item>();
        for(int i=0;i<=1000;i++){
            Item item = new Item();
            item.setIntItem(i);
            item.setStringItem(i+"");
            itemList.add(item);
        }

        CustomAdapter adapter = new CustomAdapter(getApplicationContext(), 0, itemList);
        ListView listView = new ListView(this);
        listView.setAdapter(adapter);

        AlertDialog dialog = new AlertDialog.Builder(this)
        .setTitle("選択してくだい。")
        .setView(listView).create();
        dialog.show();
    }

    // ListViewで使用するItemClass
    public class Item{
        private int intItem;
        private String stringItem;

        public void setStringItem(String stringItem){
            this.stringItem = stringItem;
        }
        public String getStringItem(){
            return this.stringItem;
        }
        public void setIntItem(int intItem){
            this.intItem = intItem;
        }
        public int getIntItem(){
            return this.intItem;
        }
    }

    // ListViewで使用するadapter
    public class CustomAdapter extends ArrayAdapter<Item>{
        private LayoutInflater inflater;

        public CustomAdapter(Context context, int resource, List<Item> objects) {
            super(context, resource, objects);
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        @Override
        public View getView(int position, View v, ViewGroup parent) {
            Item item = (Item)getItem(position);
            if (null == v) v = inflater.inflate(R.layout.custom_listview, null);

            TextView intTextView = (TextView)v.findViewById(R.id.int_item);
            intTextView.setText(item.getIntItem()+"");

            TextView stringTextView = (TextView)v.findViewById(R.id.string_item);
            stringTextView.setText(item.getStringItem());
            return v;
        }
    }
}
custom_listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/int_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp" >
    </TextView>

    <TextView
        android:id="@+id/string_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp" >
    </TextView>

</LinearLayout>

以上全文です。
ポイントはここだけです。

AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("選択してくだい。")
.setView(listView).create();
dialog.show();

作成したListViewをsetViewで渡します。こうするとこのAlertDailog内にListViewが表示されます。

listView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {}
});

でリスナーを登録するこもと可能です。
スクリーンショットです。

Screenshot_2014-06-07-17-24-47.png

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away