Android実習

18636 ワード


ProductActivity

package com.example.ex0414;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;

public class ProductActivity extends AppCompatActivity {
    ListView lvProduct;
    ProductAdapter adapter;
    ArrayList<ProductVO> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product);
       
        lvProduct = findViewById(R.id.lv);
        list = new ArrayList<ProductVO>();
        //리스트 ID를 동적으로 접근하는 방법
        for(int i =0; i<11; i++){
            //리소스객체접근.리소스ID반환메소드(리소스ID, 리소스타입, 패키지명)
            int imgId = getResources().getIdentifier("item"+(i+1), "drawable",getPackageName());
            int strId = getResources().getIdentifier("item"+(i+1), "string",getPackageName());
            list.add(new ProductVO(imgId,strId,"10"));
      
        }

        adapter = new ProductAdapter(
                ProductActivity.this,
                R.layout.product_item,
                list);
        lvProduct.setAdapter(adapter);

    }
}
  • リストIDに動的にアクセスするには、リソースオブジェクトを使用する必要があります.
  • 形式はgetResources()です.getIdentifier(リソースID、リソースタイプ、パッケージ名)を含める必要があります.
  • ProductAdapter

    package com.example.ex0414;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Toast;
    
    import androidx.appcompat.app.AlertDialog;
    
    import java.util.ArrayList;
    
    
    public class ProductAdapter extends BaseAdapter {
        Context context;
        int item_layout;
        ArrayList<ProductVO> list;
        LayoutInflater inflater;
        public ProductAdapter(Context context, int item_layout, ArrayList<ProductVO> list) {
            this.context = context;
            this.item_layout = item_layout;
            this.list = list;
            this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        @Override
        public int getCount() {
            return list.size();
        }
    
        @Override
        //선택한 인덱스의 내용물?
        public Object getItem(int i) {
            return list.get(i);
        }
    
        @Override
        //선택한 인덱스를 반환하는 부분
        public long getItemId(int i) {
            return i;
        }
    
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
    
            ItemHolder holder = null;
    
            if(view ==null){
                view = inflater.inflate(item_layout, viewGroup, false);
                holder = new ItemHolder(view);
    
                view.setTag(holder);
            }else{
                holder = (ItemHolder) view.getTag();
            }
            ProductVO vo = (ProductVO) getItem(i);
            holder.getImg().setImageResource(vo.getImg());
            holder.getPro_name().setText(vo.getName());
            holder.getPro_count().setText(vo.getProcount());
    
            holder.getBtn_product().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //대화상자 : AlertDialog
                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setTitle("상품 보기").setMessage("상품개수 : "+vo.getProcount()).setPositiveButton("확인", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
    
                        }
                    })
                    .setNegativeButton("취소", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    });
                    AlertDialog dialog = builder.create();
                    dialog.show();
    
                    Toast.makeText(context,vo.getName(),Toast.LENGTH_SHORT).show();
    
    
                }
            });
    
            return view; //젤 중요
        }
    }
    ボタンクリック時にプロンプトウィンドウをポップアップする機能が付与されています.

    ProductVO

    package com.example.ex0414;
    
    public class ProductVO {
        private int img;
        private int proname;
        private String procount;
    
        public ProductVO(){}
    
        public ProductVO(int img, int proname, String procount) {
            this.img = img;
            this.proname = proname;
            this.procount = procount;
        }
    
        public int getImg() {
            return img;
        }
    
        public void setImg(int img) {
            this.img = img;
        }
    
        public int getName() {
            return proname;
        }
    
        public void setName(int name) {
            this.proname = name;
        }
    
        public String getProcount() {
            return procount;
        }
    
        public void setProcount(String procount) {
            this.procount = procount;
        }
    
        @Override
        public String toString() {
            return "ProductVO{" +
                    "img=" + img +
                    ", proname='" + proname  +
                    ", procount='" + procount  +
                    '}';
        }
    }

    ItemHolder

    package com.example.ex0414;
    
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    public class ItemHolder {
        private TextView pro_name;
        private TextView pro_count;
        private ImageView img;
        private Button btn_product;
    
        public ItemHolder(View itemView){
            img = itemView.findViewById(R.id.img);
            pro_name = itemView.findViewById(R.id.pro_name);
            pro_count = itemView.findViewById(R.id.pro_count);
            btn_product = itemView.findViewById(R.id.btn_product);
        }
    
        public ImageView getImg() { return img; }
    
        public TextView getPro_name() {
            return pro_name;
        }
    
        public TextView getPro_count() {
            return pro_count;
        }
    
        public Button getBtn_product() {
            return btn_product;
        }
    }

    product_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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">
    
        <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/item1" />
    
        <TextView
            android:id="@+id/pro_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginTop="16dp"
            android:text="TextView"
            android:textSize="24sp"
            app:layout_constraintStart_toEndOf="@+id/img"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/pro_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:text="TextView"
            android:textSize="24sp"
            app:layout_constraintStart_toEndOf="@+id/img"
            app:layout_constraintTop_toBottomOf="@+id/pro_name" />
    
        <Button
            android:id="@+id/btn_product"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="28dp"
            android:text="상품보기"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    実行画面