2015-06-25 156 views
0

我特林負荷的圖像和文字來使用arrayAdapter定製,但負荷圖像的ListView控件失敗負荷圖像定製ArrayAdapter

這裏是我arrayAdapter

public class CustomAdapter extends ArrayAdapter<Post> { 

    private ImageTask image; 

    public CustomAdapter(Context context, ArrayList<Post> posts) { 
     super(context, 0, posts); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // Get the data item for this position 
     Post post = getItem(position);  
     // Check if an existing view is being reused, otherwise inflate the view 
     if (convertView == null) { 
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.post_list, parent, false); 
     } 
     // Lookup view for data population 
     TextView tituloView = (TextView) convertView.findViewById(R.id.titulo); 
     TextView subtituloView = (TextView) convertView.findViewById(R.id.subTitulo); 
     ImageView fotoView = (ImageView) convertView.findViewById(R.id.imageView); 
     TextView textoView = (TextView) convertView.findViewById(R.id.texto); 


     // Populate the data into the template view using the data object 
     tituloView.setText(post.post_titulo); 
     subtituloView.setText(post.post_sub_titulo); 
     //this like idk if are the way corret for load the image into ViewImage specific 
     new ImageTask(fotoView).execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png"); 

     textoView.setText(post.post_texto); 
     // Return the completed view to render on screen 
     return convertView; 
    } 
} 

這裏ImageTask圖像

public class ImageTask extends AsyncTask<String, Void, Bitmap> { 
    ImageView bmImage; 

    public ImageTask(ImageView bmImage) { 
     this.bmImage = bmImage; 
    } 

    protected Bitmap doInBackground(String... urls) { 
     String urldisplay = urls[0]; 
     Bitmap mIcon11 = null; 
     try { 
      InputStream in = new java.net.URL(urldisplay).openStream(); 
      mIcon11 = BitmapFactory.decodeStream(in); 
     } catch (Exception e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } 
     return mIcon11; 
    } 

    protected void onPostExecute(Bitmap result) { 
     bmImage.setImageBitmap(result); 
    } 
} 

這裏來自MainActivity的呼叫

// Create the adapter to convert the array to views 
CustomAdapter adapter = new CustomAdapter(this, arrayOfPost); 
// Attach the adapter to a ListView 
ListView listView = (ListView) findViewById(R.id.lvUsers); 
listView.setAdapter(adapter); 

的arrayOfPost被解析瓊森的來到了一個ArrayList和鏈接被包含有圖像,但在這個例子中我使用圖像從谷歌顯影劑

和通過這裏陣列適配器

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

     <TextView 
      android:id="@+id/titulo" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

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

     <TextView 
      android:id="@+id/subTitulo" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <TextView 
      android:id="@+id/texto" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      /> 
</LinearLayout> 

使用的XML任何知道哪些是我的錯誤,以及我應該如何處理加載圖像? 我不正確的,如果是corret負載的方式張貼在特定ViewImage圖像 //此行是在拳頭類

new ImageTask(fotoView).execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png"); 
+0

什麼故障? – zdd

+0

圖像不要猿入設備,只是文本 –

+0

你應該加入這個問題。 – zdd

回答

2

步驟在的AsyncTask加載圖像

第1步:

ImageView fotoView = (ImageView) convertView.findViewById(R.id.imageView); 

步驟2:

String URL1 = "http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png"; 

步驟3:

fotoView.setTag(URL1); 
new DownloadImageTask.execute(fotoView); 

第4步:

public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> { 

ImageView imageView = null; 

@Override 
protected Bitmap doInBackground(ImageView... imageViews) { 
    this.imageView = imageViews[0]; 
    return download_Image((String)imageView.getTag()); 
} 

@Override 
protected void onPostExecute(Bitmap result) { 
    imageView.setImageBitmap(result); 
} 

private Bitmap download_Image(String url) { 

    Bitmap bmp =null; 
    try{ 
     URL ulrn = new URL(url); 
     HttpURLConnection con = (HttpURLConnection)ulrn.openConnection(); 
     InputStream is = con.getInputStream(); 
     bmp = BitmapFactory.decodeStream(is); 
     if (null != bmp) 
      return bmp; 

     }catch(Exception e){} 
    return bmp; 
} } 

參考:Android : Loading an image from the Web with Asynctask

+0

好兄弟正是這個現在的工作:) –

+0

不客氣! – ganeshvjy

1

我看看,有哪些可以在你的代碼加以改進幾件事情:
1.在您的自定義適配器中,您應該使用ViewHolder來提高您的ListView性能,因爲這些行:convertView.findViewById()成本太高。
2.您使用AsyncTaskGetView()上載您的圖片。沒關係,但只要你滾動ListView,你的GetView()被調用,你開始另一個AsyncTask。無法保證哪個Task會先完成,因此您在ListView中的圖像位置可能不正確(現在您在每行中加載相同的圖像,這不會發生,但將來會發生)。
因此,我的建議是:在你的適配器使用ViewHolder,使用一些第三方Lib加載圖像有效,如Universal ImageLoader

+0

真的工作,發生什麼你告訴,真的加載緩慢:),現在將尋找提示提示:) –