[
과제] p222 (WebViewClient 사용)

1. 레이아웃 : my_html_demo.xml (p222)

2. 액티비티

1) 액티비티1: MyHtmlDemo1.java (p222)

2) 액티비티2: MyHtmlDemo2.java (p227)

 

[과제]상품에 날짜 표시 

1. MyProdDBCons 버전을 이전보다 높은 숫자로

    public static final int DB_VERSION = 4;  

2.MyProdDBCons 컬럼명 추가

    public static final String PROD_TIME ="prod_time";

3.MyProdDBHelper onCreate 변경

    + MyProdDBCons.PROD_NAME+ " text not null, " 

    + MyProdDBCons.PROD_TIME + " CURRENT_TIMESTAMP);");

4. my_prod_db_col.xml <ImageView>위에 TextView 추가

    <TextView android:id="@+id/my_prod_time" android:textSize="15sp"

    android:layout_height="wrap_content" android:layout_width="80sp" />

5. MyProdDBList 코드 변경

   String columns[] = { MyProdDBCons._ID, MyProdDBCons.PROD_CODE,

   MyProdDBCons.PROD_NAME, MyProdDBCons.PROD_TIME };

   String[] FROM = new String[] { MyProdDBCons.PROD_CODE,

   MyProdDBCons.PROD_NAME, MyProdDBCons.PROD_ID,

   MyProdDBCons.PROD_TIME };

   int[] TO = new int[] { R.id.my_prod_code, R.id.my_prod_name,

   R.id.my_prod_id, R.id.my_prod_time};

6. MyProdDBList MyAdapter getView 코드 추가

TextView tv4 = (TextView) row.findViewById(R.id.my_prod_time);

tv4.setText(cursor.getString(3).substring(0,10));

7. MyProdDBManager initMyProdDB 변경

SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

values.put(MyProdDBCons.PROD_TIME, sf.format(new Date()));

8.my_prod_db_col.xml <ImageView> 크기 속성 변경 

             <ImageView android:id="@+id/prod_img" android:src="@drawable/icon"

                           android:layout_width="80sp" android:layout_height="80sp" />

 

 

[과제 2] 상품 상세 보기

1. 상세보기 레이아웃 : my_prod_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">

             <ImageView android:id="@+id/prod_img_detail" android:src="@drawable/icon"

                           android:layout_weight="1" android:layout_width="fill_parent"

                           android:layout_height="wrap_content" />

</LinearLayout>

 

2. MyProdDBList MyAdapter getView cursor 코드 변경

                                        final Cursor cursor = getCursor();

                                        cursor.moveToPosition(position);

                                        final String prod_id = cursor.getString(0);

                                        final String prod_code = cursor.getString(1);

                                        final String prod_name = cursor.getString(2);

                                        tv1.setText(prod_id);

                                        tv2.setText(prod_code);

                                        tv3.setText(prod_name);

 

3. MyProdDBList MyAdapter getView 코드 변경

 

                                        img.setImageResource(getResources()

                                                                  .getIdentifier(

                                                                                             "kosta.android:drawable/" + prod_code,

                                                                                             null, null));

 

4. MyProdDBList MyAdapter getView

 img 터치리스너 추가 (setOnTouchListener)

 

img.setOnTouchListener(new OnTouchListener() {

             public boolean onTouch(View v, MotionEvent event) {

                            LayoutInflater factory = getLayoutInflater();

                            final View view = factory.inflate(R.layout.my_prod_detail, null);

                            ImageView img = (ImageView) view.findViewById(R.id.prod_img_detail);

                            img.setImageResource(getResources()

                                                                  .getIdentifier(

                                                                                             "kosta.android:drawable/" + prod_code,

                                                                                             null, null));

                                        new AlertDialog.Builder(MyProdDBList.this)

                                                     .setTitle(prod_name)

                                                     .setView(view)

                                                     .setNeutralButton("확인", null)

                                                     .show();                                        

                           return false;

             }

});

 

[과제3] 입력/수정시 날짜가 같이 업데이트 되도록

*  MyProdDBList MyAdapter getView tv4.setText(cursor.getString(3).substring(0,10)); 다음과 같이 수정후

tv4.setText(cursor.getString(3));

 

