<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" 
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TableLayout 
		android:layout_width="fill_parent" 
		android:layout_height="wrap_content"
		android:stretchColumns="1"
		>
		<TableRow>
			<TextView 
				android:id="@+id/num" 
				android:layout_width="50dp"
				android:layout_height="wrap_content" 
				android:text="번호" />
	
			<TextView
				android:id="@+id/text_num" 
				android:layout_width="fill_parent"
				android:layout_height="wrap_content"
				 />
			</TableRow>
		<TableRow>
			<TextView 
				android:id="@+id/name" 
				android:layout_width="50dp"
				android:layout_height="wrap_content" 
				android:text="이름" />
	
			<EditText
				android:id="@+id/edit_name" 
				android:layout_width="fill_parent"
				android:layout_height="wrap_content"
				 />
			</TableRow>
		<TableRow>
			<TextView 
				android:id="@+id/korean" 
				android:layout_width="50dp"
				android:layout_height="wrap_content" 
				android:text="국어" />
	
			<EditText
				android:id="@+id/edit_korean" 
				android:layout_width="fill_parent"
				android:layout_height="wrap_content" 
				android:inputType="numberDecimal"
				 />
			</TableRow>
		<TableRow>
			<TextView 
				android:id="@+id/english" 
				android:layout_width="50dp"
				android:layout_height="wrap_content" 
				android:text="영어" />
	
			<EditText
				android:id="@+id/edit_english" 
				android:layout_width="fill_parent"
				android:layout_height="wrap_content" 
				android:inputType="numberDecimal"
				 />
			</TableRow>
		<TableRow>
			<TextView 
				android:id="@+id/math" 
				android:layout_width="50dp"
				android:layout_height="wrap_content" 
				android:text="수학" />
	
			<EditText
				android:id="@+id/edit_math" 
				android:layout_width="fill_parent"
				android:layout_height="wrap_content" 
				android:inputType="numberDecimal"
				 />
			</TableRow>
	</TableLayout>
	<LinearLayout
	    android:layout_width="fill_parent" 
		android:layout_height="wrap_content"
		>
		<Button 
				android:id="@+id/add" 
				android:layout_width="fill_parent"
				android:layout_height="wrap_content" 
				android:layout_weight="1"
				android:text="등록"
				 />
		<Button 
				android:id="@+id/modify" 
				android:layout_width="fill_parent"
				android:layout_height="wrap_content" 
				android:layout_weight="1"
				android:text="수정"
				 />
		<Button 
				android:id="@+id/del" 
				android:layout_width="fill_parent"
				android:layout_height="wrap_content" 
				android:layout_weight="1"
				android:text="삭제"
				 />
	</LinearLayout>		 
	<ListView 
		android:id="@android:id/list" 
		android:layout_width="fill_parent"
		android:layout_height="fill_parent"
		android:choiceMode="singleChoice"
		 />
</LinearLayout>


package kr.android.score3;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
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;
import android.widget.TextView;
import android.widget.Toast;

public class Score3 extends ListActivity {


	private static final String SCORE_DATA = "score.txt";
	ArrayList<string> score_items;
	ArrayList<string> items;
	ArrayAdapter<string> adapter;
	EditText name,korean,english,math;
	TextView text;
	int sum, avg, num;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		score_items = new ArrayList<string>();
		items = new ArrayList<string>();

		adapter = 
				new ArrayAdapter<string>(
						this, android.R.layout.simple_list_item_single_choice, items);
				setListAdapter(adapter);

				findViewById(R.id.add).setOnClickListener(mClickListener);
				findViewById(R.id.modify).setOnClickListener(mClickListener);
				findViewById(R.id.del).setOnClickListener(mClickListener);

				text = (TextView)findViewById(R.id.text_num);

