728x90
https://pasongsong.tistory.com/201?category=1090989
- item position을 가져와야하기 때문에 다른 방식으로 바꾸기로 결정!
interface OnItemClickListener {
void onCameraClick(View v, int positon); //카메라
void onGalleryClick(View v, int positon); // 사진첩
void onDeleteClick(View v, int positon); // 삭제
}
private OnItemClickListener mListener = null;
//리스너 객체 참조를 어댑터에 전달 메서드
public void setOnItemClickListener(OnItemClickListener listener) {
this.mListener = listener;
}
- mainactivity에서 Click 을 위한 interface, 함수 만들기
ViewHolder에서 on Click 함수 만들기
변수보다 밑에 설정하기 그렇지 않으면 NULL error 발생
food_camera.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
int position = getAbsoluteAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
if (mListener != null) {
mListener.onCameraClick(v, position);
}
// Log.w("TEST: ", "test camera" );
// Intent in_camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// context.startActivity(in_camera);
}
}
});
food_del.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Log.w("TEST: ", "test del" );
int position = getAbsoluteAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
if (mListener != null) {
mListener.onDeleteClick(v, position);
}
}
}
});
Mainactivity에서 click 함수 만들기
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//recycler View 연결
RecyclerView one_food = (RecyclerView) findViewById(R.id.Recyc_food);
DayMealAdapter one_food_Adapter = new DayMealAdapter(meal_items, this);
one_food.setAdapter(one_food_Adapter);
one_food.setLayoutManager(new LinearLayoutManager(this));
meal_items.add(new One_food(R.drawable.food_1,"아침","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16"));
one_food_Adapter.setFoodList(meal_items);
//recyclerview click
one_food_Adapter.setOnItemClickListener(new DayMealAdapter.OnItemClickListener() {
@Override
public void onCameraClick(View v, int positon) {
Intent x = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(x, 101);
}
@Override
public void onGalleryClick(View v, int positon) {
}
@Override
public void onDeleteClick(View v, int positon) {
meal_items.remove(positon);
one_food_Adapter.notifyItemRemoved(positon);
}
});
}
Adapter 전체 코드
package com.example.k_food_application;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
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.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import androidx.recyclerview.widget.RecyclerView;
import org.apache.http.params.CoreConnectionPNames;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class DayMealAdapter extends RecyclerView.Adapter<DayMealAdapter.ViewHolder> {
private ArrayList<One_food> food_list;
private Context context;
private Uri photoUri;
private File photoFile;
interface OnItemClickListener {
void onCameraClick(View v, int positon); //카메라
void onGalleryClick(View v, int positon); // 사진첩
void onDeleteClick(View v, int positon); // 삭제
}
private OnItemClickListener mListener = null;
//리스너 객체 참조를 어댑터에 전달 메서드
public void setOnItemClickListener(OnItemClickListener listener) {
this.mListener = listener;
}
public DayMealAdapter(ArrayList<One_food> food_list, Context context) {
this.food_list = food_list;
this.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 in_camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// context.startActivity(in_camera);
// }
// });
//
// holder.food_del.setOnClickListener(new View.OnClickListener(){
// @Override
// public void onClick(View v){
// Log.w("TEST: ", "test camera" );
// Intent in_camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// context.startActivity(in_camera);
// }
// });
}
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;
Button food_del;
ViewHolder(@NonNull 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);
food_del = (Button) itemView.findViewById(R.id.btn_del);
food_camera.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
int position = getAbsoluteAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
if (mListener != null) {
mListener.onCameraClick(v, position);
}
// Log.w("TEST: ", "test camera" );
// Intent in_camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// context.startActivity(in_camera);
}
}
});
food_del.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Log.w("TEST: ", "test del" );
int position = getAbsoluteAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
if (mListener != null) {
mListener.onDeleteClick(v, position);
}
}
}
});
}
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
'Android' 카테고리의 다른 글
[Android Studio] 안드로이드 스튜디오 JAVA RecyclerView에서 버튼 intent 사용하기 (0) | 2022.10.09 |
---|---|
[Android Studio] 안드로이드 스튜디오 JAVA RecyclerView에서 Intent 사용하기 (0) | 2022.10.09 |
[Android Studio] 안드로이드 스튜디오 JAVA RecyclerView Button 클릭 이벤트 (0) | 2022.10.09 |
[Android Studio] 안드로이드 스튜디오 JAVA Recycler View class 만들어서 추가하기 (0) | 2022.10.09 |
[Android Studio] 안드로이드 스튜디오 Java class 생성 (0) | 2022.09.19 |