2017-04-06 6 views
0

私はフラグメントを持っていますが、アダプターを設定し、firebaseデータベースから文字列url値を取得しますが、イメージはロードされません。Firebaseを使用してリストビューのイメージをロードする

私の断片:

public class PromoFragment extends Fragment { 


private DatabaseReference mDatabase; 

List<Promo> promoList = new ArrayList<>(); 
ListView listViewPromo; 

public PromoFragment() { 
    // Required empty public constructor 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    mDatabase = FirebaseDatabase.getInstance().getReference(); 

} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 

    View view = inflater.inflate(R.layout.fragment_promo, container, false); 
    listViewPromo = (ListView) view.findViewById(R.id.listViewPromo); 
    listenPromo(); //here i load my data, i currently have 3 childs, each child has imageUrl 
    PromoAdapter promoAdapter = new PromoAdapter(getActivity().getApplicationContext(), R.layout.list_promo, promoList); 
    listViewPromo.setAdapter(promoAdapter); 
    return view; 
} 


private void listenPromo() { 

    mDatabase.child("Promo").addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      // Get Post object and use the values to update the UI 
      // MainActivity.currUser = dataSnapshot.getValue(User.class); 

      for (DataSnapshot dsp : dataSnapshot.getChildren()) { 
       promoList.add(dsp.getValue(Promo.class)); //add result into array list 

      } 

      // ... 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 
      // Getting User Class data failed... 
      Log.w("Canceled", "loadUserData:onCancelled", databaseError.toException()); 
      // ... 

     } 


    }); 
} 

}

私fragment_promo.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".PromoFragment"> 

    <ListView 
     android:id="@+id/listViewPromo" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent"> 
    </ListView> 

</RelativeLayout> 

list_promo.xml

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


    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageViewPromo" /> 

</LinearLayout> 

promo.javaクラス

私はオブジェクトごとに3つの変数がありますが、この場合はdurl(URLダウンロード)のみを使用します。

public class Promo { 
private String durl; 
private Boolean active; 
private String timestamp; 


public Promo() { //default constructor 

} 

public Promo(String durl, Boolean active, String timestamp) { 
    this.durl = durl; 
    this.active = active; 
    this.timestamp = timestamp; 
} 

public String getDurl() { 
    return durl; 
} 

public void setDurl(String durl) { 
    this.durl = durl; 
} 

public Boolean getActive() { 
    return active; 
} 

public void setActive(Boolean active) { 
    this.active = active; 
} 

public String getTimestamp() { 
    return timestamp; 
} 

public void setTimestamp(String timestamp) { 
    this.timestamp = timestamp; 
} 

}

PromoAdapter.javaクラス

public class PromoAdapter extends ArrayAdapter<Promo> { 

Context context; 
List<Promo> myList; 

public PromoAdapter(Context context, int resource, List<Promo> objects) { 
    super(context, resource, objects); 

    this.context = context; 
    this.myList = objects; 
} 


@Override 
public int getCount() { 
    if(myList != null) 
     return myList.size(); 
    return 0; 
} 

@Override 
public Promo getItem(int position) { 
    if(myList != null) 
     return myList.get(position); 
    return null; 
} 

@Override 
public long getItemId(int position) { 
    if(myList != null) 
     return myList.get(position).hashCode(); 
    return 0; 

} 


@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    Holder holder; 


    if (convertView == null){ 

     //we need a new holder to hold the structure of the cell 
     holder = new Holder(); 

     //get the XML inflation service 
     LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     convertView = inflater.inflate(R.layout.list_promo, null); 

     holder.imageView = (ImageView)convertView.findViewById(R.id.imageViewPromo); 

     convertView.setTag(holder); 

    }else{ 
     holder = (Holder)convertView.getTag(); 

    }  
    Promo promo = getItem(position); 
    Picasso.with(context) 
      .load(promo.getDurl()) 
      .placeholder(R.drawable.coin25) 
      .error(R.drawable.hnbspic) 
      .into(holder.imageView); 

    return convertView; 
} 


private class Holder{ 

    ImageView imageView; 

} 

私が試してみました:

  1. は、ブラウザで画像のURLをチェックすることは、有効な

  2. listenPrですOMOもIMAGEURL持つすべての子を得ることに成功(ピカソを使用する前に、データベースからデータを取得する前に、私はそれをテストしてみた)

  3. 変更load(promo.getDurl())

  4. load(uri.parse(promo.getDurl()))にも何の画像が表示されない、プレースホルダとエラー画像を追加。私の断片は空白です。

コードが機能している場合、3つの画像を表示する必要があります。

ご注意いただきありがとうございます。

+0

アダプターをリフレッシュすることが問題になることがあります。最初にアダプターを設定してから、イメージを取得してください.URLを使用してアダプターに通知してください。 – 9spl

+0

アダプターに設定する前にimageurlを配列に入れる必要があります。 –

+0

addValueEventListenerはバックグラウンドで動作するので、コードはバックグラウンドでそれを設定し、最初にsetAdapterに移動します。 Piccasoで画像を設定した場所の完全なコードを表示できますか? – 9spl

答えて

0

を教えてください。 onDataChangeでそれはあなたのために働くでしょう。それは最初setAdaterし、その後、あなたのresponse.soがちょうどアダプタがあなたのために働くだろう通知し得るよう

mDatabase.child("Promo").addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     // Get Post object and use the values to update the UI 
     // MainActivity.currUser = dataSnapshot.getValue(User.class); 

     for (DataSnapshot dsp : dataSnapshot.getChildren()) { 
      promoList.add(dsp.getValue(Promo.class)); //add result into array list 

     } 
     promoAdapter.notifyDataSetChanged(); 

     // ... 
    } 

    @Override 
    public void onCancelled(DatabaseError databaseError) { 
     // Getting User Class data failed... 
     Log.w("Canceled", "loadUserData:onCancelled", databaseError.toException()); 
     // ... 

    } 




}); 

addValueEventListenerはbackgoundで動作します。

+1

申し訳ありませんが、そのnot listviewPromo.notifyDataSetChanged()、しかし、promoAdapter.notifyDataSetChanged(); 、 ありがとうございました –

0

このコードを使用してください:

Transformation blurTransformation = new Transformation() { 
      @Override 
      public Bitmap transform(Bitmap source) { 
       Bitmap blurred = Blur.fastblur(context, source, 15); 
       source.recycle(); 
       return blurred; 
      } 

      @Override 
      public String key() { 
       return "blur()"; 
      } 
     }; 

    Picasso.with(context) 

        .load(R.drawable.coin25) 
        // thumbnail url goes here 
        .placeholder(R.drawable.coin25) 
        .transform(blurTransformation) 
        .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE) 
        .into(holder.imageView, new Callback() { 
         @Override 
         public void onSuccess() { 
          Picasso.with(context) 
            .load(promo.getDurl()) // image url goes here 
            .fit() 
            .centerCrop() 

            .error(R.drawable.hnbspic) 
            .placeholder(holder.imageView.getDrawable()) 
            .fit() 
            .into(holder.imageView); 

         } 

         @Override 
         public void onError() { 
         } 
        }); 

この.IF you.thenのために動作しないこれで試してください)私は(promoAdapter.notifyDataSetChangedを追加

0

あなたのコードによれば、アダプタは必ずしもリストアイテムを渡す必要はありません。 アダプターをグローバルにと宣言して、onDataChangeに変更が検出された場合は、adapter.notifyDataSetChanged()を追加するようにしてください。

関連する問題