OpenFrameWork

오픈프레임워크_Day75

px 2015. 6. 30. 14:32
### : 목차 구분 기호
--- : 목차 내에 항목 구분 기호
@@@ : 태그 용도 
,,, : 같은 목차 내에 구분 기호

목차
1. 이론 및 정보
2. 설정 및 그 밖에
3. 소스코드 또는 실습
4. 과제

###################################
1. 이론 및 정보
-----------------------------------
* 어뎁터 - 데이터를 Activity에 잘 꾸며서 전달해줌
세가지 기술

특정 위젯에 맞춰져 있는 어뎁터
- ListViewAdapter, SpinnerAdapter
이미 만들어져 있는 어뎁터
BaseAdapter로 새로 만드는 어뎁터
----------------------------------- 
* ListActivity 상속 받음 - ListViewAcitivity.java
상속 받은 이유는 클래스 기본 레이아웃을
ListView가 자동으로 되도록 만들어줌
----------------------------------- 
* R class

1. 사용자 정의 클래스 
     - 내가만든 프로젝트에 의해 생성,내가 만든 패키지 안에 있음
     - 지금까지 알던 클래스

2. 안드로이드에서 제공하는 클래스 
     - Android.R 에 있음
     - 이클립스에서 소문자 r
                             
----------------------------------- 
###################################
2. 설정 및 그 밖에
-----------------------------------
* 안드로이드 에러 해결
콘솔창에서 에러가 아래와 같이 나온다면
Error: Error parsing ~\study\android-sdk\~\~\~\~\devices.xml

인터넷에서 찾은 방법은 잘 되지 않았고 Virtual Device를 새로 만듬
----------------------------------- 
###################################
3. 소스코드 또는 실습 
-----------------------------------
3-1
WorkSpace : ~\study\AndroidWork 
/AdapterApp/src/com/example/adapterapp/ListViewAcitivity.java

package com.example.adapterapp;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListViewActivity extends ListActivity {
     public String books[] = { "JSP", "JAVA", "Oracle", "MySql",
               "HTML & CSS & JavaScript", "XML", "AJAX", "BootStrap",
               "Git & Github", "Ant", "Android", "Maven", "JUnit", "Spring",
               "UML", "JQuery" };

     @Override
     public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
          // android.R.layout.simple_list_item_1, books);

          ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    R.layout.item1, books);
          setListAdapter(adapter);
          ListView lv = getListView();
          lv.setOnItemClickListener(new OnItemClickListener() {
               @Override
               public void onItemClick(AdapterView<?> parent, View view,
                         int position, long id) {
                    Toast.makeText(getApplicationContext(),
                              books[position] + " is selected!", Toast.LENGTH_LONG)
                              .show();
               }
          });
     }

}
// package com.example.adapterapp;
//
// import android.app.Activity;
// import android.os.Bundle;
// import android.view.View;
// import android.widget.AdapterView;
// import android.widget.AdapterView.OnItemClickListener;
// import android.widget.ArrayAdapter;
// import android.widget.ListView;
// import android.widget.Toast;
//
// public class ListViewActivity extends Activity {
// public String books[] = { "JSP", "JAVA", "Oracle", "MySql",
// "HTML & CSS & JavaScript", "XML", "AJAX", "BootStrap",
// "Git & Github", "Ant", "Android", "Maven", "JUnit", "Spring",
// "UML", "JQuery" };
//
// @Override
// public void onCreate(Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_listview);
//
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
// android.R.layout.simple_list_item_1, books);
// ListView lv = (ListView) findViewById(R.id.listview1);
// lv.setAdapter(adapter);
// lv.setOnItemClickListener(new OnItemClickListener() {
// @Override
// public void onItemClick(AdapterView<?> parent, View view,
// int position, long id) {
// Toast.makeText(getApplicationContext(),
// books[position] + " is selected!", Toast.LENGTH_LONG)
// .show();
// }
// });
// }
//
// }
----------------------------------- 
3-2
WorkSpace : ~\study\AndroidWork
/AdapterApp/res/layout/activity_listview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listview1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>
----------------------------------- 
3-3
WorkSpace : ~\study\AndroidWork
/AdapterApp/res/layout/activity_spinnerview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Spinner Sample"
        android:textSize="20dp" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/spinner_test" />

</LinearLayout>
----------------------------------- 
3-4
WorkSpace : ~\study\AndroidWork
/AdapterApp/src/com/example/adapterapp/SpinnerViewActivity.java

