2017-04-06 66 views
0

請看看,我有一個片段,設置一個適配器,從Firebase數據庫中獲取字符串url值,但沒有圖像加載。firebase加載圖像在listview中使用picasso

我的片段:

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(網址下載)爲這種情況。

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. 檢查瀏覽器中的圖像網址有效

  2. 的listenPr大面也讓所有與IMAGEURL孩子成功(我測試過它之前使用之前畢加索得到數據庫中的數據)

  3. 變化load(promo.getDurl())load(uri.parse(promo.getDurl()))

  4. 添加佔位符和錯誤的形象,也沒有圖像出現。我的片段是空白的。

請幫忙..如果代碼工作,那麼它應該顯示3張圖片。

感謝您的任何關注。

+0

問題是刷新適配器,它先設置適配器,然後得到圖像url.just通知適配器後,在onDataChange – 9spl

+0

得到sussess響應imageurl應該在數組之前,我將它設置爲適配器。 –

+0

addValueEventListener在後臺工作,因此代碼將其設置在背景中並首先移動到setAdapter。你可以顯示完整的代碼,你用Piccaso設置圖像。 – 9spl

回答

0

添加promoAdapter.notifyDataSetChanged();在onDataChange中,它會爲你工作。

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工作,因此將setAdater第一,然後你會得到你的response.so只是通知適配器會爲你工作。

+1

對不起,它不是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() { 
         } 
        }); 

嘗試用這個。如果這對you.then不工作,請讓我知道

0

根據您的代碼,適配器不一定會傳遞列表項。我強烈建議您將適配器全局聲明爲並添加adapter.notifyDataSetChanged()當在onDataChange上檢測到任何更改時。

相關問題