본문 바로가기

Android

리스트뷰 - 어댑터뷰

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

 

    <ListView

        android:id="@+id/list"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

         />

 

</LinearLayout>

 


package com.example.listviewex01;

 

import java.util.ArrayList;

 

 

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.Toast;

 

public class MainActivity extends Activity {

     ArrayList<String> list;

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

          

           // 데이터

           list = new ArrayList<String>();

           list.add("00 강감찬");

           list.add("01 이순신");

           list.add("02 김유신");

           list.add("03 을지문덕");

           list.add("04 강감찬");

           list.add("05 이순신");

           list.add("07 김유신");

           list.add("08 을지문덕");

           list.add("09 강감찬");

           list.add("10 이순신");

                    

          

           // 어댑터

           ArrayAdapter<String> adapter

 = new ArrayAdapter<String>

 (this, android.R.layout.simple_list_item_1,list);

 (this, android.R.layout.simple_list_item_checked,list);

 (this, android.R.layout.simple_list_item_single_choice,list); 

         

  ListView lv = (ListView)findViewById(R.id.list);

           lv.setAdapter(adapter);

          

           lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

 

                @Override

                public void onItemClick(AdapterView<?> parent, View view, int position,

                           long arg3) {

                     // TODO Auto-generated method stub

                     Toast.makeText(MainActivity.this, list.get(position), Toast.LENGTH_SHORT).show();

                }

           });

     }

 

     @Override

     public boolean onCreateOptionsMenu(Menu menu) {

           // Inflate the menu; this adds items to the action bar if it is present.

           getMenuInflater().inflate(R.menu.main, menu);

           return true;

     }

 

}




(this, android.R.layout.simple_list_item_1,list);



(this, android.R.layout.simple_list_item_checked,list);



(this, android.R.layout.simple_list_item_single_choice,list); 



 

'Android' 카테고리의 다른 글

리스트뷰 - 항목 추가, 삭제하기(singlechoice)  (0) 2013.04.01
리스트뷰 - choiceMode  (0) 2013.04.01
메뉴 - context  (0) 2013.03.29
메뉴 - 체크메뉴  (0) 2013.03.29
메뉴(menu) - 옵션 메뉴  (0) 2013.03.29