Search code examples
androidandroid-arrayadapter

How can I extract a specific object from a list of objects in a single row of an ArrayAdapter in Android?


I have an ArrayAdapter called BTArrayAdapter & I'm trying to add two different Objects to it, one being a String and another one being a BluetoothDevice in a single row.

Following is my code:

if (BluetoothDevice.ACTION_FOUND.equals(action)) 
{   

    BluetoothDevice btd = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    BTArrayAdapter.add( btd.getName() + btd.getAddress() + btd);
}

Question is

How will I get a specific object from a single row?

BTArrayAdapter.getItem(position) returns a single row containing both, the String & the BluetoothDevice, how can I extract only the BluetooothDevice from it?

Thank you for your time!


Solution

  • you should create class like:

    class BlueToothContainer{
    
       public String adress;
       public String name;
       public BluetoothDevice device;
    
       //constructor etc..
    }
    

    Then your adapter should work on List of BlueToothContainers, thanks that you will get object and get from it only device, or only name etc.

    BTArrayAdapter.getItem(position).device //it is BluetoothDevice
    

    PS. ofcource your class should have private properties and getters to it, i writed this class on example.