728x90

Activity 화면 갱신처리를 하는 방법때문에 다양한 걸 검색해서 찾았다.


public void onClick (View v){
    Intent intent = getIntent();
    finish();
    startActivity(intent);
}


이 코드는 현재 화면을 다시 Refresh 하는 코드다.

따라서 검색어를 입력하고 검색결과를 찾는 경우에는 부적합하다.

데이터를 삭제하고 화면을 갱신해야 하는 경우에는 유용하다.


The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread.

서버에서 데이터 및 사진이미지를 가져와서 ListView 에 갱신처리하는 걸 구현하는데 이런 메시지가 나오면서 간혹 앱이 강제종료된다.

무엇이 문제일까?

 AsyncTask.execute(new Runnable() 에서 처리한 결과가 listViewAdapter.notifyDataSetChanged(); 보다 늦게 나오면서 생기는 문제였다.

 결국 이런 로직을 사용하면 안된다는 것이다.

 처리순서가 순차적으로 되지 않고 아래 코드가 먼저 실행되면서 발생하는 문제였다.

     protected void showList() {
        try {
            JSONObject jsonObj = new JSONObject(searchJSON);
            peoples = jsonObj.getJSONArray(TAG_RESULTS);

            personDataItem.clear(); // 서버에서 가져온 데이터 초기화
            for(int i=0;i<peoples.length();i++){
                JSONObject c = peoples.getJSONObject(i);
                final String uid = c.getString(TAG_UID);
                final String name = c.getString(TAG_NAME);
                final String mobileNO = c.getString(TAG_MobileNO);
                final String officeNO = c.getString(TAG_OfficeNO);
                // 이미지 사이즈가 커서 생기는 문제??
                final Bitmap myIcon = PHPComm.autoresize_decodeResource(getResources(), R.mipmap.photo_base, 160);

                final String photoURL = Value.IPADDRESS + "/photos/" + uid + ".jpg";
                System.out.println("Server Photo URL ===" + photoURL);
                AsyncTask.execute(new Runnable() {
                    @Override
                    public void run() {
                        if(isExists(photoURL) == true){
                            Bitmap rBmp = loadWebImage(photoURL);
                            System.out.println("Server Photo Image ==="+ uid +" | "+ rBmp);
                            System.out.println("Photo Image height ==="+ rBmp.getHeight());
                            int width = (int)(rBmp.getWidth() * 160.0 / rBmp.getHeight());
                            rBmp = Bitmap.createScaledBitmap(rBmp, width, 160,true);

                            // 서버에서 가져온 데이터 저장
                            listViewAdapter.addItem(rBmp,uid,name,mobileNO,officeNO);
                        } else {
                            // 서버에 사진 이미지가 없으면 기본 이미지를 포함한 정보 저장
                            listViewAdapter.addItem(myIcon,uid,name,mobileNO,officeNO);
                        }
                    }
                });

            }

            runOnUiThread(new Runnable() { // 화면에 반영하기 위하여 runOnUiThread()를 호출하여 실시간 갱신한다.
                @Override
                public void run() {
                    // 갱신된 데이터 내역을 어댑터에 알려줌
                    listViewAdapter.notifyDataSetChanged();
                }
            });

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }


도움이 되는 내용이 있어서 적어둔다.

http://www.vogella.com/tutorials/AndroidListView/article.html




블로그 이미지

Link2Me

,