본문 바로가기

Android

리스트뷰 - 항목 추가,삭제하기 (multiplechoice)

activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <EditText

            android:id="@+id/et"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:hint="데이터 입력"

            android:inputType="text" />

 

        <Button

            android:id="@+id/btn1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="추가" />

 

        <Button

            android:id="@+id/btn2"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="삭제" />

    </LinearLayout>

 

    <ListView

        android:id="@+id/lv"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:choiceMode="multipleChoice">

    </ListView>

 

</LinearLayout>

 

 

MainActivity.java


package com.example.listviewex03;

 

import java.util.ArrayList;

 

 

import android.app.Activity;

import android.inputmethodservice.InputMethodService.InputMethodImpl;

import android.os.Bundle;

import android.util.SparseBooleanArray;

import android.view.Menu;

import android.view.View;

import android.view.inputmethod.InputMethodManager;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ListView;

 

public class MainActivity extends Activity {

     ArrayList<String> list;

     ArrayAdapter<String> adapter;

     ListView lv;

     InputMethodManager imm;

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

 

           list = new ArrayList<String>();

           list.add("01");

           list.add("02");

           list.add("03");

           list.add("04");

           list.add("05");

           list.add("06");

           list.add("07");

           list.add("08");

           list.add("09");

           list.add("10");

 

           //adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice, list);

           adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked, list);

          

           lv = (ListView) findViewById(R.id.lv);

           imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);

           lv.setAdapter(adapter);

          

           findViewById(R.id.btn1).setOnClickListener(clickListener);

           findViewById(R.id.btn2).setOnClickListener(clickListener);

     }

    

     private Button.OnClickListener clickListener = new Button.OnClickListener() {

     //private View.OnClickListener clickListener = new View.OnClickListener() {

 

           @Override

           public void onClick(View v) {

                // TODO Auto-generated method stub

                EditText et = (EditText)findViewById(R.id.et);

                if(v.getId() == R.id.btn1) {

                     // 추가 버튼

                     if(et.getText().length() != 0){

                           list.add(et.getText().toString());

                           et.setText("");

                           // 갱신되었음을 어댑터에 통보한다.

                           adapter.notifyDataSetChanged();

                           imm.hideSoftInputFromWindow(et.getWindowToken(), 0);

                    

                     }

                } else if(v.getId() == R.id.btn2){

                     // 삭제 버튼

                       // multi choice

                    

                    SparseBooleanArray sba = lv.getCheckedItemPositions();

                     if(sba.size() !=0 ){

                           for(int i=lv.getCount()-1 ; i>=0 ; i--){

                                if(sba.get(i)){

                                     list.remove(i);

                                }

                           }

                    

                           lv.clearChoices();

                           adapter.notifyDataSetChanged();

                          

                     }

                }

           }

     };

 

     @Override

     public boolean onCreateOptionsMenu(Menu menu) {

           // Inflate the menu; this adds items to the action bar if it is present.

           getMenuInflater().inflate(R.menu.main, menu);

           return true;

     }

 

}





 

'Android' 카테고리의 다른 글

항목 뷰 - 확장리스트뷰  (0) 2013.04.01
항목 뷰 - 커스텀 뷰  (0) 2013.04.01
리스트뷰 - 항목 추가, 삭제하기(singlechoice)  (0) 2013.04.01
리스트뷰 - choiceMode  (0) 2013.04.01
리스트뷰 - 어댑터뷰  (0) 2013.03.29