2012-03-15 22 views
-1

我遵循hello gallery教程,但不起作用。它顯示空白屏幕Android Hello Gallery不支持

我已經將示例圖像上傳到可繪製文件夾中。

這裏是main.xml中代碼

<?xml version="1.0" encoding="utf-8"?> 
<Gallery xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gallery" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" /> 

attr.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<declare-styleable name="HelloGallery"> 
    <attr name="android:galleryItemBackground" /> 
</declare-styleable> 
</resources> 

活動類:

public class GalleryActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Gallery gallery = (Gallery) findViewById(R.id.gallery); 
    gallery.setAdapter(new ImageAdapter(this)); 

    gallery.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View v, 
       int position, long id) { 

      Toast.makeText(GalleryActivity.this, "" + position, 
        Toast.LENGTH_SHORT).show(); 
     } 

    }); 
    } 
} 

ImageAdapter類:

public class ImageAdapter extends BaseAdapter { 

int mGalleryItemBackground; 
Context mContext; 

private Integer[] mImageIds = { R.drawable.sample_0, 
     R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, 
     R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, 
     R.drawable.sample_7 }; 

public ImageAdapter(Context context) { 

    mContext = context; 

    TypedArray attr = mContext 
      .obtainStyledAttributes(R.styleable.HelloGallery); 
    mGalleryItemBackground = attr.getResourceId(
      R.styleable.HelloGallery_android_galleryItemBackground, 0); 
    attr.recycle(); 

    } 

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

    ImageView imageView = new ImageView(mContext); 
    imageView.setImageResource(mImageIds[position]); 
    imageView.setLayoutParams(new Gallery.LayoutParams(150, 100)); 
    imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
    imageView.setBackgroundResource(mGalleryItemBackground); 

    return imageView; 
    } 
} 

當我調試的代碼,我注意到,該方法getView從未執行過。 請求幫助。

感謝

回答

0

getCount()方法在Adapter類中?在這裏發佈的代碼。您沒有正確複製示例代碼。請先做。

+0

謝謝。有用。我認爲這種方法並不重要。 – user1134475 2012-03-16 01:36:50

1

你需要在你ImageAdapter重寫這些方法:

public int getCount() { 
    // TODO Auto-generated method stub 
    return 0; 
} 

public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return null; 
} 

請檢查。