2

Android プロジェクトでFastAdapterを使用しています。

これは私がそれを使用している方法です:

    public class HRequest extends AbstractItem<HRequest, HRequest.ViewHolder> {

        public String imageURL;

        public HRequest() {

        }

        public HRequest(String imageURL) {
            this.imageURL = imageURL;
        }

        // Fast Adapter methods
        @Override
        public int getType() {
            return R.id.recycler_view;
        }
        @Override
        public int getLayoutRes() {
            return R.layout.h_request_list_row;
        }
        @Override
        public void bindView(ViewHolder holder) {
            super.bindView(holder);

            holder.imageURL.setText(imageURL);

        }
        // Manually create the ViewHolder class
        protected static class ViewHolder extends RecyclerView.ViewHolder {

            TextView imageURL;

            public ViewHolder(View itemView) {
                super(itemView);
                imageURL = (TextView)itemView.findViewById(R.id.imageURL);

if (!imageURL.getText().toString().isEmpty()) {

                Toast.makeText(itemView.getContext(), imageUID.getText().toString(), Toast.LENGTH_SHORT).show();

if (imageURL.getText().toString().startsWith("https://firebasestorage.googleapis.com/") || imageURL.getText().toString().startsWith("content://")) {
                    Picasso.with(itemView.getContext())
                            .load(imageURL.getText().toString())
                            .into(homelessImage);
                } else {
                    Toast.makeText(itemView.getContext(), "some problem", Toast.LENGTH_SHORT).show();
                }

            } else {
                Toast.makeText(itemView.getContext(), "no imageUID found", Toast.LENGTH_SHORT).show();
            }

            }
        }

    }

ここにあるR.layout.h_request_list_row

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              xmlns:ads="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              xmlns:tools="http://schemas.android.com/tools">

            <TextView
                android:id="@+id/imageURL"
                android:text="imageURL"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

</LinearLayout>

ここでの問題は、データベースから取得されたものとしてでimageURLはなく、レイアウト ファイルで定義されたものとして設定されているため、画像がダウンロードされないことです。私はこの要点に従いました: https://gist.github.com/puf/f49a1b07e92952b44f2dc36d9af04e3cimageURLimageURL

私はここで確信していString imageURLます:

public HRequest(String imageURL) {
            this.imageURL = imageURL;
        }

Firebase データベースから取得した URL を正常に取得しました。

ここで何が問題なのか教えてください。

質問の形式が悪くてすみません。私はここではまだ初心者です。

4

1 に答える 1

1

問題は、 に画像を読み込もうとすることですViewHolder

が作成された時点では、メソッドを介しViewHolderたセットはまだありませんでした。これは、が作成された後に呼び出されるためです。ImageUrlbindViewViewHolder

そのため、画像読み込みロジックをbindViewメソッドに移動する必要があります。これViewHolderRecyclerView、ビューを効率的にキャッシュし、リスナーを定義し、RecyclerView で使用されるすべてのビューを「固定」しておくためのものです。

コードを機能させるには、次のように変更する必要があります。

public class HRequest extends AbstractItem < HRequest, HRequest.ViewHolder > {
    public String imageURL;
    public HRequest() {}
    public HRequest(String imageURL) {
        this.imageURL = imageURL;
    }
    // Fast Adapter methods
    @Override
    public int getType() {
        return R.id.recycler_view;
    }
    @Override
    public int getLayoutRes() {
        return R.layout.h_request_list_row;
    }
    @Override
    public void bindView(ViewHolder holder) {
        super.bindView(holder);
        holder.imageURL.setText(imageURL);

        if (!imageURL.isEmpty()) {
            Toast.makeText(holder.itemView.getContext(), imageURL, Toast.LENGTH_SHORT).show();
            if (imageURL.startsWith("https://firebasestorage.googleapis.com/") || imageURL.startsWith("content://")) {
                Picasso.with(holder.itemView.getContext()).cancelRequest(holder.myImageView);
                Picasso.with(holder.itemView.getContext()).load(imageURL).into(holder.myImageView);
            } else {
                Toast.makeText(holder.itemView.getContext(), "some problem", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(holder.itemView.getContext(), "no imageUID found", Toast.LENGTH_SHORT).show();
        }
    }
    // Manually create the ViewHolder class
    protected static class ViewHolder extends RecyclerView.ViewHolder {
        TextView imageURL;
        ImageView myImageView;
        public ViewHolder(View itemView) {
            super(itemView);
            imageURL = (TextView) itemView.findViewById(R.id.imageURL);
            myImageView = (ImageView) itemView.findViewById(R.id.myImageView);
        }
    }
}

したがって、基本的には、アイテムがリストに表示されるたびRecyclerViewに呼び出され、データを適用する必要があります。bindViewまた、新しい画像が適用される前に、最初に with picassoのリクエストをキャンセルする必要がありますImageView(このコード スニペットも追加しました)。ViewHolderImageView

于 2016-06-22T09:14:29.057 に答える