PROGRAMMING

ListViewEdit 본문

Android

ListViewEdit

Raccoon2125 2020. 11. 4. 12:53
package com.example.a201104_2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ListView listView;
    ArrayList<String> items;
    Button btnAdd, btnModify, btnDelete;

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

        listView = findViewById(R.id.listView);

        items = new ArrayList<>();
        final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_single_choice, items);
        listView.setAdapter(adapter);

        btnAdd = findViewById(R.id.btnAdd);
        btnModify = findViewById(R.id.btnModify);
        btnDelete = findViewById(R.id.btnDelete);

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = adapter.getCount();
                items.add("LIST " + Integer.toString(count + 1));
                adapter.notifyDataSetChanged();
            }
        });

        btnModify.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) {
                        items.set(checked, Integer.toString(checked + 1) + "번 아이템 수정");
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        });

        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int checked, count;
                count = adapter.getCount();
                if (count > 0) {
                    checked = listView.getCheckedItemPosition();
                    if (checked > -1 && checked < count) {
                        items.remove(checked);
                        listView.clearChoices();
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }
}
<?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:id="@+id/topLayout"
    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_margin="10dp"/>

    <LinearLayout
        android:id="@+id/buttonLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnAdd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:text="Add" />

        <Button
            android:id="@+id/btnModify"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:text="Modify" />

        <Button
            android:id="@+id/btnDelete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Delete" />
    </LinearLayout>
</LinearLayout>

'Android' 카테고리의 다른 글

데이터 저장(이메일) & onDestroy()  (0) 2020.11.05
CustomListView  (0) 2020.11.04
ListView  (0) 2020.11.04
버튼-백그라운드컬러  (0) 2020.11.03
어플 접속화면(feat. 이상형월드컵)  (0) 2020.10.30
Comments