0

I've written this code from a tutorial.

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    // TODO Auto-generated method stub
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.single_row, viewGroup,false);

    TextView title = (TextView) row.findViewById(R.id.txtTitle);
    TextView description = (TextView) row.findViewById(R.id.txtDescription);
    ImageView image = (ImageView) row.findViewById(R.id.imgPic);

    SingleRow temp = list.get(i);

    title.setText(temp.title);
    description.setText(temp.description);
    image.setImageResource(temp.image);


    return row;
}

In this line of code:

TextView title = (TextView) row.findViewById(R.id.txtTitle);

I think a TextView is copied to a variable of the same kind. and then in this line of code:

title.setText(temp.title);

we fill that variable with something. then the row variable which is a View and it's not related to 'title' variable is returned.
How it works? I thinks these variables have nothing to do here.

3 Answers3

0

That's the method used to return the view for a row in the listview. row variable is actually related to title, as you can see here.-

TextView title = (TextView) row.findViewById(R.id.txtTitle);

in other words, title is a TextView inside row object, and that code retrieves it to set its text. To sum up, the whole getView method is inflating a single_row View, and setting properties for all relevant children of row.

ssantos
  • 16,001
  • 7
  • 50
  • 70
0
View row = inflater.inflate(R.layout.single_row, viewGroup,false);

You are inflating a layout single_row

TextView title = (TextView) row.findViewById(R.id.txtTitle);

Initializing textview which is in singlerow.xml

title.setText(temp.title);

Setting title to textview.

You are infalting a layout for each row in listview.

Also it is better to use a viewHolder pattern.

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

Also you can move the below to the constructor of adapter class and declare inflater as a class member

 inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

The below link may be of interest to you

How ListView's recycling mechanism works

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

This code inflates a new view, settings it's contents. This means that you're creating a new view programatically. It's often used i.e. when populating a list, where you'll have number of rows, each identical in structure, but with different values.

Here is how it works:

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    // Get the inflater service
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    // Inflate view with ID = R.layout.single_row into the row variable
    View row = inflater.inflate(R.layout.single_row, viewGroup,false);

    // Get child views of row: title, description and image.
    TextView title = (TextView) row.findViewById(R.id.txtTitle);
    TextView description = (TextView) row.findViewById(R.id.txtDescription);
    ImageView image = (ImageView) row.findViewById(R.id.imgPic);

    // This get's some template view which will provide data: title, description and image
    SingleRow temp = list.get(i);

    // Here you're setting title, description and image by using values from `temp`.
    title.setText(temp.title);
    description.setText(temp.description);
    image.setImageResource(temp.image);

    // Return the view with all values set. This view will be later probably added somewhere as a child (maybe into a list?)
    return row;
}
kamituel
  • 34,606
  • 6
  • 81
  • 98