'Calendar'에 해당되는 글 1건

  1. 2012.01.18 안드로이드 Calendar 예제 따라하기 (39)
JAVA/Android2012.01.18 15:36
- GridView를 이용해서 달력을 만들고 Database를 이용해 일정을 입력,삭제,수정 할 수 있는 프로그램을
만들어 보자.
- 이 예제를 통해서 달력 및 일정관리 어플리케이션을 만들어 볼 수 있다.

-Java 소스
public class Ex11_CalendarActivity extends Activity implements OnClickListener,
OnItemClickListener {
ArrayList<String> mItems;
ArrayAdapter<String> adapter;
TextView textYear;
TextView textMon;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

textYear = (TextView) this.findViewById(R.id.edit1);
textMon = (TextView) this.findViewById(R.id.edit2);

mItems = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mItems);

GridView gird = (GridView) this.findViewById(R.id.grid1);
gird.setAdapter(adapter);
gird.setOnItemClickListener(this);

Date date = new Date();// 오늘에 날짜를 세팅 해준다.
int year = date.getYear() + 1900;
int mon = date.getMonth() + 1;
textYear.setText(year + "");
textMon.setText(mon + "");

fillDate(year, mon);

Button btnmove = (Button) this.findViewById(R.id.bt1);
btnmove.setOnClickListener(this);

}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (arg0.getId() == R.id.bt1) {
int year = Integer.parseInt(textYear.getText().toString());
int mon = Integer.parseInt(textMon.getText().toString());
fillDate(year, mon);
}

}

private void fillDate(int year, int mon) {
mItems.clear();

mItems.add("일");
mItems.add("월");
mItems.add("화");
mItems.add("수");
mItems.add("목");
mItems.add("금");
mItems.add("토");

Date current = new Date(year - 1900, mon - 1, 1);
int day = current.getDay(); // 요일도 int로 저장.

for (int i = 0; i < day; i++) {
mItems.add("");
}

current.setDate(32);// 32일까지 입력하면 1일로 바꿔준다.
int last = 32 - current.getDate();

for (int i = 1; i <= last; i++) {
mItems.add(i + "");
}
adapter.notifyDataSetChanged();

}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
if (mItems.get(arg2).equals("")) {
;
} else {
Intent intent = new Intent(this, ExToday.class);//해당 일을 눌렸을때
intent.putExtra("Param1", textYear.getText().toString() + "/" 
+ textMon.getText().toString() + "/" + mItems.get(arg2));
startActivity(intent);
}
}
}
--------------------------------------------------------------------------------------------------------
public class ExToday extends Activity implements OnItemClickListener,
OnClickListener { //오늘의 일정 목록 띄우기
MyDBHelper mDBHelper;
String today;
Cursor cursor;
SimpleCursorAdapter adapter;
ListView list;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.extoday);

Intent intent = getIntent();
today = intent.getStringExtra("Param1");

TextView text = (TextView) findViewById(R.id.texttoday);
text.setText(today);

mDBHelper = new MyDBHelper(this, "Today.db", null, 1);
SQLiteDatabase db = mDBHelper.getWritableDatabase();

cursor = db.rawQuery(
"SELECT * FROM today WHERE date = '" + today + "'", null);

adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, cursor, new String[] {
"title", "time" }, new int[] { android.R.id.text1,
android.R.id.text2 });

ListView list = (ListView) findViewById(R.id.list1);
list.setAdapter(adapter);
list.setOnItemClickListener(this);

mDBHelper.close();

Button btn = (Button) findViewById(R.id.btnadd);
btn.setOnClickListener(this);

}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Intent intent = new Intent(this, Detail.class);
cursor.moveToPosition(position);
intent.putExtra("ParamID", cursor.getInt(0));
startActivityForResult(intent, 0);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(this, Detail.class);
intent.putExtra("ParamDate", today);
startActivityForResult(intent, 1);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
// super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 0:
case 1:
if (resultCode == RESULT_OK) {
// adapter.notifyDataSetChanged();
SQLiteDatabase db = mDBHelper.getWritableDatabase();
cursor = db.rawQuery("SELECT * FROM today WHERE date = '"
+ today + "'", null);
adapter.changeCursor(cursor);
mDBHelper.close();
}
break;
}
}
}
------------------------------------------------------------------------------------------------------- 
public class Detail extends Activity implements OnClickListener { // 일정목록 추가하기
MyDBHelper mDBHelper;
int mId;
String today;
EditText editDate, editTitle, editTime, editMemo;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);

