안드로이드

 

 

MainActivity
package com.example.choi.ex02;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

    public void onClick(View v){
        Intent intent=null;//인텐트 선언
        switch (v.getId()){ //클릭한 버튼의 아이디
            case R.id.button:
                //현재 화면에서 ListDemo1으로 이동
                intent =new Intent(this, ListDemo1.class);
                break;
            case R.id.button2:
                //현재 화면에서 ListDemo1으로 이동
                intent =new Intent(this, ListDemo2.class);
                break;

            case R.id.button3:
                intent=new Intent(this, ListXml.class);
                break;
            case R.id.button4:
                intent=new Intent(this, ListArray.class);
                break;
            case R.id.button5:
                intent =new Intent(this, PhoneBookActivity.class);
                break;
            case R.id.button6:
                intent=new Intent(this, ProductActivity.class);
                break;
        }

        startActivity(intent);
    }





}



 

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.ex02.MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:text="ListActivity"
            android:onClick="onClick"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button" />

        <Button
            android:text="리스트(ListActivity2)"
            android:onClick="onClick"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button2" />

        <Button
            android:text="리스트-xml"
            android:onClick="onClick"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button3" />


        <Button
            android:text="리스트뷰(ArrayList)"
            android:onClick="onClick"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button4" />

        <Button
            android:text="리스트뷰(전화번호부)"
            android:onClick="onClick"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button5" />

        <Button
            android:text="리스트뷰(상품목록)"
            android:onClick="onClick"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button6" />


    </LinearLayout>





</RelativeLayout>





 

 

 

list2.xml

<?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/list2"
    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.ex02.ListDemo2">


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="19dp"
        android:layout_marginStart="19dp"
        android:layout_marginTop="21dp">

        <TextView
            android:text="TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView" />

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


    </LinearLayout>



</RelativeLayout>

 

 

 

폰북

 

 

PhonebookDTO

 

package com.example.choi.ex02;

/**
 * Created by choi on 2017-02-10.
 */

public class PhonebookDTO {

    private String name;
    private String tel;

    //Alt + Insert =>getter/setter =>Ctrl 키 누른 상태에서 클릭;

    public PhonebookDTO(String name , String tel){
        this.name=name;
        this.tel=tel;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }


    @Override
    public String toString() {
        return "PhonebookDTO{" +
                "name='" + name + '\'' +
                ", tel='" + tel + '\'' +
                '}';
    }


}

 

 

phonebook.xml

<?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">

    <TextView
        android:text=""
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="40sp"
        android:id="@+id/txtName" />


    <TextView
        android:text=""
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:id="@+id/txtTel"
        />




</LinearLayout>

 

PhoneBookActivity
package com.example.choi.ex02;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class PhoneBookActivity extends AppCompatActivity {

    //1. 위젯 선언
    TextView txtResult;
    ListView list;
    List<PhonebookDTO> items;

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

        //위젯 연결
        txtResult=(TextView)findViewById(R.id.textResult);
        list=(ListView)findViewById(R.id.list);

        //3.ArrayList 생성
        items=new ArrayList<>();
        items.add(new PhonebookDTO("김철수", "010-2222-33333"));
        items.add(new PhonebookDTO("김민수", "010-3344-3432"));
        items.add(new PhonebookDTO("정민주", "010-24322-3345"));
        items.add(new PhonebookDTO("최준호", "010-2122-33333"));
        items.add(new PhonebookDTO("최정호", "010-2274-6333"));
        items.add(new PhonebookDTO("최현수", "010-2211-3833"));
        items.add(new PhonebookDTO("강현민", "010-2220-32333"));

        //4. 아답터 연결
        MyAdapter adapter=new MyAdapter( this,  R.layout.phonebook, items);
        list.setAdapter(adapter);


    }

    //내부 클래스
    class MyAdapter extends ArrayAdapter<PhonebookDTO>{
        //Alt + Insert

        public MyAdapter(Context context, int resource, List<PhonebookDTO> items) {
            super(context, resource, items);
        }

        //int position :인덱스 값(0부터)
        //View convertView  : 현재  child view
        //ViewGroup parent : 부모 view

        //Override
        @NonNull
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v =convertView; //현재 자식부


            if(v==null){  //null이면 (첫번째 자식뷰만 생성)
               //레이아웃 생성기  객체
                LayoutInflater li =(LayoutInflater)getLayoutInflater();
                //Phonebook.xml 을 복사하여 새로운 레이아웃을 생성함.
                v =li.inflate(R.layout.phonebook, null);
            }
            //ArrayList 에서 데이터를 가져옴
           final PhonebookDTO dto =items.get(position);
            //자식뷰의 리소스를 연결시킴
            //자식뷰.findViewByID(자식뷰의 리소스 아이디)
            TextView txtName=(TextView)v.findViewById(R.id.txtName);
            TextView txtTel=(TextView)v.findViewById(R.id.txtTel);
            txtName.setText(dto.getName());
            txtTel.setText(dto.getTel());

            //자식뷰를  클릭할 때 전화 걸기
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

              // Intent.ACTION_CALL : 전화걸기  화면
              // tel: 010 -1111 -2222
              // Uri.parse("스트림 형식의 url ") : 스트링을  Uri 로 변환
                    Intent intent =
                            new Intent(Intent.ACTION_CALL,
                                    Uri.parse("tel: "+ dto.getTel()));

              //API Level 23이상 (Android 6.0) 에서는 런타임 사용권한 체크
                    startActivity(intent);

                }
            });

            return v;
        }

    }




}



 

 

