What's the role of adapters in Android?

前端 未结 10 1172
面向向阳花
面向向阳花 2020-12-02 04:10

I want to know when, where and how adapters are used in the context of Android.

The information from Android\'s developer documentation wa

相关标签:
10条回答
  • 2020-12-02 04:50

    There are already given multiple answer,But I want to give a different answer.

    Adapter means you can that Its bridge provider.

    Adapters are the link between a set of data and the AdapterView that displays the data.

    0 讨论(0)
  • 2020-12-02 04:55

    At the end, adapters are very useful to do a report. If one wants to show a report of some information, one can use this tool to show the data on the view.

    0 讨论(0)
  • 2020-12-02 04:56

    Let’s assume you want to display a list in your Android app. For this you will use the ListView provided by Android. ListViews don’t actually contain any data themselves. It’s just a UI element without data in it. You can populate your ListViews by using an Android adapter.

    Adapter is an interface whose implementations provide data and control the display of that data. ListViews own adapters that completely control the ListView’s display. So adapters control the content displayed in the list as well as how to display it.

    The Adapter interface includes various methods to communicate data to the ListView. You can create your own adapter from scratch by implementing BaseAdapter.

    public class ArrayAdapter<T> extends BaseAdapter implements Filterable {
    
    // One of the constructors
    public ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects) {
        init(context, resource, textViewResourceId, Arrays.asList(objects));
    }
    
    void manyMoreMethods(){} 
    
    }
    

    Lets define an adapter:

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
       android.R.layout.simple_list_item_1, android.R.id.text1, values);
    
    • First parameter: Context
    • Second parameter: Layout for the row
    • Third parameter: ID of the TextView to which the data is written
    • Fourth parameter: The array of data
    0 讨论(0)
  • 2020-12-02 04:56

    An adapter acts as a bridge between an AdapterView and the underlying data for that view. The adapter provides access to the data items and is responsible for creating a view for each item in the data set.

    Adapters are a smart way to connect a View with some kind of data source. Typically, your view would be a ListView and the data would come in form of a Cursor or Array. So adapters come as subclasses of CursorAdapter or ArrayAdapter.

    0 讨论(0)
提交回复
热议问题