안드로이드 개발 질문/답변
(글 수 45,052)
서버에 접속후 xml을 파싱후에 listview에 출력하게끔 했습니다.
listview의 내용은 텍스트와 이미지로 되어있습니다.
그런데 문제는 listview를 스크롤 할때마다 서버에서 이미지를 다시 받아오곤 합니다.
처음에 이미지를 받아오면 스크롤을 해도 다시 안받아 오게끔 하는 방법이 있을까요..
아래는 소스입니다. 답변 부탁드립니다.
public class Test extends Activity { private String phoneNum; private ListView lv; private List<Map<String, String>> list; private MenuAdapter mAdapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.contents); lv = (ListView) findViewById(R.id.listitem); TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); // IMSI(International Mobile Subscriber Identity) - GSM/UMTS 에서 모바일 유저의 // 고유값(15자) phoneNum = telManager.getLine1Number(); if (phoneNum != null) { if (phoneNum.length() > 11) { phoneNum = phoneNum.replace("+82", "0"); } else if (phoneNum.toString().equals("") || phoneNum == null) { Toast.makeText(this, "전화번호를 확인해주세요.", Toast.LENGTH_SHORT) .show(); finish(); } // +82010 이외에 8210 82010 등 } else { Toast.makeText(this, "SIM카드 잠금해제 후에 사용해주세요.", Toast.LENGTH_SHORT) .show(); finish(); } String url = "http://localhost"; addList(url); mAdapter = new MenuAdapter(this, android.R.layout.simple_list_item_1, list); lv.setAdapter(mAdapter); } private void addList(String url) { list = new ArrayList<Map<String, String>>(); Map<String, String> map = new HashMap<String, String>(); XmlPullParserFactory parserCreator; XmlPullParser parser = null; try { URL xmlText = new URL("url); parserCreator = XmlPullParserFactory.newInstance(); parser = parserCreator.newPullParser(); parser.setInput(xmlText.openStream(), null); int eventType = parser.getEventType(); String tag = null; while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_TAG) { tag = parser.getName(); } else if (eventType == XmlPullParser.END_TAG) { eventType = parser.next(); } else if (eventType == XmlPullParser.TEXT) { String textStr = parser.getText(); if (tag.equals("co_idx")) { map = new HashMap<String, String>(); map.put("co_idx", textStr); } else if (tag.equals("subject")) { map.put("subject", textStr); } else if (tag.equals("artist")) { map.put("artist", textStr); } else if (tag.equals("duration")) { map.put("duration", textStr); } else if (tag.equals("price")) { map.put("price", textStr); } else if (tag.equals("picture")) { map.put("picture", textStr); } else if (tag.equals("video")) { map.put("video", textStr); list.add(map); } } eventType = parser.next(); } } catch (MalformedURLException e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "서비스가 원활하지 않습니다. 잠시후에 다시 시도해보세요.", Toast.LENGTH_LONG) .show(); } catch (XmlPullParserException e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "서비스가 원활하지 않습니다. 잠시후에 다시 시도해보세요.", Toast.LENGTH_LONG) .show(); } catch (SocketException e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "서비스가 원활하지 않습니다. 잠시후에 다시 시도해보세요.", Toast.LENGTH_LONG) .show(); } catch (SocketTimeoutException e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "서비스가 원활하지 않습니다. 잠시후에 다시 시도해보세요.", Toast.LENGTH_LONG) .show(); } catch (ConnectTimeoutException e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "서비스가 원활하지 않습니다. 잠시후에 다시 시도해보세요.", Toast.LENGTH_LONG) .show(); } catch (IOException e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "서비스가 원활하지 않습니다. 잠시후에 다시 시도해보세요.", Toast.LENGTH_LONG) .show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "서비스가 원활하지 않습니다. 잠시후에 다시 시도해보세요.", Toast.LENGTH_LONG) .show(); } } private class MenuAdapter extends ArrayAdapter { private List<Map<String, String>> items; public MenuAdapter(Context context, int textViewResourceId, List<Map<String, String>> items) { super(context, textViewResourceId, items); this.items = items; } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.contents_row, null); } final Map<String, String> map = items.get(position); TextView textView01 = (TextView) v.findViewById(R.id.subject); TextView textView02 = (TextView) v.findViewById(R.id.explain); TextView textView03 = (TextView) v.findViewById(R.id.time); TextView textView04 = (TextView) v.findViewById(R.id.price); ImageView imageView = (ImageView) v.findViewById(R.id.imageView); String Url = map.get("picture"); try { URL url = new URL("Url); URLConnection conn = url.openConnection(); conn.connect(); BufferedInputStream bis = new BufferedInputStream( conn.getInputStream()); Bitmap bm = BitmapFactory.decodeStream(bis); bis.close(); Bitmap resized = Bitmap.createScaledBitmap(bm, 73, 53, true); imageView.setImageBitmap(resized); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); } catch (IOException e) { } textView01.setText(map.get("subject")); textView02.setText(map.get("artist")); textView03.setText(map.get("duration")); textView04.setText(map.get("price")); return v; } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { finish(); } return super.onKeyDown(keyCode, event); } }
캐쉬만들어서 사용해보세요~
HashMap<String, Bitmap> cache = new HashMap<String, Bitmap>();
cache.get(url) 해서 url에 해당하는 bitmap이 있으면 이미지뷰에 set해주고 null이면 서버에 요청에서 받아오고 이런식으로요.
서버에서 이미지 받아온경우에는 cache.put(url, bitmap)해주시구요.
그리고 서버에서 이미지 받아오는거를 getview에서 바로하지말고 쓰레드로 돌리시는게 좋을거같네요.