Android

[Android Studio] 안드로이드 스튜디오 JAVA RecyclerView에서 Intent 사용하기

파송송 2022. 10. 9. 20:26
728x90

 

  private Context context;
  this.context = context;

위의 두개를 설정해준다


Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(cameraIntent);

context를 이용하여 intent를 사용한다


전체코드

package com.example.k_food_application;


import android.content.Context;
import android.content.Intent;
import android.provider.MediaStore;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResult;
import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;
import androidx.recyclerview.widget.RecyclerView;

import java.io.IOException;
import java.util.ArrayList;

public class DayMealAdapter extends RecyclerView.Adapter<DayMealAdapter.ViewHolder> {

    //btn
    public interface OnItemClickListener {
        void onItemClick(View view, int pos);
    }

    // 리스너 객체 참조를 저장하는 변수
    private OnItemClickListener mListener = null ;

    // OnItemClickListener 리스너 객체 참조를 어댑터에 전달하는 메서드
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener ;
        this.context = context;
    }


    private ArrayList<One_food> food_list;
    private Context context;



    //---------------

    @NonNull
    @Override
    public DayMealAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.food_item, parent, false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull DayMealAdapter.ViewHolder holder, int position) {
        holder.onBind(food_list.get(position));

        holder.food_camera.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Log.w("TEST: ", "test camera" );


                Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                context.startActivity(cameraIntent);

            }

        });
    }

    public void setFoodList(ArrayList<One_food> list){
        this.food_list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return food_list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView food_img;
        TextView food_day;
        TextView food_na;
        TextView food_dan;
        TextView food_dang;
        TextView food_ma;
        TextView food_zin;
        TextView food_ene;
        TextView food_p;
        TextView food_wei;
        TextView food_fat;
        TextView food_fe;
        TextView food_k;
        TextView food_ca;
        TextView food_chol;
        TextView food_tan;
        TextView food_trans;

        Button food_camera;


        ViewHolder(View itemView) {
            super(itemView) ;

            // 뷰 객체에 대한 참조. (hold strong reference)
            food_img = (ImageView) itemView.findViewById(R.id.IV_food);
            food_day = (TextView) itemView.findViewById(R.id.text_day);
            food_na = (TextView) itemView.findViewById(R.id.text_na);
            food_dan = (TextView) itemView.findViewById(R.id.text_dan);
            food_dang = (TextView) itemView.findViewById(R.id.text_dang);
            food_ma = (TextView) itemView.findViewById(R.id.text_ma);
            food_zin = (TextView) itemView.findViewById(R.id.text_zin);
            food_ene = (TextView) itemView.findViewById(R.id.text_ene);
            food_p = (TextView) itemView.findViewById(R.id.text_p);
            food_wei = (TextView) itemView.findViewById(R.id.text_wei);
            food_fat = (TextView) itemView.findViewById(R.id.text_fat);
            food_fe = (TextView) itemView.findViewById(R.id.text_fe);
            food_k = (TextView) itemView.findViewById(R.id.text_k);
            food_ca = (TextView) itemView.findViewById(R.id.text_ca);
            food_chol = (TextView) itemView.findViewById(R.id.text_chol);
            food_tan = (TextView) itemView.findViewById(R.id.text_tan);
            food_trans = (TextView) itemView.findViewById(R.id.text_trans);

            food_camera = (Button) itemView.findViewById(R.id.btn_camera);
        }

        void onBind(One_food item){
            food_img.setImageResource(item.getFood_resourceId());
            food_day.setText(item.getDay());
            food_na.setText(item.getNa());
            food_dan.setText(item.getDan());
            food_dang.setText(item.getDang());
            food_ma.setText(item.getMa());
            food_zin.setText(item.getZin());
            food_ene.setText(item.getEne());
            food_p.setText(item.getP());
            food_wei.setText(item.getWei());
            food_fat.setText(item.getFat());
            food_fe.setText(item.getFe());
            food_k.setText(item.getK());
            food_ca.setText(item.getCa());
            food_chol.setText(item.getChol());
            food_tan.setText(item.getTan());
            food_trans.setText(item.getTrans());

        }
    }

}
728x90