안드로이드 개발 질문/답변
(글 수 45,052)
android 2.2(api 8)로 작업하고 있습니다.
인터넷에 떠도는 달력 소스와 디비 활용 소스를 하나로 합칠려고 하는데요
<자바 소스입니다.>
package com.android.dbtest1; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Date; import java.util.Vector; import android.app.Activity; import android.database.Cursor; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TableLayout; import android.widget.TableRow; import android.widget.TextView; public class DbTest1Activity extends Activity implements OnClickListener { EditText editName, editCost, editNewName, editNewCost; Button btAdd, btDelete, btUpdate; ListView listBook; String outputFilePath; BookDbAdapter bookAdapter; Cursor cursor; BookCursorAdapter cursorAdapter; Vector vec; int firstDay; int totDays; int iYear; int iMonth; public void onCreate1(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Date today = new Date(); iYear = today.getYear(); iMonth = today.getMonth(); vec = new Vector(); TableLayout table = (TableLayout) findViewById(R.id.table); for (int i = 0; i < 6; i++) { TableRow tr = new TableRow(this); for (int j = 0; j < 7; j++) { TextView tv = new TextView(this); if (j == 0) tv.setTextColor(Color.RED); else if (j == 6) tv.setTextColor(Color.BLUE); else tv.setTextColor(Color.BLACK); tv.setGravity(Gravity.CENTER_HORIZONTAL); tr.addView(tv); vec.add(tv); } table.addView(tr); } table.setStretchAllColumns(true); table = (TableLayout) findViewById(R.id.week); table.setStretchAllColumns(true); setCalendar(iYear, iMonth); Button btn = (Button) findViewById(R.id.pre); btn.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { iMonth--; setCalendar(iYear, iMonth); } }); btn = (Button) findViewById(R.id.next); btn.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { iMonth++; setCalendar(iYear, iMonth); } }); } private void setCalendar(int year, int month) { Date date = new Date(); date.setYear(year); date.setMonth(month); date.setDate(1); firstDay = date.getDay(); totDays = 31; for (int i = 29; i <= 32; i++) { date.setDate(i); if (date.getDate() == 1) { totDays = i - 1; break; } } Log.i("mylog", firstDay + " " + totDays); date = new Date(); date.setYear(year); date.setMonth(month); iYear = date.getYear(); iMonth = date.getMonth(); TextView tvToday = (TextView) findViewById(R.id.today); tvToday.setText((iYear + 1900) + "년" + (iMonth + 1) + "월"); for (int i = 0; i < vec.size(); i++) { ((TextView) vec.get(i)).setText(""); } int iDate = 1; for (int i = firstDay; i < firstDay + totDays; i++) { ((TextView) vec.get(i)).setText(String.valueOf(iDate++)); } } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } public void init() { editName = (EditText) findViewById(R.id.editText1); editCost = (EditText) findViewById(R.id.editText2); editNewName = (EditText) findViewById(R.id.editText3); editNewCost = (EditText) findViewById(R.id.editText4); listBook = (ListView) findViewById(R.id.listView1); btAdd = (Button) findViewById(R.id.button1); btDelete = (Button) findViewById(R.id.button2); btUpdate = (Button) findViewById(R.id.button3); if (btAdd != null) btAdd.setOnClickListener(this); if (btDelete != null) btDelete.setOnClickListener(this); if (btUpdate != null) btUpdate.setOnClickListener(this); bookAdapter = new BookDbAdapter(this); copyDbToPkg(); bookAdapter.open(outputFilePath); setListFromDb(); } public void onClick(View v) { // TODO Auto-generated method stub if (v == btAdd) {// Add book into db. try { String strName = editName.getText().toString(); String strCost = editCost.getText().toString(); double digitCost = 0; Log.i("BTN CLICKED", strName + ":" + strCost); if (strName != null && strCost != null) { digitCost = Integer.parseInt(strCost); bookAdapter.insertBook(strName, digitCost); } } catch (Exception e) { e.printStackTrace(); } } else if (v == btDelete) { String strId = editNewName.getText().toString(); int deleteId = -1; deleteId = Integer.parseInt(strId); bookAdapter.deleteBook(deleteId); } else if (v == btUpdate) { String strId = editName.getText().toString(); int deleteId = -1; deleteId = Integer.parseInt(strId); String strName = editNewName.getText().toString(); String strCost = editNewCost.getText().toString(); double newCost = Double.parseDouble(strCost); bookAdapter.updateBook(deleteId, strName, newCost); } notifyListDataChanged(); } public void notifyListDataChanged() { cursorAdapter.getCursor().requery(); cursorAdapter.notifyDataSetChanged(); } public void setListFromDb() { cursor = bookAdapter.fetchAllBooks(); Log.i("BTN CLICKED", "cursor:" + cursor.getCount()); cursorAdapter = new BookCursorAdapter(this, cursor); listBook.setAdapter(cursorAdapter); } public void copyDbToPkg() { // DB Copy try { // ** 이부분을 넣지않으면 몇몇 기기에서 데이터베이스가 정상적으로 복사되지 않습니다 **// // bookAdapter.mDb= bookAdapter.getReadableDatabase(); // bookAdapter.mDb.close(); outputFilePath = this.getFilesDir() + "/book.db"; if (!isFileExists("book", this.getFilesDir().toString())) { // InputStream is = // this.getResources().openRawResource(R.raw.book); InputStream is = this.getAssets().open("book.db"); // **zip파일일 경우 // ZipInputStream zis = new ZipInputStream(is); // zis.getNextEntry(); FileOutputStream fos = new FileOutputStream(outputFilePath); byte[] buffer = new byte[4096]; while (true) { int readSize = is.read(buffer, 0, buffer.length); if (readSize == -1) break; fos.write(buffer, 0, readSize); } fos.close(); // zis.close(); is.close(); } else Log.i("TEST_KSI", "db is already copied"); } catch (IOException e) { e.printStackTrace(); } } public boolean isFileExists(String fileName, String filePath) { boolean result = false; File file = new File(filePath, fileName + ".db"); if (file.exists()) result = true; return result; } }
<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:layout_gravity="center_horizontal" android:orientation="vertical" > <LinearLayout android:id="@+id/menu" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="10px" android:layout_marginRight="10px" android:layout_marginTop="10px" android:orientation="horizontal" > <Button android:id="@+id/pre" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:text="이전" /> <TextView android:id="@+id/today" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="3" android:gravity="center_horizontal|center_vertical" android:text="today" android:textColor="#FFFFFFFF" /> <Button android:id="@+id/next" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:text="다음" /> </LinearLayout> <TableLayout android:id="@+id/week" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/menu" android:layout_marginLeft="10px" android:layout_marginRight="10px" android:background="#FF8DC701" android:shrinkColumns="true" android:stretchColumns="true" > <TableRow > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="일" android:textColor="#FFFF0000" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="월" android:textColor="#FF000000" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="화" android:textColor="#FF000000" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="수" android:textColor="#FF000000" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="목" android:textColor="#FF000000" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="금" android:textColor="#FF000000" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="토" android:textColor="#FF0000FF" /> </TableRow> </TableLayout> <TableLayout android:id="@+id/table" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/week" android:layout_marginLeft="10px" android:layout_marginRight="10px" android:background="#FFF6F3A4" /> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/table" > <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.26" > <requestFocus > </requestFocus> </EditText> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.26" > </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.26" android:text="INSERT" > </Button> </LinearLayout> <LinearLayout android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/linearLayout1" > <EditText android:id="@+id/editText3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.26" > </EditText> <EditText android:id="@+id/editText4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.26" > </EditText> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="DELETE" > </Button> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="UPDATE" > </Button> </LinearLayout> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_below="@+id/linearLayout2" > </ListView> </LinearLayout>
실행을 해보면 달력의 월화수목금토일까지는 나오는데
그 아래에 날짜가 출력이 되지 않습니다.
처음 안드로이드 개발을 해보는 거라 모르는게 많은데요
시간 내 주셔서 해결 방법 알려주시길 부탁드립니다.
질문 전에 공지 읽었습니다.
처음 안드로이드 개발 해보는 학생입니다.
코드가 길더라도 가르침 부탁드립니다.