본문 바로가기

PROGRAM/Android

ListView 아이템 추가,수정,삭제

source : https://recipes4dev.tistory.com/48

 

안드로이드 리스트뷰 아이템 추가,수정,삭제. (Android ListView Item Add,Modify,Delete)

1. ListView 아이템 다루기 지금까지의 ListView예제들에서는 Activity의 onCreate()함수에서 아이템에 표시될 데이터를 Adapter에 미리 적용하는 방식만을 사용했습니다. 즉, 최초 ListView 아이템을 구성하고

recipes4dev.tistory.com

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:choiceMode="singleChoice"
        android:layout_marginBottom="10dp"/>

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

MainActivity.java

public class MainActivity extends AppCompatActivity {

    ListView listview;
    ArrayList<String> items;

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

        // 빈 데이터 리스트 생성.
        items = new ArrayList<String>() ;
        // ArrayAdapter 생성. 아이템 View를 선택(single choice)가능하도록 만듦.
        final ArrayAdapter adapter = new ArrayAdapter(this, 
        				android.R.layout.simple_list_item_single_choice, items) ;

        // listview 생성 및 adapter 지정.
        listview = findViewById(R.id.listview);
        listview.setAdapter(adapter) ;

        Button btn_add = (Button)findViewById(R.id.btn_add) ;
        btn_add.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                int count = adapter.getCount();
                // 아이템 추가.
                items.add("LIST" + Integer.toString(count + 1));
                // listview 갱신
                adapter.notifyDataSetChanged();
            }
        }) ;

        // modify button에 대한 이벤트 처리.
        Button btn_modify = (Button)findViewById(R.id.btn_modify) ;
        btn_modify.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                int checked ;
                int count = adapter.getCount() ;

                if (count > 0) {
                    // 현재 선택된 아이템의 position 획득.
                    checked = listview.getCheckedItemPosition();
                    if (checked > -1 && checked < count) {
                        // 아이템 수정
                        items.set(checked, Integer.toString(checked+1) + "번 아이템 수정") ;

                        // listview 갱신
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        });

        // delete button에 대한 이벤트 처리.
        Button btn_delete = (Button)findViewById(R.id.btn_delete) ;
        btn_delete.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                int count, checked ;
                count = adapter.getCount() ;

                if (count > 0) {
                    // 현재 선택된 아이템의 position 획득.
                    checked = listview.getCheckedItemPosition();

                    if (checked > -1 && checked < count) {
                        // 아이템 삭제
                        items.remove(checked) ;

                        // listview 선택 초기화.
                        listview.clearChoices();

                        // listview 갱신.
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        }) ;

    }
}

'PROGRAM > Android' 카테고리의 다른 글

설명문 띄우기  (0) 2020.05.08
Fragment 예제  (0) 2020.05.08
ListView -02 (extends ListActivity)  (0) 2020.04.30
ListView 추가하기 - 01  (0) 2020.04.30
Android - listview  (0) 2020.04.28