				name = (EditText)findViewById(R.id.edit_name);
				korean = (EditText)findViewById(R.id.edit_korean);
				english = (EditText)findViewById(R.id.edit_english);
				math = (EditText)findViewById(R.id.edit_math);
	} 

	public void onResume() {
		super.onResume();

		try {
			InputStream in=openFileInput(SCORE_DATA);

			if (in!=null) {
				InputStreamReader tmp=new InputStreamReader(in);
				BufferedReader reader=new BufferedReader(tmp);
				String str;
				String[] scoredb;

				while ((str = reader.readLine()) != null) {
					scoredb = str.split("\\|");
					score_items.add(str);
					items.add(scoredb[0]+" (총점 : "+scoredb[4]+", 평균 : "+scoredb[5]+")");
				}

				in.close();
			}
		}
		catch (java.io.FileNotFoundException e) {
			// 아직 저장된 내용이 없을 뿐, 문제는 없다.
		}
		catch (Throwable t) {
			Toast
			.makeText(this, "예외: "+t.toString(), 2000)
			.show();
		}
	}


	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		num = position;
		text.setText(position+"");
	}

	public void saveData() {

		try {
			OutputStreamWriter out=
					new OutputStreamWriter(openFileOutput(SCORE_DATA, MODE_PRIVATE));

			StringBuffer sb = new StringBuffer();
			for(String str : score_items){
				sb.append(str+"\n");
			}

			out.write(sb.toString());
			out.close();		
			Toast.makeText(this, "데이터 처리 완료", 4000).show();
		}
		catch (Throwable t) {
			Toast
			.makeText(this, "예외: "+t.toString(), 2000)
			.show();
		}
	}

	public void uiSetChanged(){
		name.setText("");
		korean.setText("");
		english.setText("");
		math.setText("");
		getListView().clearChoices();
		adapter.notifyDataSetChanged();
		saveData();
	}

	Button.OnClickListener mClickListener = new View.OnClickListener() {
		public void onClick(View v) {
			if(v.getId() == R.id.add){

				String name2 = name.getText().toString();
				String korean2 = korean.getText().toString();
				String english2 = english.getText().toString();
				String math2 = math.getText().toString();
				if (name2 !=null && korean2 !=null && english2 !=null && math2 !=null) {
					sum = Integer.parseInt(korean2)+
							Integer.parseInt(english2)+
							Integer.parseInt(math2);
					avg = sum/3;

					score_items.add(0,name2+"|"+korean2+"|"+english2+"|"+math2+"|"+sum+"|"+avg);
					items.add(0,name2+" (총점 : "+sum+",평균 : " + avg + ")");
					uiSetChanged();
				}   
			}else if(v.getId() == R.id.modify){
				String name2 = name.getText().toString();
				String korean2 = korean.getText().toString();
				String english2 = english.getText().toString();
				String math2 = math.getText().toString();
				if (name2 !=null && korean2 !=null && english2 !=null && math2 !=null) {
					sum = Integer.parseInt(korean2)+
							Integer.parseInt(english2)+
							Integer.parseInt(math2);
					avg = sum/3;

				score_items.set(num,name2+"|"+korean2+"|"+english2+"|"+math2+"|"+sum+"|"+avg);
				items.set(num,name2+" (총점 : "+sum+",평균 : " + avg + ")");
				uiSetChanged();
				} 
			}else{
				ListView list = getListView();
				int id=list.getCheckedItemPosition();
				if (id != ListView.INVALID_POSITION) {
					score_items.remove(id);
					items.remove(id);
					uiSetChanged();
				}

			}
		}
	};
	/*
	protected void onPause() {
		super.onPause();
		saveData();
	}
	 */

}




'Android > 개발&Application' 카테고리의 다른 글

예비  (0) 2012.04.28
한글 대탐험 : 안드로이드 어플 개발  (0) 2012.04.28
Project 어플 개발 : U-Alarm  (0) 2012.04.28
사칙연산 Spinner와 Radio버튼으로 하기  (0) 2012.04.28
계산기  (0) 2012.04.28

+ Recent posts