0

I am learning android program for some few days but now I am wondering about the use of adapter in listview .Please help me to understand this topic.The code I am learning is in below

       dictionaryListView.setAdapter(new BaseAdapter() {

        @Override
        public View getView(int position, View view, ViewGroup arg2) {
            if (view==null) {
                view=getLayoutInflater().inflate(R.layout.list_item, null);
            }
            TextView textView=(TextView) view.findViewById(R.id.listItemTextView);
            textView.setText(allWordDefinitions.get(position).word);

            return view;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return allWordDefinitions.size();
        }
    });

    dictionaryListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position,
                long arg3) {
            Intent intent =new Intent(DictionaryListActivity.this, WordDefinitionDetailActivity.class);
            intent.putExtra("word", allWordDefinitions.get(position).word);
            intent.putExtra("definition", allWordDefinitions.get(position).definition);

            startActivity(intent);
        }
    });

What is the need of setadapter method here

4 Answers4

6

Quoting 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.

You are use a BaseAdapter overriding getView where you inflate the custom layout and return a view object.

http://developer.android.com/reference/android/widget/BaseAdapter.html

 view=getLayoutInflater().inflate(R.layout.list_item, null);// inflate custom layout

and

 return view;

So each row has a textview.

 @Override
        public View getView(int position, View view, ViewGroup arg2) {
            if (view==null) { // only when view in null inflate the layout
                view=getLayoutInflater().inflate(R.layout.list_item, null);
            }
            TextView textView=(TextView) view.findViewById(R.id.listItemTextView);
            // initialize the textview
            textView.setText(allWordDefinitions.get(position).word);
            // set text to textview
            // position is the index 

            return view; // return view
        }

getCount returns the size of the list ie return allWordDefinitions.size();. This indicates the number of rows in listview.

Also read How ListView's recycling mechanism works

You should also consider using a ViewHolder pattern.

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

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • is the position in textView.setText(allWordDefinitions.get(position).word); set by default? –  Feb 07 '14 at 17:56
  • what default? you set the text to textview based on the index ie position. the list contains the data. for 1st time index 0 , next index 1 and so on.... – Raghunandan Feb 07 '14 at 17:58
0

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.

The adapter is responsible for creating the views for each individual item in your ListView (or other AbsListView based class), and using the data you provide it to fill in the views.

2Dee
  • 8,609
  • 7
  • 42
  • 53
0

enter code hereFrom Android doc

Adapters in Android are a bridge between the Adapter View (e.g. ListView) 
and the underlying data for that view. Imagine what would 
have been the world without adapters!

enter image description here

Without Adapters, to implement the ListView functionality, you will need to: Create a TextView within a ScrollView group. Then you will have to implement pagination concept for the contents of the TextView. You will also have to write additional code to identify the click event on a particular row in the TextView.

Here the Link to know more

Nambi
  • 11,944
  • 3
  • 37
  • 49
0

Adapter is Component that take your Data and attach it to controls you have in layout.

getView()

this method in adapter will call each time you will create list item and you will use it to attach each data into specific control

getItemId()

this method you will implement it to return id of each data you attach to your list items so you can get

this id in onitemclick event in ListView

getItem this method like getItemId but it return the object itself

this method getCount will return the count of list items that adapter will create for you and to know how many time will call getView method for you

mohammed momn
  • 3,230
  • 1
  • 20
  • 16