안드로이드

 

레이아웃 xml 없이 ListActivity를 상속 받아 뷰 생성

class MainActivity
package com.example.choi.mystudy10;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

import java.util.ArrayList;

public class MainActivity extends ListActivity {

    ArrayList<String> data=null;
    ArrayAdapter<String> adapter=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        data =new ArrayList<>();
        data.add("월");
        data.add("화");
        data.add("수");
        data.add("목");
        data.add("금");
        data.add("토");
        data.add("일");

        adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,data);

        setListAdapter(adapter);

    }
}

 

 

 

스피너

 

 

 

 

 

class MainActivity 
package com.example.choi.mystudy10_2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Spinner spinner=null;
    ArrayAdapter<CharSequence> adapter=null;
    String[] strings=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        strings=getResources().getStringArray(R.array.traverArea);

        adapter=ArrayAdapter.createFromResource(this, R.array.traverArea, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner=(Spinner)findViewById(R.id.spinner1);
        spinner.setAdapter(adapter);


        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,
                        strings[position].toString()

                        , Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }




}

 

R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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="com.example.choi.mystudy10_2.MainActivity">

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/spinner1" />
</RelativeLayout>

 

R.array.traverArea
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="traverArea">
        <item>가나</item>
        <item>뉴질랜드</item>
        <item>독일</item>
        <item>미국</item>
        <item>일본</item>
        <item>영국</item>
        <item>프랑스</item>
        <item>중국</item>
        <item>한국</item>
        <item>호주</item>
    </string-array>


</resources

 

 

 

android

 

about author

PHRASE

Level 60  머나먼나라

살아있으면서도 죽은 듯이 지내고, 죽어지내면서도 마음이 가는 대로 살 수 있는 지혜를 터득하라. -지도무난

댓글 ( 4)

댓글 남기기

작성