주소관리 앱개발 ver.1 (7일차)
- /layout/frag_detail.xml 생성
1234567891011<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/txtName"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="TextView" /></LinearLayout> - FragDetail 클래스 생성
12345678910public class FragDetail extends android.support.v4.app.Fragment {@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.frag_detail, container, false);return view;}} - FragList 클래스 수정
12345678910111213141516171819202122232425262728293031323334public class FragList extends android.support.v4.app.Fragment{ArrayList<AddrDTO> arList;@Nullable@Overridepublic View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.frag_list, container, false);AddrDTO dto = new AddrDTO();AddrDAO dao = new AddrDAO(getContext());arList = dao.getArrayList("");ListView listView = view.findViewById(R.id.lstView);AddrAdapter adapter = new AddrAdapter(this.getContext(), arList);listView.setAdapter(adapter);listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {//Toast.makeText(getContext(), ""+arList.get(position).name, Toast.LENGTH_SHORT).show();Fragment fragDetail = new FragDetail();Bundle bundle = new Bundle();bundle.putInt("id", arList.get(position)._id);fragDetail.setArguments(bundle);FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();ft.replace(R.id.container, fragDetail);ft.commit();}});return view;}}- ListView의 item을 클릭했을때의 리스너 연결
- listView.setOnItemClickListerner(new 까지 쓰고 Ctrl+Space를 누르고 마우스로 선택
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) 의 매개변수가 다르면 parent, position, id로 수정
- FragDetail 클래스로 id를 전송하는 부분
123Bundle bundle = new Bundle();bundle.putInt("id", arList.get(position)._id);fragDetail.setArguments(bundle);
- AddrDAO 클래스에 다음 메서드 추가
123456789101112131415161718192021222324// 테이블 전체 내용을 ArrayList로public AddrDTO getRecord(int id) {AddrDTO dto = new AddrDTO();SQLiteDatabase db = getConn();String sql;Cursor cursor;sql = "SELECT * FROM `" + tableName + "` WHERE `_id` = " + id + ";";cursor = db.rawQuery(sql, null);while(cursor.moveToNext()){dto._id = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));dto.name = cursor.getString(cursor.getColumnIndexOrThrow("name"));dto.tel = cursor.getString(cursor.getColumnIndexOrThrow("tel"));dto.hp = cursor.getString(cursor.getColumnIndexOrThrow("hp"));break;}cursor.close();// 목록 작성 끝return dto;}// getRecordDTO() - FragDetail 클래스 수정
1234567891011121314151617public class FragDetail extends android.support.v4.app.Fragment {@Nullable@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.frag_detail, container, false);Bundle bundle = getArguments();int id = bundle.getInt("id");AddrDAO dao = new AddrDAO(getContext());AddrDTO dto = dao.getRecord(id);((TextView)view.findViewById(R.id.txtName)).setText(dto.name);return view;}}- 전송된 id를 받는 부분
12Bundle bundle = getArguments();int id = bundle.getInt("id");
- 전송된 id를 받는 부분