안드로이드 스튜디오

안드로이드 스튜디오 ListView 3 (추가, 삭제, 수정)

;세미콜론; 2020. 5. 2. 11:15
반응형
<?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">
    
    <EditText
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="TEXT"
        android:textSize="25dp" />
        
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

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

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

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

    <ListView
        android:id="@+id/View"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

    </ListView>

</LinearLayout>

 

package com.example.listview2;

import androidx.appcompat.app.AppCompatActivity;

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

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ListView listview;
    ArrayList<String> items;
    EditText text;
    Button add, modify, delete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = findViewById(R.id.text);
        add = findViewById(R.id.add);
        modify = findViewById(R.id.modify);
        delete = findViewById(R.id.delete);

        items = new ArrayList<>();
        final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_single_choice, items);
        listview = findViewById(R.id.View);
        listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listview.setAdapter(adapter);

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = adapter.getCount();
                items.add(Integer.toString(count + 1) + ") " + text.getText());
                adapter.notifyDataSetChanged();
                text.setText("");
            }
        });
        modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int check;
                int count = adapter.getCount();
                if (count > 0) {
                    check = listview.getCheckedItemPosition(); //선택 항목 position 얻기
                    if (check > -1 && check < count)
                        items.set(check, Integer.toString(check + 1) + ") " + text.getText() + " (수정됨)");
                    adapter.notifyDataSetChanged();
                    text.setText("");
                }
            }
        });
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int check, count= adapter.getCount();
                if (count>0){
                    check = listview.getCheckedItemPosition();
                    if (check>-1 && check<count){
                        items.remove(check);
                        listview.clearChoices();
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }
}

 

반응형