<activity_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=".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/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginRight="2dp"
android:text="Add" />
<Button
android:id="@+id/btnModify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="Modify" />
<Button
android:id="@+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="2dp"
android:text="Delete" />
</LinearLayout>
</LinearLayout>
<MainActivity.java>
package com.example.listviewedit;
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);
items = new ArrayList<String>();
final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_single_choice, items);
listView = findViewById(R.id.listView);
listView.setAdapter(adapter);
btnAdd = findViewById(R.id.btnAdd);
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 = findViewById(R.id.btnModify);
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 = findViewById(R.id.btnDelete);
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int count, checked;
count = adapter.getCount();
if (count > 0) {
checked = listView.getCheckedItemPosition();
// 현재 선택된 아이템의 position 획득.
checked = listView.getCheckedItemPosition();
if (checked > -1 && checked < count) {
// 아이템 삭제
items.remove(checked);
// listview 선택 초기화.
listView.clearChoices();
// listview 갱신
adapter.notifyDataSetChanged();
}
}
}
});
}
}
'Android' 카테고리의 다른 글
안드로이드 - 이메일 저장 (0) | 2020.11.05 |
---|---|
project - 오토믹 자동차 (0) | 2020.11.04 |
안드로이드 - 영화 리스트 만들기 (0) | 2020.11.04 |
안드로이드 - toast 클릭 시 클릭한 것 뜨게하기 (여러 개 가능) (0) | 2020.11.04 |
버튼, 라디오버튼 만들어서 클릭 시 색 변환 (0) | 2020.11.03 |