ListView is one of the most commonly used View in android. I observed that most of the newbie in android find it very difficult to implement it.
So lets start our work with a simple code example...
here are the steps.
now the most important method in this adapter class is getView method. if you want to make your own custom list containing some images, text etc then you will have to override this method properly.
What this method does is it generates the view of each row of list, as soon as you scroll the list. getView gets called each time you move through the list. It inflates the new layout from the layout resources or uses the recycled layout from the recycler to generate the view of list rows.
Here comes the new word 'Recycler'. Actually what android does is it stores only those items in the memory which are visible+ one view in Recycler.
As we run our application, this getView method will be called and it inflates layout from resources for all the visible items. But when we scroll through our list, the item that scrolls out of the screen moves to Recyler.
then to display next item getView does not inflate layout from resources but it uses the view that has already been stored in Recycler.
when there is no view available in the recycler then the parameter "convertView" in the getView method gets a null value. So that we can inflate layout on the basis of this value.
I found an Image on Internet, i thought this would help you to visualize the getView properly.
We did the similar thing in the above example......
So lets start our work with a simple code example...
here are the steps.
- Make a class that extends ListActivity.
- Make your own custom Adapter class. Now what this custom adapter class do is set our data and inflate layout on for listview.
- Override the required set of methods in your custom adapter class.
- that is it.....still have confusions? It seems there are lots of confusions. Then have a look at the code snippet. This might solve all your confusions......
public class ListExample extends ListActivity {//extend your class with ListActivity. private MyAdapter mAdapter;//Make an instance of your custom Adapter Class(MyAdapter). @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mAdapter = new MyAdapter(); for (int i = 0; i < 124; i++) { mAdapter.addItem("Data " + i);//Add some Data to your adapter. } setListAdapter(mAdapter);//now set this adapter to your list } /**this is your custom adapter class.
*/ private class MyAdapter extends BaseAdapter { private ArrayList mData = new ArrayList(); private LayoutInflater mInflater; public MyAdapter() { mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public void addItem(final String item) { mData.add(item); notifyDataSetChanged(); } @Override public int getCount() {//this defines the size of the list. return mData.size(); } @Override public String getItem(int position) { return mData.get(position); } @Override public long getItemId(int position) { return position; }
/**getView is called internally by adapter to generate the view of he rows of the list*/ @Override public View getView(int position, View convertView, ViewGroup parent) { System.out.println("getView " + position + " " + convertView); ViewHolder holder = null; if (convertView == null) { convertView = mInflater.inflate(R.layout.item1, null); holder = new ViewHolder(); holder.textView = (TextView)convertView.findViewById(R.id.text); convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } holder.textView.setText(mData.get(position)); return convertView; } } public static class ViewHolder { public TextView textView; } }
What this method does is it generates the view of each row of list, as soon as you scroll the list. getView gets called each time you move through the list. It inflates the new layout from the layout resources or uses the recycled layout from the recycler to generate the view of list rows.
Here comes the new word 'Recycler'. Actually what android does is it stores only those items in the memory which are visible+ one view in Recycler.
As we run our application, this getView method will be called and it inflates layout from resources for all the visible items. But when we scroll through our list, the item that scrolls out of the screen moves to Recyler.
then to display next item getView does not inflate layout from resources but it uses the view that has already been stored in Recycler.
when there is no view available in the recycler then the parameter "convertView" in the getView method gets a null value. So that we can inflate layout on the basis of this value.
I found an Image on Internet, i thought this would help you to visualize the getView properly.
We did the similar thing in the above example......