GridView and ImageAdapter

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

GridView and ImageAdapter

Postby timweicker » Thu Jun 11, 2009 7:15 pm

Sorry to bother you again, but - again a simple problem:

---images.java---
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package test.images;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.os.Bundle;
  8.  
  9. import android.widget.GridView;
  10.  
  11.  
  12.  
  13. public class images extends Activity {
  14.  
  15.  
  16.  
  17.         /** Called when the activity is first created. */
  18.  
  19.         @Override
  20.  
  21.         public void onCreate(Bundle savedInstanceState) {
  22.  
  23.                 super.onCreate(savedInstanceState);
  24.  
  25.                 setContentView(R.layout.main);
  26.  
  27.                 GridView gridview = (GridView) findViewById(R.id.gridview);
  28.  
  29.                 gridview.setAdapter(new ImageAdapter(this));
  30.  
  31.         }
  32.  
  33. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


---ImageAdapter.java---
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package test.images;
  2.  
  3.  
  4.  
  5. import android.content.Context;
  6.  
  7. import android.view.View;
  8.  
  9. import android.view.ViewGroup;
  10.  
  11. import android.widget.BaseAdapter;
  12.  
  13. import android.widget.ImageView;
  14.  
  15.  
  16.  
  17. public class ImageAdapter extends BaseAdapter {
  18.  
  19.     private Context mContext;
  20.  
  21.  
  22.  
  23.     public ImageAdapter(Context c) {
  24.  
  25.         mContext = c;
  26.  
  27.     }
  28.  
  29.  
  30.  
  31.     public int getCount() {
  32.  
  33.         return images.length;
  34.  
  35.     }
  36.  
  37.  
  38.  
  39.     public Object getItem(int position) {
  40.  
  41.         return null;
  42.  
  43.     }
  44.  
  45.  
  46.  
  47.     public long getItemId(int position) {
  48.  
  49.         return 0;
  50.  
  51.     }
  52.  
  53.  
  54.  
  55.     public View getView(int position, View convertView, ViewGroup parent) {
  56.  
  57.         ImageView imageView;
  58.  
  59.         if (convertView == null) {
  60.  
  61.             imageView = new ImageView(mContext);
  62.  
  63.         } else {
  64.  
  65.             imageView = (ImageView) convertView;
  66.  
  67.         }
  68.  
  69.  
  70.  
  71.         imageView.setImageResource(images[position]);
  72.  
  73.         return imageView;
  74.  
  75.     }
  76.  
  77.     public Integer[] images = {
  78.  
  79.     };
  80.  
  81. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Now, when I'm in images.java, how can I add images to the field ImageAdapter.images? I tried
ImageAdapter.images[0] = R.drawable.icon;
and several other things, but this doesn't work.

Do you have any idea?

Best regards,
Tim
timweicker
Junior Developer
Junior Developer
 
Posts: 20
Joined: Wed Jun 10, 2009 7:55 pm

Top

Postby vili07 » Wed Jun 17, 2009 10:27 am

Hi,
You must create Integer[] Array for contain Image resource. Example:
private Integer[] mThumbIds = {

R.drawable.icon1,
R.drawable.icon2,
R.drawable.icon3,
}
Then, You call them: imageView.setImageResource(mThumbIds[position]);
If you want, you can set Id for image.
vili07
Freshman
Freshman
 
Posts: 3
Joined: Wed Jun 17, 2009 10:16 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests