5

Maybe I am asking the wrong question just for curiosity. I am creating the custom adapter by extending array adapter. Now I want to display this in GridView. I have gone through many articles and everywhere I found they are using only base adapter to display the GridView. Please guys tell me what is the logic behind this? Can we use Array adapter in place of Base Adapter?

Rahul Patil
  • 2,707
  • 2
  • 21
  • 32
rupesh
  • 2,865
  • 4
  • 24
  • 50

3 Answers3

4

Yes we can. ArrayAdpter itself extends from BaseAdapter. May be this will clear things up http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3_r2.1/android/widget/ArrayAdapter.java#ArrayAdapter

In-fact if you are just trying to show some stuff in ListView or GridView that doesn't require any complex custom adapter then its easier to just use an ArrayAdapter. Its just an adapter backed by an array of arbitrary objects.

M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
  • my doubt is if i will use custom adapter that is extending Array Adapter , so only in layout file i will have to do the change from ListView to GridView. Am i right?? – rupesh Dec 23 '13 at 06:29
  • 1
    You can use same adapter for either `GridView` or `ListView`. So yes you just need to change in layout and as well as where you call `setAdapter()`. – M-Wajeeh Dec 23 '13 at 06:30
2

You can certainly use ArrayAdapter in GridView. see this code example

gridView = (GridView) findViewById(R.id.gridView1);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, numbers);

gridView.setAdapter(adapter);

for more detail

Adhikari Bishwash
  • 2,770
  • 28
  • 32
  • But is it possible to load photos into a `GridView`, using `ArrayAdapter`? The code you gave uses a `simple_list_item_1`, which is a `TextView` layout. I am trying to find a way to use `ArrayAdapter` but to upload images, no text. – Azurespot Mar 19 '15 at 09:43
  • You can use custom adapter this blog http://www.mkyong.com/android/android-gridview-example/ explains – Adhikari Bishwash Mar 19 '15 at 11:17
  • Thanks, but that blog only covers simple text `String` in a `GridView`. Images are a whole other thing, as I'm learning, you need a model, and to change things into `Bitmap`, etc... :( – Azurespot Mar 20 '15 at 02:15
0
public class GridAdapter extends ArrayAdapter implements OnItemClickListener
    {
        Context c;
        int[] img;
        String[] p;
        public GridAdapter(Context context, int[] img1,String[] s1) {
            super(context,R.layout.forgridmain,R.id.textViewgd1,s1);
            img=img1;
            c=context;
            p=s1;
        }
        public View getView(int position,View ConvertView,ViewGroup parent)
        {
            LayoutInflater inflater=(LayoutInflater)c.getSystemService(LAYOUT_INFLATER_SERVICE);
            ConvertView =inflater.inflate(R.layout.forgridmain, parent, false);
            ImageView iv1=(ImageView) ConvertView.findViewById(R.id.imageView111);
            TextView tv=(TextView)ConvertView.findViewById(R.id.textViewgd1);
            RelativeLayout rl=(RelativeLayout)ConvertView.findViewById(R.id.relalypout);
            rl.setBackgroundColor(Color.TRANSPARENT);
            iv1.setImageURI(Uri.parse("android.resource://"+getPackageName()+"/drawable/"+img[position]));
            tv.setText(p[position]);
            gv.setOnItemClickListener(ga);  
            return ConvertView;

        }
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

            Intent trans=new Intent(MainActivity.this,Listed.class);
            trans.putExtra("first",p[arg2]);
            startActivity(trans);
        }


    }

I used this to add images and text in a grid view. The layout that grid view inflates contains one imageView and one textView.

adao7000
  • 3,632
  • 2
  • 28
  • 37
codeKnight
  • 103
  • 1
  • 6