Android Adapters

Spread the love

Android provides several sub classes of Adapter that are useful for retrieving different kinds of data and building views for an Adapter View . The two most common adapters are:

1. Array Adapter

2. Simple Customer Adapter

Array Adapter

ArrayAdapter adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, myStringArray);

  • The arguments for this constructor are:
  • Your app Context
  • The layout that contains a Text View for each string in the array
  • The string array

    ListView listView = (ListView) findViewById(R.id.listview);

    listView.setAdapter(adapter);

    • Then simply call setAdapter()on your ListView:

AdapterView.OnItemClickListener

 

// Create a message handling object as an anonymous class.
private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id) {
        // Do something in response to the click
    }
};

listView.setOnItemClickListener(mMessageClickedHandler);