package com.example.adapterapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class SpinnerViewActivity extends Activity {
     String girlgroup[] = { "wonder girls", "girls generation", "kara", "tiara",
               "Space A", "Miss A", "AOA" };

     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_spinnerview);

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

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

          spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
               @Override
               public void onItemSelected(AdapterView<?> parent, View view,
                         int position, long id) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(),
                              girlgroup[position] + " is selected!",
                              Toast.LENGTH_SHORT).show();
               }

               @Override
               public void onNothingSelected(AdapterView<?> parent) {
               }
          });
     }
}
----------------------------------- 
3-5 
WorkSpace : ~\study\AndroidWork
/AdapterApp/res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">AdapterApp</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>

    <string-array name="spinner_test">
        <item>Seoul</item>
        <item>London</item>
        <item>Paris</item>
        <item>Newyork</item>
    </string-array>

</resources>
----------------------------------- 
3-6
WorkSpace : ~\study\AndroidWork
/AndroidWork/AdapterApp/res/layout/activity_item1.xml

<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
        android:textSize="15dp"
        android:textColor="#0000FF" />


<!-- 
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <TextView 
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
             android:textSize="15dp"
             android:textColor="#0000FF" />
     <TextView 
         android:layout_width="match_parent"
         android:layout_height="match_parent"
             android:textSize="15dp"
             android:textColor="#FF0000"
             android:text="$10000" 
             android:layout_marginLeft="20dp"/>
</LinearLayout>
-->
----------------------------------- 
3-7 
WorkSpace : ~\study\AndroidWork
/AndroidWork/AdapterApp/res/layout/activity_auto.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <AutoCompleteTextView
        android:id="@+id/auto1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </AutoCompleteTextView>

</LinearLayout>
----------------------------------- 
3-8 
WorkSpace : ~\study\AndroidWork
/AdapterApp/res/layout/activity_gallery.xml

<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gallery1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"/>
----------------------------------- 
3-9 
WorkSpace : ~\study\AndroidWork
/AdapterApp/src/com/example/adapterapp/GalleryActivity.java

package com.example.adapterapp;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class GalleryActivity extends Activity {
     Gallery gallery;
     int images[] = { R.drawable.sample_0, R.drawable.sample_1,
               R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,
               R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 };

     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_gallery);
          gallery = (Gallery) findViewById(R.id.gallery1);

          ImageAdapter adapter = new ImageAdapter(this);
          gallery.setAdapter(adapter);
     }

     class ImageAdapter extends BaseAdapter {
          Context ctx;

          public ImageAdapter(Context ctx) {
               this.ctx = ctx;
          }

          @Override
          public int getCount() {
               return images.length;
          }

          @Override
          public Object getItem(int position) {
               return images[position];
          }

          @Override
          public long getItemId(int position) {
               return 0;
          }

          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
               ImageView imgView;
               if (convertView == null) {
                    imgView = new ImageView(GalleryActivity.this);
                    // imgView = new ImageView(getApplicationContext());
                    // imgView = new ImageView(ctx);
               } else {
                    imgView = (ImageView) convertView;
               }

               imgView.setImageResource(images[position]);
               imgView.setScaleType(ImageView.ScaleType.FIT_XY);
               imgView.setLayoutParams(new Gallery.LayoutParams(400, 300));
               return imgView;
          }
     }
}
----------------------------------- 
3-10 
WorkSpace : ~\study\AndroidWork
/AdapterApp/res/layout/activity_gridview.xml

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="60dp"
    android:gravity="center"
    android:horizontalSpacing="30dp"
    android:numColumns="auto_fit"
    android:padding="30dp"
    android:verticalSpacing="30dp" />
----------------------------------- 
3-11 
WorkSpace : ~\study\AndroidWork
/AdapterApp/src/com/example/adapterapp/GridViewActivity.java