고칠곳 : MyProdDBManger.java, MyProdContentProvider.java

 

[HTTP처리 과제 1]

0. 웹주소 확인

http://okgosu.net/book/get_xml_data.php?kind=green

http://okgosu.net/book/get_xml_data.php?kind=yellow

 

1. 액티비티 작성 MyHttpDemo - ListActivity

2. import 추가: import org.w3c.dom.Element;

3. onCreate 오버라이딩

ArrayList<String> items = new ArrayList<String>();

HttpClient hc = new DefaultHttpClient();

HttpGet req = new HttpGet("http://okgosu.net/book/get_xml_data.php?kind=green");

try {

   ResponseHandler<String> res = new BasicResponseHandler();

   String resStr = hc.execute(req, res);

   DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

   Document doc = builder.parse(new InputSource( new StringReader(resStr)));

   NodeList nl = doc.getElementsByTagName("item");

   for(int i=0; i<nl.getLength(); i++){

       Element el = (Element)nl.item(i);

       String data = (String)el.getFirstChild().getNodeValue();

       items.add(data);

    }

    setListAdapter(new ArrayAdapter(this,  android.R.layout.simple_list_item_1, items));

} catch (Exception e) {

    e.printStackTrace();

}

[과제2]

1. 레이아웃 생성 - my_http_demo.xml

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

             <LinearLayout android:orientation="horizontal"

                           android:layout_width="fill_parent" android:layout_height="wrap_content">

              <Spinner android:id="@+id/sp" android:layout_height="wrap_content"

                           android:layout_width="wrap_content" 

                           android:layout_weight="1"/>

             <Button android:id="@+id/go_button" android:layout_height="wrap_content"

                           android:layout_width="wrap_content" android:text="검색" />

             </LinearLayout>

             <ListView android:id="@android:id/list"

                           android:drawSelectorOnTop="false" android:layout_width="fill_parent"

                           android:layout_height="fill_parent" />                   

</LinearLayout>

 

2. MyHttpDemo my_http_demo.xml 연동

setContentView(R.layout.my_http_demo);

 

3. Spinner 다음의 데이터가 보이도록 어댑터 설정: green, red, blue, yellow

final Spinner sp = (Spinner)findViewById(R.id.sp);

String[] arr = {"red", "green", "yellow", "blue"};

ArrayAdapter<String> adapter = 

new ArrayAdapter<String>(MyHttpDemo.this, android.R.layout.simple_spinner_item, arr);

sp.setAdapter(adapter);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

 

4. 버튼을 클릭하면 스피너에서 선택한 아이템의 데이터를 뽑아오도록 리스너 설정

1) HttpGet url 스피너의 선택한 아이템 주소로 세팅

2) HttpClient 부터 try~catch 다시 로직 수행

Button go_button = (Button)findViewById(R.id.go_button);

go_button.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

        ArrayList<String> items = new ArrayList<String>();

        HttpClient hc = new DefaultHttpClient();

        HttpGet req = new HttpGet("http://okgosu.net/book/get_xml_data.php?kind=" + 

                sp.getSelectedItem().toString());

        try {

            ................................상동.............................

        }               

    }

});

 

[네이버 오픈API 처리]

0. 네이버 오픈API 주소

http://openapi.naver.com/search?target=book&query=okgosu&key=0dace7eac2f00aa313ddd5fafe7426b2

 

1. MyOpenAPI (ListActivity) 생성

2. my_http_demo.xml 레이아웃 연결 

3. Spinner 아이템은 : book, movieman, shop

4. 버튼 클릭시 HttpGet객체를 생성하여 오픈API 데이터 페치

 StringBuilder url = new StringBuilder("http://openapi.naver.com/search?");

 url.append("target=" + sp.getSelectedItem().toString());

 url.append("&query=flex");

 url.append("&key=0dace7eac2f00aa313ddd5fafe7426b2");

  HttpGet req = new HttpGet(url.toString());

 

5. DOM파싱의 for 문의 data설정을

1) 고치기 내용

String data = (String)el.getFirstChild().getNodeValue();

2) 고친 내용

String data = el.getElementsByTagName("title").item(0)

                   .getFirstChild().getNodeValue();

 

 

 

profile