开发者

question on getView()

Can anyone possible explain what the following code does?

  public View getView(int position, View convertView, ViewGr开发者_JAVA技巧oup parent) {

   // TODO Auto-generated method stub
   View myView = null;
   try {
    myView = convertView;
    if (null == myView) {
     LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

     myView = li.inflate(R.layout.demographic_list_item, null);

    }
    if (mScan_listItems[position] != null) {
     // read the values and attach them.
     TextView tv1 = (TextView) myView
       .findViewById(R.id.DemoGraphicItem_Text);

     tv1.setText(mScan_listItems[position]);

    }

   } catch (Exception e) {
    e.printStackTrace();
   }

   return myView;
  }

 }


The Adapter.getView docs give some indication of the use of getView:

public abstract View getView (int position, View convertView, ViewGroup parent)

Get a View that displays the data at the specified position in the data set. You can either create a View manually or inflate it from an XML layout file. When the View is inflated, the parent View (GridView, ListView...) will apply default layout parameters unless you use inflate(int, android.view.ViewGroup, boolean) to specify a root view and to prevent attachment to the root.

position

The position of the item within the adapter's data set of the item whose view we want.

convertView

The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view.

parent

The parent that this view will eventually be attached to

Returns

A View corresponding to the data at the specified position.


getView will get called for every item in your dataset. From the Adapter API docs:

An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.


Build a custom view by implementing Baseadapter interface. It has two important methods:getcount() and getview(). The getcount() method returns the number of views the adapter is giving to listview or gridview. The getview() method will give the views one by one. Its convertview attribute is crucial as it returns a previously created view. Check for null and if it is not null use the same view for a different position for efficiency or else inflate a new view and return it to the parent.


package com.stampcollections;
import java.util.Vector;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import com.database.DBAdapter;
import com.techvalens.servercommunication.WebServiceDetails;
public class GridViewFirst extends Activity implements OnItemClickListener,        
WebServiceDetails
{
public static int position = 0;
GridView MyGrid;
DBAdapter dataBase = new DBAdapter(this);
Vector <String>vector2 = new Vector<String>();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gridfirst);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));
    gridview.setOnItemClickListener(this);
}
public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

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

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

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

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some  
                attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setBackgroundResource(R.drawable.stampbackground);
        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {

      R.drawable.ghandig,R.drawable.madhu,R.drawable.munshi,R.drawable.namdev,
      R.drawable.otter,R.drawable.panda,R.drawable.rabindranath,R.drawable.rail,
      R.drawable.rail1,R.drawable.raj,R.drawable.taj,R.drawable.tortoise
    };
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    //  Intent intent = new Intent(GridViewFirst.this,TabActivity.)
    switchTabInActivity(3);
    position = arg2;

}
public void switchTabInActivity(int indexTabToSwitchTo){
    TabActivity ParentActivity;
    ParentActivity = (TabActivity) this.getParent();
    ParentActivity.switchTab(indexTabToSwitchTo);

}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