Reputation: 333
I am trying to display the already paired devices in a listview, I tried google Bluetooth guide but it was hard :( my code is not working, app crashes and logcat ArrayAdapter requires the resource ID to be a TextView
here is my code:
private BluetoothAdapter bluetooth;
private ArrayAdapter<String> mArrayAdapter;
...
...
(oncreate)
bluetooth = BluetoothAdapter.getDefaultAdapter();
final ListView newDevicesListView = (ListView)
findViewById(R.id.list);
mArrayAdapter = new ArrayAdapter<String>(this, R.layout.main, R.id.list);
newDevicesListView.setAdapter(mArrayAdapter);
newDevicesListView.setVisibility(View.INVISIBLE);
...
(connect button onclick)
else {
//bluetooth available.
if (!bluetooth.isEnabled()) {
// available off.
textview.setText("bluetooth is disabled, enabling.");
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, RESULT_OK);
} else {
bluetooth.startDiscovery();
Set<BluetoothDevice> bondedSet = bluetooth.getBondedDevices();
if (bondedSet.size() > 0) {
for (BluetoothDevice device : bondedSet) {
newDevicesListView.setVisibility(View.VISIBLE);
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
} else {
mArrayAdapter.add("No Devices");
}
}
}
So, I also tried to assign the arrayadapter to R.id.textview but the textview doesn't show something..
my xml file:
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list" />
Upvotes: 0
Views: 1818
Reputation:
Do the following steps (This may include images on ListView):
1- In your layout use ListView:
<ListView
android:id="@+id/paired_devices_listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
2- Create a new layout for the adapter (Example of name: layout_adapter.xml
):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/adapter_image"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/adapter_text"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/img"
android:layout_toEndOf="@+id/img" />
</RelativeLayout>
3- Create a new class that extends an ArrayAdapter of Strings:
public class CustomAdapter extends ArrayAdapter<String> {
private Activity mContext;
private ArrayList<String> mNames;
private ArrayList<Drawable> mImages;
//The ArrayAdapter constructor
public CustomAdapter(Activity context, ArrayList<String> names, ArrayList<Drawable> images, ArrayList<String> values) {
super(context, R.layout.layout_adapter, values);
//Set the value of variables
mNames = names;
mImages = images;
}
//Here the ListView will be displayed
@Override
public View getView(int position, View view, ViewGroup parent) {
View layoutView = mContext.getLayoutInflater().inflate(R.layout.layout_adapter, null, true);
TextView mTextView = (TextView) layoutView.findViewById(R.id.adapter_text);
ImageView mImageView = (ImageView) layoutView.findViewById(R.id.adapter_image);
mTextView.setText(mNames.get(position));
mImageView.setImageDrawable(mImages.get(position));
return layoutView;
}
}
4- Include in your activity:
public class YourActivityClass extends Activity implements AdapterView.OnItemClickListener {
private ListView mListView;
private ArrayList<String> names;
private ArrayList<Drawable> images;
private ArrayList<String> addresses;
private BluetoothAdapter mBluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBluetoothAdapter = BluetoothAdapter.getBluetoothAdapter();
mListView = (ListView) findViewById(R.id.paired_devices_listview);
names = new ArrayList<String>();
images = new ArrayList<Drawable>();
addresses = new ArrayList<String>();
for (BluetoothDevice device : mBluetoothAdapter.getBondedDevices()) {
names.add(device.getName());
images.add(getDrawableByMajorClass(device.getBluetoothClass().getMajorDeviceClass()));
addresses.add(device.getAddress());
}
CustomAdapter adapter = new CustomAdapter(this, names, images, addresses);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(this);
}
private Drawable getDrawableByMajorClass(int major) {
Drawable drawable = null;
switch (major) {
case BluetoothClass.Device.Major.COMPUTER:
drawable = getResources().getDrawable(R.drawable.computer_icon);
break;
case BluetoothClass.Device.Major.PHONE:
drawable = getResources().getDrawable(R.drawable.phone_icon);
break;
default:
drawable = getResources().getDrawable(R.drawable.some_bluetooth_device_icon);
break;
}
return drawable;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//String value is the address...
//...of selected device
String value = (String) mListView.getItemAtPosition(position);
//Do your stuff here
}
}
If you don't want to get the address and you want to get the name of device, use String value = names.get(position)
instead of String value = (String) mListView.getItemAtPosition(position)
NOTE:
The example that you got the code reveals the address too and "get only the name or only the address" can be a little hard.
Upvotes: 1