ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [안드로이드] ListView 이벤트처리2 -리스트추가,수정,삭제
    Android/공부 2020. 5. 26. 16:43
    Listview 이벤트 처리 - 리스트 추가, 수정, 삭제 구현

     

     

     

     

     

     

     

    1. xml

    1. activity_main.xml

    • 레이아웃에 노출시킬 리스트뷰 1개와 리스트를 조작할 버튼을 추가한다.

    <?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=".MainActivity">
    
        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/listView"/>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <Button
                android:id="@+id/btn_add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="add"/>
    
            <Button
                android:id="@+id/btn_modify"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="modify"/>
    
            <Button
                android:id="@+id/btn_delete"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="delete"/>
        </LinearLayout>
    
    </LinearLayout>

       

     

     


    2. java

    1. MainActivity.java

     

     

    public class MainActivity extends AppCompatActivity {
    
        ListView listView;
        Button btn_add, btn_modify, btn_delete;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            listView = findViewById(R.id.listView);
            btn_add = findViewById(R.id.btn_add);
            btn_modify = findViewById(R.id.btn_modify);
            btn_delete = findViewById(R.id.btn_delete);
    
            final ArrayList<String> data = new ArrayList<String>();
            final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_single_choice, data);
            listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            listView.setAdapter(adapter);
    • xml의 리스트뷰와 버튼을 검색해서 연결해주고, 리스트뷰와 어댑터를 연결한다.

    • 리스트뷰는 아이템 view를 선택(single choice) 할 수 있도록 만든다.

     

     

    btn_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int count = adapter.getCount();
                    data.add("List"+Integer.toString(count+1));
                    adapter.notifyDataSetChanged();
                }
            });
    • 리스트뷰를 추가하는 코드

    • 어댑터에서 현재 만들어져 있는 리스트뷰의 개수를 세고 다음번째 리스트를 만든다.

    • 리스트를 추가할때는 ( 리스트 이름. add )를 사용한다.

     

     

    btn_modify.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int checked;
                    int count = adapter.getCount();
                    if ( count>0 ) {
                        checked = listView.getCheckedItemPosition();
                        if ( checked>-1 && checked<count ) {
                            data.set(checked, Integer.toString(checked+1)+"번 아이템 수정");
                            adapter.notifyDataSetChanged();
                        }
                    }
                }
            });
    • 리스트 뷰의 리스트가 1개 이상 생성되어있을 때, 클릭한 리스트의 포지션 값을 받아 데이터를 수정하도록 한다.

    • 리스트의 수정은 set 함수를 사용한다.

     

     

     

    btn_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int count, checked;
                    count = adapter.getCount();
                    if ( count>0 ) {
                        checked = listView.getCheckedItemPosition();
                        if ( checked>-1 && checked<count ) {
                            data.remove(checked);
                            Toast.makeText(getApplicationContext(),
                                    (checked+1)+"번 아이템 삭제", Toast.LENGTH_SHORT).show();
                            listView.clearChoices();
                            adapter.notifyDataSetChanged();
                        }
                    }
                }
            });
    • 리스트를 지울 때는 remove 함수를 사용한다.

    • 리스트는 선택 리스트로 만들었기 때문에 데이터가 지워진 후에 선택 초기화를 실행시켜준다.

     

     

     

    • 모든 이벤트 실행 후에는 adapter.notifyDataSetChanged() 함수로 어댑터를 갱신 시켜준다.


     

     

    댓글

Designed by Tistory.