2swan

RecyclerView 예제2 본문

Programming/Android Example

RecyclerView 예제2

2swan 2023. 8. 9. 13:50

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"
    android:orientation="vertical"
    tools:context=".MainActivity4">

    <EditText
        android:id="@+id/etName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="이름"/>

    <EditText
        android:id="@+id/etTel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="전화번호"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnInsert"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="추가"/>

        <Button
            android:id="@+id/btnUpdate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="수정"
            android:enabled="false"/>

        <Button
            android:id="@+id/btnDelete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="삭제"
            android:enabled="false"/>

    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

 

item_phone4.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>

 

main.activity

public class MainActivity4 extends AppCompatActivity {
    private  int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);

        EditText etName = findViewById(R.id.etName);
        EditText etTel = findViewById(R.id.etTel);

        Button btnInsert = findViewById(R.id.btnInsert);
        Button btnUpdate = findViewById(R.id.btnUpdate);
        Button btnDelete = findViewById(R.id.btnDelete);

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        PhoneAdapter4 phoneAdapter4 = new PhoneAdapter4();

        //전체보기
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext(),
                RecyclerView.VERTICAL, false);
        for(int i = 0; i < 7; i++){
            phoneAdapter4.addItem(new Phone4("이순신"+"("+i+")","010-1234-5678"));
        }
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(phoneAdapter4);

        //phoneAdapter4.setOn~
        phoneAdapter4.setOnItemClickListener(new PhoneAdapter4.OnItemClickListener() {
            @Override
            public void onItemClick(int pos) {
//                Log.d("pospos : ", pos+"");
                Phone4 phone4 = phoneAdapter4.getItem(pos);
                Toast.makeText(getApplicationContext(),phone4.getName()+"선택됨", Toast.LENGTH_SHORT).show();

                etName.setText(phone4.getName());
                etTel.setText(phone4.getTel());
                btnUpdate.setEnabled(true);
                btnDelete.setEnabled(true);
            }
        });

        //추가
        btnInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Phone4 phone4 = new Phone4(etName.getText().toString(),
                        etTel.getText().toString());
                phoneAdapter4.addItem(phone4);
                etName.setText("");
                etTel.setText("");
            }
        });

        //수정
        btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Phone4 phone4 = new Phone4(etName.getText().toString(),
                        etTel.getText().toString());
                phoneAdapter4.updateItem(phone4,position);
                etName.setText("");
                etTel.setText("");
                btnUpdate.setEnabled(false);
                btnDelete.setEnabled(false);
            }
        });

        //삭제
        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                phoneAdapter4.removeItem(position);
                btnUpdate.setEnabled(false);
                btnDelete.setEnabled(false);
            }
        });



    }
}

 

Phone4.java

public class Phone4 {
    private String name;
    private String tel;

    public Phone4(String name, String tel) {
        this.name = name;
        this.tel = tel;
    }

    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;
    }
}

 

PhoneAdapter4.java

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

    private ArrayList<Phone4>phone4List = new ArrayList<>();

    //인터페이스
    public interface  OnItemClickListener{
        void onItemClick(int pos);
    }

    //전체보기
    public Phone4 getItem(int position) {
        return phone4List.get(position);
    }

    //추가
    public void addItem(Phone4 phone4){
        phone4List.add(phone4);
        notifyDataSetChanged();
    }

    //수정
    public void updateItem(Phone4 phone4, int position) {
        Phone4 p = phone4List.get(position);
        p.setName(phone4.getName());
        p.setTel(phone4.getTel());
        notifyDataSetChanged();
    }

    //삭제
    public void removeItem(int position) {
        phone4List.remove(position);
        notifyDataSetChanged();
    }


    private  OnItemClickListener onItemClickListener;
    //setter
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }










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

    @Override
    public void onBindViewHolder(@NonNull PhoneAdapter4.ViewHolder holder, int position) {
        Phone4 phone4 = phone4List.get(position);
        holder.setItem(phone4);
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView txtName, txtTel;
        public ViewHolder(@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();
                    onItemClickListener.onItemClick(position);
                }
            });
        }

        public void setItem(Phone4 phone4) {
            txtName.setText(phone4.getName());
            txtTel.setText(phone4.getTel());
        }

    }
}

결과 값

'Programming > Android Example' 카테고리의 다른 글

슬라이드 예제 2  (0) 2023.08.09
가로(세로) 슬라이드  (0) 2023.08.09
RecyclerView 추가, 전체보기  (0) 2023.08.09
RecyclerView 예제  (0) 2023.08.09
RecyclerView Image  (0) 2023.08.09