editDate = (EditText) findViewById(R.id.editdate);
editTitle = (EditText) findViewById(R.id.edittitle);
editTime = (EditText) findViewById(R.id.edittime);
editMemo = (EditText) findViewById(R.id.editmemo);

Intent intent = getIntent();
mId = intent.getIntExtra("ParamID", -1);
today = intent.getStringExtra("ParamDate");

mDBHelper = new MyDBHelper(this, "Today.db", null, 1);

if (mId == -1) {
editDate.setText(today);
} else {
SQLiteDatabase db = mDBHelper.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM today WHERE _id='" + mId
+ "'", null);

if (cursor.moveToNext()) {
editTitle.setText(cursor.getString(1));
editDate.setText(cursor.getString(2));
editTime.setText(cursor.getString(3));
editMemo.setText(cursor.getString(4));
}
mDBHelper.close();
}

Button btn1 = (Button) findViewById(R.id.btnsave);
btn1.setOnClickListener(this);
Button btn2 = (Button) findViewById(R.id.btndel);
btn2.setOnClickListener(this);
Button btn3 = (Button) findViewById(R.id.btncancel);
btn3.setOnClickListener(this);

if (mId == -1) {
btn2.setVisibility(View.INVISIBLE);

}
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SQLiteDatabase db = mDBHelper.getWritableDatabase();

switch (v.getId()) {
case R.id.btnsave:
if (mId != -1) {
db.execSQL("UPDATE today SET title='"
+ editTitle.getText().toString() + "',date='"
+ editDate.getText().toString() + "', time='"
+ editTime.getText().toString() + "', memo='"
+ editMemo.getText().toString() + "' WHERE _id='" + mId
+ "';");
} else {
db.execSQL("INSERT INTO today VALUES(null, '"
+ editTitle.getText().toString() + "', '"
+ editDate.getText().toString() + "', '"
+ editTime.getText().toString() + "', '"
+ editMemo.getText().toString() + "');");
}
mDBHelper.close();
setResult(RESULT_OK);
break;
case R.id.btndel:
if (mId != -1) {
db.execSQL("DELETE FROM today WHERE _id='" + mId + "';");
mDBHelper.close();
}
setResult(RESULT_OK);
break;
case R.id.btncancel:
setResult(RESULT_CANCELED);
break;
}
finish();
}
}
--------------------------------------------------------------------------------------------------------
public class MyDBHelper extends SQLiteOpenHelper {  //데이터베이스 클래스

public MyDBHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub

db.execSQL("CREATE TABLE today(_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "title TEXT, " + "date TEXT , " + "time TEXT, "
+ "memo TEXT );");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXIST today;");
onCreate(db);
}

}

-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" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:gravity="center"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/edit1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="년도 입력" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="년" />

        <EditText
            android:id="@+id/edit2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="월을 입력" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="월" />

        <Button
            android:id="@+id/bt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="클릭" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

        <GridView
            android:id="@+id/grid1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:numColumns="7" >
        </GridView>
    </LinearLayout>

</LinearLayout> 
--------------------------------------------------------------------------------------------------------
<!-- extoday.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:layout_weight="0"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/texttoday"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="일정 관리" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/list1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnadd"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="일정 추가" />
    </LinearLayout>

</LinearLayout> 
-------------------------------------------------------------------------------------------------------
<!--detail.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
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="제목" />
        <EditText
            android:id="@+id/edittitle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center" />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="날짜" />
        <EditText
            android:id="@+id/editdate"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="2011/5/20" />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="시간" />
        <EditText
            android:id="@+id/edittime"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="00:00" />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="메모" />
        <EditText
            android:id="@+id/editmemo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/btnadd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/btnsave"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="저장" />
        <Button
            android:id="@+id/btndel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="삭제" />
        <Button
            android:id="@+id/btncancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="취소" />
    </LinearLayout>
</LinearLayout>

</LinearLayout> 

-실행 화면

      



- Source code Link


신고
Posted by Namseungil

티스토리 툴바