[안드로이드] 도서관리 #2 Book 목록
- 참고 자료
- 기타 필요한 것은 1차로 이 홈페이지에서 검색
2차로 google에서 검색
- 기타 필요한 것은 1차로 이 홈페이지에서 검색
- 기본 정보
- 제출일시 : 07월 8일 24시
- 제출방법 : http://smart9.iwinv.net/final/ 에 글쓰기로 저장
계정은 카톡으로
(현재 보는 페이지와 유사하게 글쓰기) - 오류가 생겨 해결이 안될 때
- naver, google에서 검색해서 본인 어떻게든 해결
- Run – Debug app 메뉴를 통해 1단계씩 실행시키면서 오류 찾기
- Run-Debug 기능이 느리지만 가장 효과적으로 실력이 향상됨
- 그래도 안되면 같은 조원들에게 묻기
- 그래도 안되면 언제든지 단톡에 묻기
- 답을 해줄 수 있는 학생이 답해주고,
- 그래도 안되면 내가 답해주겠음
(단, 컴파일 오류는 알려주지 않음, 실행오류만 묻길 바람)
- naver, google에서 검색해서 본인 어떻게든 해결
- mybook.db 생성 : Sqlite 사용
- tbook 테이블 생성 -> MainDBHelper.java에서 프로그래밍으로 생성
- 테이블의 필드와 DTO의 멤버필드 관계를 엑셀로 정리 : 다운로드
- 1차적으로 아래 소스 코드를 실행시켜보고 완성이 되면
우리학교 도서관 홈페이지를 참조하여 필드 추가 및 UI 추가 구성
- tbook 테이블 생성 -> MainDBHelper.java에서 프로그래밍으로 생성
- MainDBHelper.java 소스 : 변화 없음
- BookDTO.java 소스 : 변화 없음
- BookDAO.java 소스 : getArrayList() 메소드 추가
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162package kr.co.moak.summer2019;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteException;import android.util.Log;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.Vector;public class BookDAO {private String tableName = "tbook";private Context m_context;public BookDAO(Context context){this.m_context = context;}// DB연결 메소드public SQLiteDatabase getConn() {MainDBHelper dbHelper = new MainDBHelper(m_context);SQLiteDatabase db = dbHelper.getWritableDatabase();return db;}/** 도서목록 출력 */public ArrayList<BookDTO> getArrayList() {ArrayList<BookDTO> arList = new ArrayList<BookDTO>();SQLiteDatabase db = getConn();String sql;Cursor cursor;// 목록 작성 시작sql = "SELECT * FROM `" + tableName + "` ORDER BY `_id` DESC ;";Log.v("MYTAG", sql);cursor = db.rawQuery(sql, null);while(cursor.moveToNext()){BookDTO dto = new BookDTO();dto.set_id(cursor.getInt(cursor.getColumnIndexOrThrow("_id")));dto.setTitle(cursor.getString(cursor.getColumnIndexOrThrow("title")));dto.setPric(cursor.getInt(cursor.getColumnIndexOrThrow("pric")));dto.setPubl(cursor.getString(cursor.getColumnIndexOrThrow("publ")));Log.v("MYTAG", dto.toString());arList.add(dto);}cursor.close();// 목록 작성 끝return arList;}// getArrayList()} - activity_list_listitem.xml 소스
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:padding="2dp"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="제목"/><TextViewandroid:id="@+id/txtTitle"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="3"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="가격"/><TextViewandroid:id="@+id/txtPric"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="3"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="출판사"/><TextViewandroid:id="@+id/txtPubl"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="3"/></LinearLayout></LinearLayout> - activity_list.xml 소스
123456789101112131415161718192021222324<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><Buttonandroid:id="@+id/btnHome"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:text="홈으로" /><ListViewandroid:id="@+id/listView"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_above="@id/btnHome"/></RelativeLayout> - BookAdapter.java 소스
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081package kr.co.moak.summer2019;import android.app.Activity;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.RadioButton;import android.widget.TextView;import java.util.ArrayList;public class BookAdapter extends BaseAdapter {Activity context;ArrayList<BookDTO> arList;LayoutInflater mInflater;BookAdapter(Activity context, ArrayList<BookDTO> arList) {this.context = context;this.arList = arList;mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);}@Overridepublic View getView(final int position, View convertView, ViewGroup parent) {View row = mInflater.inflate(R.layout.activity_list_listitem, parent, false);String title = arList.get(position).getTitle();int pric = arList.get(position).getPric();String publ = arList.get(position).getPubl();TextView txtTitle = ((TextView) row.findViewById(R.id.txtTitle));TextView txtPric = ((TextView) row.findViewById(R.id.txtPric));TextView txtPubl = ((TextView) row.findViewById(R.id.txtPubl));txtTitle.setText(title);txtPric.setText(pric + "");txtPubl.setText(publ);return row;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn arList.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn arList.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic boolean isEnabled(int position) {// TODO Auto-generated method stubreturn true;}@Overridepublic int getItemViewType(int position) {// TODO Auto-generated method stubreturn super.getItemViewType(position);}@Overridepublic int getViewTypeCount() {// TODO Auto-generated method stubreturn super.getViewTypeCount();}} - ListActivity.java 소스
123456789101112131415161718192021222324252627282930313233343536package kr.co.moak.summer2019;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.ListView;import java.util.ArrayList;public class ListActivity extends AppCompatActivity implements View.OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_list);Button btnHome = (Button)this.findViewById(R.id.btnHome);btnHome.setOnClickListener(this);ListView listView = (ListView)this.findViewById(R.id.listView);BookDAO dao = new BookDAO(this);ArrayList<BookDTO> arList = dao.getArrayList();BookAdapter adapter = new BookAdapter(this, arList);listView.setAdapter(adapter);}@Overridepublic void onClick(View view) {switch (view.getId()){case R.id.btnHome:finish();break;}}} - activity_main.xml 소스 : 변화 없음
- MainActivity.java 소스 : case R.id.btnList : 아래의 주석 제거
1234567891011121314151617181920212223242526272829303132333435363738package kr.co.moak.summer2019;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btnInsert = (Button)this.findViewById(R.id.btnInsert);btnInsert.setOnClickListener(this);Button btnList = (Button)this.findViewById(R.id.btnList);btnList.setOnClickListener(this);}@Overridepublic void onClick(View view) {Intent intent = null;switch (view.getId()){case R.id.btnInsert:intent = new Intent(this, InsertActivity.class);startActivity(intent);break;case R.id.btnList:intent = new Intent(this, ListActivity.class);startActivity(intent);break;}}} - 교수참조 : 구글 드라이브의 내가만든 소스코드에 안드로이드_Summer2019_20190627.zip 파일 올려놨음