0
我有一個自定義的BaseAdapter是加載圖像到一個自定義的列表視圖使用異步任務的網址,但圖像不顯示。這裏是適配器:ListView項目不加載從url的圖像
private class HorizontalAdapter extends BaseAdapter{
public HorizontalAdapter() {
super();
final Checkpoint current = localCheckpoints[checkpointPosition];
urls[0] = current.getGallery()[0].getUrl();
urls[1] = current.getYoutube();
urls[2] = current.getSoundcloud();
}
@Override
public int getCount() {
return 3;
}
@Override
public Object getItem(int position) {
return urls[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String urlToLoad = urls[position];
if(convertView == null){
convertView = LayoutInflater.from(getActivity()).inflate(R.layout.horizontal_listview_item,parent,false);
}
ImageView imageView = (ImageView)convertView.findViewById(R.id.cp_image);
FrameLayout container = (FrameLayout)convertView.findViewById(R.id.youtubeShower);
Button playButton = (Button)convertView.findViewById(R.id.playStop);
playButton.setVisibility(View.VISIBLE);
if(position == 0){
Log.d("horizontalsv","loading image with url: " + urlToLoad);
new loadImage(imageView).execute(urlToLoad);
//container.setVisibility(View.VISIBLE);
imageView.setVisibility(View.VISIBLE);
}
else if(position == 1){
container.setVisibility(View.VISIBLE);
imageView.setVisibility(View.INVISIBLE);
String videoId = extractYTId(urlToLoad);
PlayYoutubeVideFragment youtubeFrag = PlayYoutubeVideFragment.newInstance(videoId);
myContext.getSupportFragmentManager().beginTransaction().replace(R.id.youtubeShower, youtubeFrag).commit();
}else{
}
return convertView;
}
}
,這裏是爲ListView項的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
>
<FrameLayout
android:id="@+id/youtubeShower"
android:layout_width="match_parent"
android:layout_height="288dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true">
</FrameLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cp_image"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="87dp"
android:text="Play"
android:id="@+id/playStop"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
這裏是怎麼了加載圖像:
private class loadImage extends AsyncTask<String,Void,Bitmap>{
ImageView target;
public loadImage(ImageView imageViewToLoad){
target = imageViewToLoad;
if(imageViewToLoad == null){
Log.e("in loadImage","imageview is null");
}else{
Log.d("in loadImage","imageView is there");
}
}
@Override
protected Bitmap doInBackground(String... params) {
String urlString = params[0];
try {
URL url = new URL(urlString);
try {
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
return bmp;
}catch(IOException e){
Log.e("IOException",e.getMessage());
return null;
}
}catch (MalformedURLException e){
Log.e("URL ERROR", "url = " + urlString + " and error = " + e.getMessage());
return null;
}
}
@Override
protected void onPostExecute(Bitmap o) {
super.onPostExecute(o);
if(o != null){
Log.d("in loadimage","bitmap is not null");
target.setImageBitmap(o);
//lvTest.setAdapter(new HorizontalAdapter());
}else{
Log.e("in loadimage","bitmap is null");
target.setImageBitmap(null);
target.setBackgroundColor(Color.WHITE);
}
}
}
是否有任何理由爲什麼圖像不加載?位圖在加載圖像asynctask中作爲NOT NULL返回,所以它應該可以工作。
你只是想加載第一個位置的圖像? (「如果位置== 0」行) – Tushar