2015-04-23 70 views
-1

對不起,我的英語。我正在使用適配器,並且我可能會錯誤地使用它。我試圖顯示圖像和textView作爲輸出。 Textview在輸出中正常顯示,但圖像不是。請幫助我,我嘗試了很多次修復它。在DownloadImageTask加載圖片。在DownloadImageTask我有類ImageLoader,我用this tutorial。我沒有任何錯誤,我有什麼(適配器不輸出圖像(安卓)

ArrayList<News> ArrayListNews; 
int Resourse; 
Context context; 
LayoutInflater vi; 

public NewsAdapter(Context context, int resource, ArrayList<News> objects) { 
    super(context, resource, objects); 

    ArrayListNews = objects; 
    Resourse = resource; 
    this.context = context; 

    vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

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

    ViewHolder holder; 
    if(convertView == null) { 
     convertView = vi.inflate(Resourse, null); 
     holder = new ViewHolder(); 

     holder.imageview = (ImageView) convertView.findViewById(R.id.imagenews); 
     holder.nameNews = (TextView) convertView.findViewById(R.id.namenews); 
     holder.dayNews = (TextView) convertView.findViewById(R.id.daynews); 

     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    new DownloadImageTask(holder.imageview).execute(ArrayListNews.get(position).getImageNews()); 
    holder.nameNews.setText(ArrayListNews.get(position).getNameNews()); 
    holder.dayNews.setText(ArrayListNews.get(position).getDayNews()); 

    return convertView; 
} 

static class ViewHolder { 
    public ImageView imageview; 
    public TextView nameNews; 
    public TextView dayNews; 
} 




//this is class set image 
private class DownloadImageTask extends AsyncTask<String, Void, String> { 
    ImageView bmImage; 

    ImageLoader imgLoader; 

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

    @Override 
    protected String doInBackground(String... urls) { 
     String urldisplay = urls[0]; 
     return urldisplay; 

    } 

    protected void onPostExecute(String result) { 

     imgLoader = new ImageLoader(context.getApplicationContext()); 
     imgLoader.DisplayImage(result, bmImage); 

    } 
} 
+0

你需要緩存嗎?或者你只是想加載圖片? – Paritosh

+0

@Paritosh我想只是加載圖像。我用魔術棒在列表視圖中顯示圖像 – wqedasdqwdasdasdw

回答

1

我會建議你使用Picasso library,這是非常容易使用。只需添加依賴(Android Studio中)或添加.jar文件到您的圖書館(在Eclipse)。

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView); 

就是這樣。現在,你將不再需要AsyncTask下載圖像。

getView()

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

    ViewHolder holder; 
    if(convertView == null) { 
     convertView = vi.inflate(Resourse, null); 
     holder = new ViewHolder(); 

     holder.imageview = (ImageView) convertView.findViewById(R.id.imagenews); 
     holder.nameNews = (TextView) convertView.findViewById(R.id.namenews); 
     holder.dayNews = (TextView) convertView.findViewById(R.id.daynews); 

     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    // I am not sure about return value of ArrayListNews.get(position).getImageNews(); 
    // It should be URL pointing to your image file 
    Picasso.with(context).load(ArrayListNews.get(position).getImageNews()).into(holder.imageview); 

    holder.nameNews.setText(ArrayListNews.get(position).getNameNews()); 
    holder.dayNews.setText(ArrayListNews.get(position).getDayNews()); 

    return convertView; 
}