본문 바로가기

Android

ListView,삭제버튼,입력버튼

 

 

입력가능,삭제가능

<프로그램 설계>

리스트,삭제버튼,입력버튼


java>Main4Activity

package com.example.ex03;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class Main4Activity extends AppCompatActivity {
    ArrayList array;
    ArrayAdapter adapter;
    ListView list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);
        array=new ArrayList();
        array.add("짜장면");
        array.add("짬뽕");
        array.add("우동");
        array.add("만두");

        adapter= new ArrayAdapter(this,
                android.R.layout.simple_list_item_single_choice,array);
        list=findViewById(R.id.list);
        list.setAdapter(adapter);
        list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String str=array.get(position);
                Toast.makeText(Main4Activity.this,str,Toast.LENGTH_SHORT).show();
            }
        });
    }
    public void mClick(View v){
        EditText edit=findViewById(R.id.edit);
        switch(v.getId()){
            case R.id.btnadd:
                array.add(edit.getText().toString());
                adapter.notifyDataSetChanged();
                edit.setText("");
                break;
            case R.id.btndel:
                int pos=list.getCheckedItemPosition();
                if(pos != ListView.INVALID_POSITION){
                    array.remove(pos);
                    list.clearChoices();
                    adapter.notifyDataSetChanged();
                }
                break;
        }
    }




}


res>layout>activity_main4.xml

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
32
33
34
35
36
37
<?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"
    tools:context=".Main4Activity"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/edit"
            android:layout_width="0sp"
            android:layout_weight="4"
            android:layout_height="wrap_content"/>
        <Button
            android:onClick="mClick"
            android:id="@+id/btnadd"
            android:layout_width="0sp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="입력"/>
        <Button
            android:onClick="mClick"
            android:id="@+id/btndel"
            android:layout_width="0sp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="삭제"/>
    </LinearLayout>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list"/>
</LinearLayout>
 
 

 

manifests>AndroidManifest.xml
 (activity실행순서지정)

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
<?xml version="1.0" encoding="utf-8"?>
    package="com.example.ex03">
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".menu"></activity>
        <activity android:name=".Main3Activity" />
        <activity android:name=".Main2Activity" />
        <activity android:name=".MainActivity" /> <!-- 다수의 mainActivity 중 실행순서 정하기 -->
        <activity android:name=".Main4Activity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>
 
 

 

'Android' 카테고리의 다른 글

캘린더를 활용한 다이어리 앱 만들기  (0) 2019.11.11
주소록만들기(버튼,옵션메뉴)  (0) 2019.11.07
사칙연산 화면 만들기  (0) 2019.11.07
메뉴와 서브메뉴만들기  (0) 2019.11.07
사칙연산 화면 만들기  (0) 2019.11.05