2012-12-28 8 views
0

가능한 복제를 작동하지 않습니다
How do I do a lazy load of images in ListView비트 맵

내가 비트 맵 및 텍스트와 목록보기가 있습니다. 내가 그림을 다운로드하고 그것을보고 싶을 때, 내 애플 리케이션에 나타나지 않습니다.

 List<HashMap<String, Object> > aList = new ArrayList<HashMap<String, Object> >(); 


    for(int i=0;i<ilosctalentow.size();i++){ 
     if (ilosctalentow.get(i).indexOf("0/")==-1) 
     { 
     HashMap<String, Object> hm = new HashMap<String,Object>(); 
     hm.put("txt", "xxx"); 
     hm.put("cur","Currency : " + ilosctalentow.get(i)); 
     Bitmap bmp = DownloadImage("http://www.xxx.pl/xxx/xxx/xhxuxj.png"); 
     hm.put("flag",bmp); 
     aList.add(hm); 

     Log.i(TAG,Integer.toString(R.drawable.afghanistan)); 
     } 
    } 

    // Keys used in Hashmap 
    String[] from = { "flag","txt","cur" }; 

    // Ids of views in listview_layout 
    int[] to = { R.id.flag,R.id.txt,R.id.cur}; 

    // Instantiating an adapter to store each items 
    // R.layout.listview_layout defines the layout of each item 
    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to); 

    // Getting a reference to listview of main.xml layout file 
    ListView listView = (ListView) findViewById(R.id.listview); 

    // Setting the adapter to the listView Zaraz mnie huj strzeli 
    listView.setAdapter(adapter); 

도와주세요 : 내가 R.drawable.imagename에서 이미지를 사용하는 경우 그것은 ... 내 코드를 작동합니다!

+0

UI 스레드에서 이미지를 다운로드 할 수 있는지 의심 스럽습니다. 백그라운드 스레드에서 이미지를 다운로드하고 다운로드가 끝나면 어댑터를 업데이트해야합니다. 또한 SimpleAdapter 클래스 대신 자신의 어댑터를 만들어야한다고 생각합니다. – vorrtex

+0

녹색 droid로 만들 수 있습니까? – user1906882

+0

모든 Android 버전에서 가능합니다. 다음은 유명한 질문입니다. 코드 샘플을 찾을 수 있습니다. http://stackoverflow.com/questions/541966/how-do-i-do-a-lazy-load-of-images-in-listview – vorrtex

답변

0

다른 질문에 대한 링크를 답변으로 게시하고 현재 솔루션에 적용하는 방법을 설명 할 수 있다고 생각합니다. 응용 프로그램이 시작

Lazy load of images in ListView

는 이미지없이 간단한 목록을 표시합니다. 그런 다음 비트 맵을 다운로드하고 생성하는 백그라운드 스레드를 시작하십시오. 스레드가 작업을 완료하면 목록을 업데이트하여로드 된 이미지를 표시합니다.

이 작업을하려면 사용자 지정 어댑터를 만들어야합니다. (허용 대답에서 클래스가 몇 가지 버그를 포함하고 있기 때문에) this answer에서 DrawableManager 클래스를 가지고 같은 어댑터를 쓰기 :

이 어댑터에 대한보기 가방에 요소 또는 다른 클래스를 사용하여 발견 저장함으로써 개선 될 수
// You should create your own class instead of TestItem 
public class TestAdapter extends ArrayAdapter<TestItem> { 

    private final LayoutInflater mInflater; 
    private final DrawableBackgroundDownloader mDrawableBackgroundDownloader = new DrawableBackgroundDownloader(); 

    public TestAdapter(Context context, List<TestItem> objects) { 
     super(context, -1, objects); 
     mInflater = LayoutInflater.from(context); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     final TestItem item = this.getItem(position); 

     View view = convertView == null 
       ? mInflater.inflate(R.layout.listview_layout, null) 
       : convertView; 

     TextView txtView = (TextView) view.findViewById(R.id.txt); 
     TextView curView = (TextView) view.findViewById(R.id.cur); 
     ImageView flagView = (ImageView) view.findViewById(R.id.flag); 

     txtView.setText(item.getText()); 
     curView.setText(item.getCurrency()); 
     mDrawableBackgroundDownloader.loadDrawable(item.getFlagUrl(), flagView, null); 

     return view; 
    } 
} 

이미지 다운로드 및 캐싱. 그러나 단순화를 위해 그대로 두겠습니다.

관련 문제