상품 목록

 

 

ProductDTO
package com.example.choi.ex02;

/**
 * Created by choi on 2017-02-11.
 */

public class ProductDTO {


    private String productName;
    private int price;

    public ProductDTO(String productName, int price) {
        this.productName = productName;
        this.price = price;
    }


    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }


    @Override
    public String toString() {
        return "ProductDTO{" +
                "productName='" + productName + '\'' +
                ", price=" + price +
                '}';
    }
}

 

product.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtProductName"
        android:textSize="30sp"
        android:textColor="@android:color/holo_blue_dark" />

    <TextView
        android:text="TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtPrice"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="20sp" />



</LinearLayout>

 

 

ProductActivity
package com.example.choi.ex02;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class ProductActivity extends AppCompatActivity {

    //1.변수선언

    ListView list;
    TextView textView;
    List<ProductDTO> items;


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

        //2.객체 생성
        list =(ListView)findViewById(R.id.list);
        textView=(TextView)findViewById(R.id.textView);

        Log.i("test", "리스트 :");

        items =new ArrayList<>();
        ProductDTO[] dtos=new ProductDTO[10];
        for(int i=0; i<dtos.length; i++){
            dtos[i]=new ProductDTO("상품" + i, i*1000);
            items.add(dtos[i]);
        }

        Log.i("test", "리스트 :" +items.toString());


        //아답터에 연결시킴
        MyAdapter adapter=new MyAdapter(this, R.layout.product, items);
        list.setAdapter(adapter);
    }

    //내부 클래스 (커스텀 아답터)
    class MyAdapter extends ArrayAdapter<ProductDTO>{

        //생성자 , getView
        public MyAdapter(Context context, int resource, List<ProductDTO> objects) {
            super(context, resource, objects);
        }


        @NonNull //리턴값이 null이 될 수 없음 //@Nullable // 널 리턴 가능
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v =convertView;

            //첫번째 자식 뷰만 레이아웃 생성기로 생성함
            if(v ==null){
                LayoutInflater li =(LayoutInflater) getLayoutInflater();
                 v=li.inflate(R.layout.product, null);
            }

            ProductDTO dto =items.get(position);
            TextView txtProductName=(TextView)v.findViewById(R.id.txtProductName);
            TextView txtPrice=(TextView)v.findViewById(R.id.txtPrice);

            txtProductName.setText(dto.getProductName());
            txtPrice.setText(dto.getPrice() + "원");

            return v;
        }


    }



}

 

 

 

 

android

 

about author

PHRASE

Level 60  머나먼나라

오늘의 문제는 싸우는 것이요, 내일의 문제는 이기는 것이며, 모든 날의 문제는 죽는 것이다. -위고

댓글 ( 4)

댓글 남기기

작성