Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- jinja2
- bootstrap
- 콘솔 가상환경 # 콘솔 #가상환경
- PyQt5
- #ifdef
- Action
- DB 데이터
- javascript
- 실시간 시계
- #endif
- 환경변수 설정
- heroku
- 튜토리얼
- VS Code
- OpenCV
- 사이트 도메인
- #if
- href
- OpenCV + Flask
- Django
- DB 데이터 저장
- #undef
- 명령어
- bootstrap4 패키지
- MySQL 세팅
- 성능지표
- #else
- flask
- openweathermap
- #ifndef
Archives
- Today
- Total
PROGRAMMING
ListViewEdit 본문
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