2swan
RecyclerView 추가, 전체보기 본문
main.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=".MainActivity3"
android:orientation="vertical"
android:layout_margin="10dp">
<Button
android:id="@+id/btnList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="전체보기"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floatBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:srcCompat="@android:drawable/ic_input_add"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
item_phone3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txtName"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:textSize="20sp"/>
<TextView
android:id="@+id/txtTel"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:textSize="20sp"/>
</LinearLayout>
layout_add3.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"
android:padding="10dp">
<EditText
android:id="@+id/etname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="이름"
android:textSize="20sp"/>
<EditText
android:id="@+id/ettel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="전화번호"
android:textSize="20sp"/>
</LinearLayout>
main.activity
public class MainActivity3 extends AppCompatActivity {
private PhoneAdapter3 phoneAdapter3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Button btnList = findViewById(R.id.btnList);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
FloatingActionButton floatingActionButton = findViewById(R.id.floatBtn);
//추가
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
View dialogView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.layout_add3,null);
EditText etname = dialogView.findViewById(R.id.etname);
EditText ettel = dialogView.findViewById(R.id.ettel);
AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity3.this);
dlg.setTitle("등록");
dlg.setView(dialogView);
dlg.setPositiveButton("등록", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Phone3 p = new Phone3(etname.getText().toString(),ettel.getText().toString());
phoneAdapter3.addItem(p);
}
});
dlg.setNegativeButton("닫기",null);
dlg.show();
}
});
//전체보기
btnList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// ArrayList<Phone3> phone3List = new ArrayList<>();
phoneAdapter3 = new PhoneAdapter3();
for(int i=0; i<10; i++){
phoneAdapter3.addItem(new Phone3("홍길동"+i,"010-1111-2222"));
}
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity3.this,
RecyclerView.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(phoneAdapter3);
}
});
}
}
Phone3.java
public class Phone3 {
private Long id;
private String name;
private String tel;
public Phone3(String name, String tel) {
this.name = name;
this.tel = tel;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
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;
}
}
PhoneAdapter3.java
public class PhoneAdapter3 extends RecyclerView.Adapter<PhoneAdapter3.PhoneViewHolder> {
private ArrayList<Phone3>phone3List = new ArrayList<>();
// //추가
// public PhoneAdapter3(ArrayList<Phone3> phone3List){
// this.phone3List = phone3List;
// }
//삭제
public void removeItem(int position){
phone3List.remove(position);
notifyDataSetChanged();
}
//추가
public void addItem(Phone3 phone3){
phone3List.add(phone3);
}
//수정
public void updateItem(Phone3 phone3, int position){ //phone3 수정 내용
Phone3 p = phone3List.get(position); //수정할 객체
p.setName(phone3.getName());
p.setTel(phone3.getTel());
notifyDataSetChanged();
}
@NonNull
@Override
public PhoneAdapter3.PhoneViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from((parent.getContext()));
View view = inflater.inflate(R.layout.item_phone3, parent, false);
return new PhoneViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull PhoneAdapter3.PhoneViewHolder holder, int position) {
Phone3 phone3 = phone3List.get(position);
holder.txtName.setText(phone3.getName());
holder.txtTel.setText(phone3.getTel());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
View dialogView =view.inflate(view.getContext(),R.layout.layout_add3,null);
EditText etName = dialogView.findViewById(R.id.etname);
EditText etTel = dialogView.findViewById(R.id.ettel);
etName.setText(phone3.getName());
etTel.setText(phone3.getTel());
AlertDialog.Builder dlg = new AlertDialog.Builder(view.getContext());
dlg.setTitle("수정");
dlg.setView(dialogView);
dlg.setPositiveButton("수정", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Phone3 phone = new Phone3(etName.getText().toString(),
etTel.getText().toString());
updateItem(phone,holder.getAdapterPosition()); //수정메소드
}
});
dlg.setNegativeButton("삭제", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.d("<<<삭제", "삭제");
removeItem(holder.getAdapterPosition());
}
});
dlg.show();
}
});
}
@Override
public int getItemCount() {
return phone3List == null ?0:phone3List.size();
}
public class PhoneViewHolder extends RecyclerView.ViewHolder {
TextView txtName, txtTel;
public PhoneViewHolder(@NonNull View itemView) {
super(itemView);
txtName = itemView.findViewById(R.id.txtName);
txtTel = itemView.findViewById(R.id.txtTel);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = getAdapterPosition();
}
});
}
}
}
결과 값
'Programming > Android Example' 카테고리의 다른 글
가로(세로) 슬라이드 (0) | 2023.08.09 |
---|---|
RecyclerView 예제2 (0) | 2023.08.09 |
RecyclerView 예제 (0) | 2023.08.09 |
RecyclerView Image (0) | 2023.08.09 |
SQLite 일기장 앱 (0) | 2023.08.09 |