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);

 

About Chandana Nalin Cooray 75 Articles
Chandan is a tech enthusiast and digital strategist with over 5 years of experience in Artificial Intelligence and Software development. He specializes in simplifying complex tech trends for global audiences. When he’s not reviewing the latest AI tools, he shares his insights on future technology on chandanai.com. Follow him on LinkedIn for more tech updates