2012-08-07 24 views
2

我有一個從數據庫中提取圖像URL的列表。一旦它得到的網址,我使用下面的代碼將它們存儲在可繪:不止一次顯示的列表中的圖像

private Drawable LoadImageFromWebOperations(String url){ 
    try{ 
     InputStream is = (InputStream) new URL(url).getContent(); 
     Drawable d = Drawable.createFromStream(is, "src name"); 
     return d; 
    }catch (Exception e) { 
     System.out.println("Exc="+e); 
     return null; 
    } 
} 

然後我保存繪製成一個鏈表,並繼續拉動其他圖像的URL,一旦做到這一點的畫面得到顯示。我遇到的問題是它能夠很好地拖動網頁,但是當它顯示在應用上時,前兩張圖片很好,但當我滾動查看第三張圖片時,它會顯示第一張圖片,對此有何看法? ?

下面是getview和自定義列表代碼:

private class RowData { 

    protected int mId; 
    protected String mTitle; 

    RowData(int id,String title){ 
    mId=id; 
    mTitle = title; 
    } 
    @Override 
    public String toString() { 
      return mId+" "+mTitle+" "; 
    } 
} 


private class CustomAdapter extends ArrayAdapter<RowData> { 

    public CustomAdapter(Context context, int resource, 
        int textViewResourceId, List<RowData> objects) { 
      super(context, resource, textViewResourceId, objects); 

    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
      ViewHolder holder = null; 

      TextView title = null; 
      TextView date = null; 
      TextView detail = null; 
      ImageView i11=null; 
      String postDate; 

      RowData rowData= getItem(position); 

      if(null == convertView){ 
        convertView = mInflater.inflate(R.layout.newspg, null); 
        holder = new ViewHolder(convertView); 
        convertView.setTag(holder);     
      } 
      holder = (ViewHolder) convertView.getTag();  

      title = holder.gettitle();  
      title.setText(rowData.mTitle); 
      postDate = getpostDate(news, rowData.mTitle); 
      Log.i("Date",postDate); 
      date = holder.getDate();  
      date.setText(postDate); 



      i11=holder.getImage(position); 
      Log.i("pos",Integer.toString(position)); 
      //i11.setImageResource(imgid[0]); 

      return convertView; 
    } 




    private class ViewHolder {  
     private View mRow; 
     private TextView title = null; 
     private TextView date = null; 
     private TextView detail = null; 
     private ImageView i11=null;    

      public ViewHolder(View row) { 
      mRow = row; 
      } 

      public TextView gettitle() { 
        if(null == title){ 
          title = (TextView) mRow.findViewById(R.id.title); 
        } 
        return title; 
      } 

      public TextView getDate() { 
       if(null == date){ 
         date = (TextView) mRow.findViewById(R.id.date); 
       } 
       return date; 
     } 


      public ImageView getImage(int position) { 
       if(null == i11){ 

        i11 = (ImageView) mRow.findViewById(R.id.img); 

        i11.setImageDrawable(newArtists.get(position).getNewsPic()); 

      } 

      return i11; 
    } 

} 
} 
+1

看起來好像你正試圖在'ListView'中使用它們,因爲你沒有正確地調用'ListAdapter'中的單元格。 – hwrdprkns 2012-08-07 20:24:53

+0

是的,我使用的是listview,你會推薦我使用什麼? – paul590 2012-08-07 20:39:46

+0

我已經描述了發生的事情,通常它是自定義列表適配器中的錯誤。你發佈的代碼看起來很好。 – Samuel 2012-08-07 20:47:29

回答

0

這是發生,因爲列表視圖重新使用以前的行(與包括您的圖片的意見),以建立新的。所以你必須注意這一點。例如,您可以隱藏ImageView,直到您將完美的圖片放入ImageView並將其顯示出來爲止。

+0

這個伎倆!非常感謝! – paul590 2012-08-07 21:14:16

+0

我有這個完全相同的問題。除了我可以看到9個可見,並向下滾動到第10個,和第1個​​一樣。你能否詳細說明你的意思是隱藏imageview,直到你得到好的照片。謝謝。 – Doomsknight 2012-08-07 22:48:47

+1

我所做的事情是將圖片設置爲不可見,並且只有在將圖片放入圖像視圖時才能看到它 – paul590 2012-08-08 23:13:50