package com.example.adapterapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class GridViewActivity extends Activity {
     int images[] = { R.drawable.sample_0, R.drawable.sample_1,
               R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,
               R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7,
               R.drawable.image, R.drawable.duckling, R.drawable.img0,
               R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
               R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5,
               R.drawable.sample_6, R.drawable.sample_7, R.drawable.image,
               R.drawable.duckling, R.drawable.img0, R.drawable.sample_0,
               R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3,
               R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6,
               R.drawable.sample_7, R.drawable.image, R.drawable.duckling,
               R.drawable.img0, R.drawable.sample_0,
               R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3,
               R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6,
               R.drawable.sample_7, R.drawable.image, R.drawable.duckling,
               R.drawable.img0, R.drawable.sample_0,
               R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3,
               R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6,
               R.drawable.sample_7, R.drawable.image, R.drawable.duckling,
               R.drawable.img0, R.drawable.sample_0,
               R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3,
               R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6,
               R.drawable.sample_7, R.drawable.image, R.drawable.duckling,
               R.drawable.img0 };

     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_gridview);
          ImageAdapter adapter = new ImageAdapter();
          GridView gv = (GridView) findViewById(R.id.gridview1);
          gv.setAdapter(adapter);
          
          gv.setOnItemClickListener(new OnItemClickListener() {

               @Override
               public void onItemClick(AdapterView<?> parent, View view,
                         int position, long id) {
                    // TODO Auto-generated method stub
                    
               }
          });
     }

     class ImageAdapter extends BaseAdapter {
          @Override
          public int getCount() {
               return images.length;
          }

          @Override
          public Object getItem(int position) {
               return images[position];
          }

          @Override
          public long getItemId(int position) {
               return 0;
          }

          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
               ImageView imgView;
               if (convertView == null) {
                    imgView = new ImageView(getApplicationContext());
                    imgView.setLayoutParams(new GridView.LayoutParams(75, 75));
               } else {
                    imgView = (ImageView) convertView;
               }
               imgView.setImageResource(images[position]);
               return imgView;
          }
     }

}
----------------------------------- 
3-12 
WorkSpace : ~\study\AndroidWork
/AdapterApp/res/layout/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="How to use ListView" />

    <Button
        android:id="@+id/btnSpinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="How to use SpinnerView" />

    <Button
        android:id="@+id/btnAuto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="How to use AutoCompleteTextView" />

    <Button
        android:id="@+id/btnGallery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="How to use Gallery" />

    <Button
        android:id="@+id/btnGrid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="How to use GridView" />

    <Button
        android:id="@+id/btnFlipper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="How to use ViewFlipper" />

    <Button
        android:id="@+id/btnImageList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="make Image List" />

</LinearLayout>
----------------------------------- 
3-13 
WorkSpace : ~\study\AndroidWork
/AdapterApp/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.adapterapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="ListViewActivity"></activity>
        <activity android:name="SpinnerViewActivity"></activity>
        <activity android:name="AutoAcitivity"></activity>
        <activity android:name="GalleryActivity"></activity>
        <activity android:name="GridViewActivity"></activity>
        <activity android:name="FlipperActivity"></activity>
        <activity android:name="ImageViewListActivity"></activity>
    </application>

</manifest>
----------------------------------- 
3-14 
WorkSpace : ~\study\AndroidWork
/AdapterApp/src/com/example/adapterapp/ImageViewListActivity.java

package com.example.adapterapp;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;

public class ImageViewListActivity extends Activity {

     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         ArrayList<ImageViewListDto> data = new ArrayList<ImageViewListDto>();
         data.add(new ImageViewListDto(R.drawable.background, "JSP"));
         data.add(new ImageViewListDto(R.drawable.front_blue, "Java"));
         data.add(new ImageViewListDto(R.drawable.front_green, "Spring"));
         data.add(new ImageViewListDto(R.drawable.front_red, "Android"));
         data.add(new ImageViewListDto(R.drawable.android, "HTML"));
     }

}
----------------------------------- 
3-15 
WorkSpace : ~\study\AndroidWork
/AdapterApp/res/layout/item2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imgHeader"
        android:layout_width="60dp"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/tvBook"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center" />

</LinearLayout>
----------------------------------- 
3-16 
WorkSpace : ~\study\AndroidWork
/AdapterApp/src/com/example/adapterapp/ImageViewListDto.java

package com.example.adapterapp;

public class ImageViewListDto {
     private int icon;
     private String book;

     public ImageViewListDto(int icon, String book) {
          this.icon = icon;
          this.book = book;
     }

     public int getIcon() {
          return icon;
     }

     public String getBook() {
          return book;
     }

}
----------------------------------- 
###################################
4. 과제
-----------------------------------
-----------------------------------
###################################
5. 과제 해결
-----------------------------------
-----------------------------------
###################################
6. 기타
----------------------------------- 
-----------------------------------