2011-06-25 4 views
0

Tout d'abord je suis un débutant.Android Tutoriel HelloGallery Problème

De toute façon, donc j'essayais de jouer avec le Hello Gallery tutorial. Et je suis coincé à l'étape # 6. Je ne sais pas où la méthode onCreate se termine.

`@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

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

    g.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView parent, View v, int position, long id) { 
      Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show(); 
     } 
    }); 
}' 

Où puis-je mettre ceci? Il dit juste « Retour au fichier HelloGallery.java Après la onCreate méthode (Bundle), définir la classe ImageAdapter personnalisée. »

'public class ImageAdapter extends BaseAdapter { 
    int mGalleryItemBackground; 
    private Context mContext; 

    private Integer[] mImageIds = { 
      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 c) { 
     mContext = c; 
     TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery); 
     mGalleryItemBackground = a.getResourceId(
       R.styleable.HelloGallery_android_galleryItemBackground, 0); 
     a.recycle(); 
    } 

    public int getCount() { 
     return mImageIds.length; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView i = new ImageView(mContext); 

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

     return i; 
    } 
}' 

Répondre

1

Une méthode se termine après son accolade } finale. Il suffit de mettre le code après le bloc entier.

@Override 
public void onCreate(Bundle savedInstanceState) { 
... // stuff you pasted before 
} 

// put new code here 
+0

ouais, désolé, c'est tout nouveau pour moi. Essayer de comprendre comment afficher du code comme vous l'avez fait. Juste une minute. – Cataroux

+0

Juste pour que je puisse me familiariser avec le fonctionnement de la galerie. – Cataroux

+0

vous n'avez pas besoin de publier tout ce code. Insérez simplement la nouvelle classe juste après la méthode 'onCreate'. Ce sera une classe interne de 'HelloGallery'. – Mat