본문 바로가기

카테고리 없음

기본 상품목록 화면 만들기

activity_product.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BookActivity"
    android:orientation="vertical"
    android:padding="10sp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="상품목록"
        android:textSize="30sp"
        android:layout_gravity="center"/>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list"/>
</LinearLayout>

item_product.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".ProductActivity"
    android:background="#FF5722"
    android:padding="10sp"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtname"
        android:text="공기청정기"
        android:textColor="#FFEB3B"
        android:textSize="30sp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtprice"
        android:text="1000000"
        android:textColor="#FFEB3B"
        android:textSize="30sp"
        android:layout_alignParentRight="true"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtcompany"
        android:text="삼성"
        android:textColor="#FFEB3B"
        android:textSize="30sp"
        android:layout_below="@id/txtname"/>
    <Button
        android:id="@+id/btnorder"
        android:layout_below="@id/txtprice"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="주문"/>
</RelativeLayout>

ProductVO.java

package com.example.ex04;

public class ProductVO {
    private String name;
    private int price;
    private String company;

    public String getName() {
        return name;
    }

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

    public int getPrice() {
        return price;
    }

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

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public ProductVO(String name, int price, String company) {
        this.name = name;
        this.price = price;
        this.company = company;
    }
}

ProductActivity.java

package com.example.ex04;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

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

public class ProductActivity extends AppCompatActivity {
    ArrayList<ProductVO> array;
    ListView list;
    MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product);
        array=new ArrayList<ProductVO>();
        array.add(new ProductVO("세탁기",100,"LG"));
        array.add(new ProductVO("컴퓨터",80,"hp"));
        array.add(new ProductVO("아이폰",105,"apple"));

        adapter=new MyAdapter();
        list=findViewById(R.id.list);
        list.setAdapter(adapter);
    }
    public class MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return array.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }
        //아이템의 개수만큼 get
        @Override
        public View getView(final int position, View view, ViewGroup parent) {
            if(view == null){
                view=getLayoutInflater().inflate(R.layout.item_product,parent,false);
                TextView txtname=view.findViewById(R.id.txtname);
                txtname.setText(array.get(position).getName());
                TextView txtprice=view.findViewById(R.id.txtprice);
                txtprice.setText(array.get(position).getPrice() +"만원");
                TextView txtcompany=view.findViewById(R.id.txtcompany);
                txtcompany.setText(array.get(position).getCompany());

                Button btnorder=view.findViewById(R.id.btnorder);
                btnorder.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String str=array.get(position).getName() + "을(를) 주문하셨습니다.";
                        Toast.makeText(ProductActivity.this,str,Toast.LENGTH_SHORT).show();
                    }
                });
            }
            return view;
        }
    }
}