0

why and how , this line is processing the whole array label.setText(items[position]); without any loop condition !

this is my , class which is accessing the array_adapter class . for resources ....

class FunnyLookingAdapter extends ArrayAdapter 
{
    Context ctxt;

    FunnyLookingAdapter(Context ctxt, int resource,String[] items) 
    {
        super(ctxt, resource, items);
        this.ctxt=ctxt;
    }

    public View getView(int position, View convertView,ViewGroup parent) 
    {
        TextView label=(TextView)convertView;

        if (convertView==null) 
        {
            convertView=new TextView(ctxt);
            label=(TextView)convertView;
        }

        label.setText(items[position]); //------------------ this is the line !
        return(convertView);
    }
}

and this phase of code is using the previous class . so the code is ::

    GridView grd = ( GridView ) findViewById ( R.id.gd_v );

    grd.setAdapter(new FunnyLookingAdapter(this,
            android.R.layout.simple_list_item_1,
            items));

and the String resource array is :

String[] items={"lorem", "ipsum", "dolor", "sit", "amet",
        "consectetuer", "adipiscing", "elit", "morbi", "vel",
        "ligula", "vitae", "arcu", "aliquet", "mollis",
        "etiam", "vel", "erat", "placerat", "ante",
        "porttitor", "sodales", "pellentesque", "augue", "purus"};

2 Answers2

0

Try this code:

Context ctxt;
String[] mItems;

FunnyLookingAdapter(Context ctxt, int resource,String[] items) 
{
    super(ctxt, resource, items);
    this.ctxt=ctxt;
    this.mItems = items;
}

public View getView(int position, View convertView,ViewGroup parent) 
{
    TextView label=(TextView)convertView;

    if (convertView==null) 
    {
        convertView=new TextView(ctxt);
        label=(TextView)convertView;
    }

    label.setText(mItems[position]); //------------------ this is the line !
    return(convertView);
}

This way you can access your items array. In your code the String array is accessible only by the constructor method, so the getView() function couldn't actually see it.

Pavlos
  • 2,183
  • 2
  • 20
  • 27
  • thanks , but i think you dont understand my question .... i am asking that without any loop , how this code can access the array , label.setText(mItems[position]); ---------- from this line ! – Tushar Pandey Dec 05 '12 at 11:59
  • Look at the answer below mine. The guy there explains very well your misunderstanding! – Pavlos Dec 05 '12 at 12:43
0

The Adapter is designed in such a way that, it will take the count of items and will create that many views for the List to show.

In simple words The Loop(which you are thinking of) is written in the Super class so you don't have to write again and it simply process the whole array.

There are many good links you can refer to understand the Adapters.

  1. Understanding BaseAdapters and how to use them

  2. Understanding the Adapter in the Android SDK

  3. Pro - Android

  4. Learning android

Community
  • 1
  • 1
MKJParekh
  • 34,073
  • 11
  • 87
  • 98