0

重複の可能性:
ListView で画像の遅延読み込みを行うにはどうすればよいですか

ビットマップとテキストを含むリストビューがあります。写真をダウンロードして表示したいのですが、アプリに表示されません。R.drawable.imagename の画像を使用すると動作します...私のコード:

        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);

助けてください!

4

1 に答える 1

0

別の質問へのリンクを回答として投稿し、それを現在のソリューションに適用する方法を説明できると思います。

ListViewでの画像の遅延読み込み

アプリケーションが起動したら、画像なしの簡単なリストを表示します。次に、ビットマップをダウンロードして作成するバックグラウンドスレッドを開始します。スレッドがジョブを完了したら、ロードされた画像が表示されるようにリストを更新します。

これを機能させるには、カスタムアダプタを作成する必要があります。この回答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;
    }
}

このアダプタは、見つかった要素をビューバッグに保存するか、画像のダウンロードとキャッシュに別のクラスを使用することで改善できます。ただし、わかりやすくするためにそのままにしておきます。

于 2012-12-28T23:27:07.553 に答える