Replace entry in ArrayAdapter
Is there any way to replace value in ArrayAdapter
mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
..
..
for (BluetoothDevice device : pairedDevices) {
String name = MPGDeviceDetailsControl.getDeviceDetails(this, device.getAddress(), device.getName()).getDisplayName();
mPairedDevicesArrayAdapter.add(name + "\n" + device.getAddress());
}
If I want to replace one of the entries, is there a way to do this without deleting or restoring. The problem is that it fits at the bottom.
+3
source to share
3 answers
Something like this should work to replace the elements
int i = 0;
for (BluetoothDevice device : pairedDevices) {
String name = MPGDeviceDetailsControl.getDeviceDetails(this, device.getAddress(), device.getName()).getDisplayName();
mPairedDevicesArrayAdapter.remove(name); // has to copy all items back 1 position
mPairedDevicesArrayAdapter.insert(name + "\n" + device.getAddress(), i); // copy them +1 again
i++;
}
but that would be more efficient if you have access to the list that this ArrayAdapter supports and replaces / modifies that.
ArrayList<Strign> mList = new ArrayList<String>();
mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name, mList);
// setup
mList.add...
mPairedDevicesArrayAdapter.notifyDatasetChanged();
// change something
mList.set(i, newValue); // just a replace, no copy
mPairedDevicesArrayAdapter.notifyDatasetChanged();
+14
source to share
You can replace the displayed value without retyping and using nested adapters. Just change the original collection and call adapter notifyDatasetChanged ()
//adapter initialization
MyClass[] someArray = ... ;
ArrayList<Strign> adapter = new ArrayList<String>(context, R.layout.item_layout, someArray);
//change something
someArray[i] = newValue;
adapter.notifyDatasetChanged();
This is pretty much the same as zapl's answer, except that it doesn't use nested adapters. It should work the same when the adapter is initialized with a list, but I haven't tested it.
0
source to share