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 |
Tags
- struts2
- 클래스
- 이클립스
- Spring
- 어노테이션
- 오버로딩
- 배열
- 안드로이드
- 예외처리
- 국제화
- HTML
- 메서드
- JSP
- 전화걸기
- OGNL
- layout
- Java
- 메소드
- 생성자
- JavaScript
- Eclips
- paint
- oracle
- mybatis
- Android
- 에러페이지
- Menu
- AWT
- Graphic
- 기본
Archives
- Today
- Total
note
ListActivity 사용 본문
23ListViewDemo2
main.xml
24ListViewDemo3
main.xml
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- hint = 입력시 없어짐(입력하기 전에 보여지는 텍스트) -->
<EditText
android:id="@+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="새 해야 할 일" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false" />
</LinearLayout>
package kr.android.listview2;
//ListActivity 사용하기 ArrayList사용하여 추가 삭제기능
import java.util.ArrayList;
import android.app.ListActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.os.Bundle;
public class ListViewDemo2 extends ListActivity implements OnKeyListener {
//이벤트 리스너
ArrayList todoItems;
ArrayAdapter aa;
EditText myEditText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//이벤트 소스
myEditText = (EditText) findViewById(R.id.myEditText);
//데이터 저장소 생성
todoItems = new ArrayList();
//ArrayList와 ListView 데이터 바인딩
aa = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, todoItems);
//ArrayAdapter를 ListView에 등록
setListAdapter(aa);
//이벤트 소스와 이벤트 리스너가 구현된 객체 연결
myEditText.setOnKeyListener(this);
}
//전달된 인자
//View v : 이벤트 발생 객체
//int keyCode : 입력한 키
//KeyEvent event : KeyEvent 객체
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
//저장소에 데이터 저장(index 0에 데이터 저장)만약 기존 데이터가 있으면,
//신규 데이터는 index0에 저장 기존데이터는 다음 index에 저장
todoItems.add(0, myEditText.getText().toString());
//데이터 저장소로부터 데이터를 새롭게 읽어들여서 ListView를 갱신
aa.notifyDataSetChanged();
//빈문자열로 다시 만들어 입력을 받게끔
myEditText.setText("");
return true;
}
}
return false;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/newitem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Add" />
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Del" />
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice" />
</LinearLayout>
package kr.android.listview3;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class ListViewDemo3 extends ListActivity {
ArrayList items;
ArrayAdapter adapter;
ListView list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//데이터 저장소 생성 및 초기 데이터 설정
items = new ArrayList();
items.add("First");
items.add("Second");
items.add("Third");
//ArrayList에 있는 데이터를 ListView에 바인딩
adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_single_choice, items);
//ArrayAdapter를 ListView에 등록
setListAdapter(adapter);
//onClick에 두개를 진입하기 위해 설정 (이렇게 안하면 코드가2배)
//이벤트 소스와 이벤트를 처리해주는 객체 연결
findViewById(R.id.add).setOnClickListener(mClickListener);
findViewById(R.id.delete).setOnClickListener(mClickListener);
}
//익명내부 클래스 형태의 이벤트 처리
Button.OnClickListener mClickListener = new View.OnClickListener(){
public void onClick(View v){
EditText ed = (EditText)findViewById(R.id.newitem);
switch(v.getId()){ //이벤트가 발생한 객체의 id
case R.id.add:
String text = ed.getText().toString();
if(text.length() != 0){
items.add(0,text);
ed.setText("");
adapter.notifyDataSetChanged();
}
break;
case R.id.delete:
int id;
//현재 동작하는 ListView객체 반환
list = getListView();
id=list.getCheckedItemPosition();
//getCheckedItemPosition()으로부터 반환받은
//position이 정상적인 position인지 검증
if(id != ListView.INVALID_POSITION){
//데이터 삭제
//ArrayList 데이터 삭제
items.remove(id);
//라디오 버튼을 선택하지 않은 것으로 초기화
list.clearChoices();
//ArrayList에 변경된 데이터를 다시 읽어들여
//ListView를 갱신
adapter.notifyDataSetChanged();
}
break;
}
}
};
}
'Android > 기본' 카테고리의 다른 글
| 그리드 Grid (0) | 2012.01.09 |
|---|---|
| 스핀 컨트롤 Spinner (0) | 2012.01.09 |
| ListActivity ArrayAdapter (0) | 2012.01.09 |
| Android Table Layout (0) | 2012.01.06 |
| Android Frame Layout (0) | 2012.01.06 |