Couleur de texte inattendue (blanc) avec vue en liste des appareils Bluetooth
-
21-12-2019 - |
Question
J'essaie de créer une simple listView pour répertorier tous les appareils Bluetooth.Quand j'en trouve un, je l'ajoute simplement dans une liste et je l'envoie à la liste à l'aide d'un adaptateur matriciel.Tout fonctionne bien mais je ne vois pas le texte car il est blanc sur fond blanc (voir photo)
Il y a deux éléments dans la liste mais je ne les vois pas.
Je déclare mes valeurs comme ceci :
private ListView listViewBT = null;
private List<String> listBluetoothString = null;
private ArrayList<BluetoothDevice> listBluetoothDevices = null;
Je les initialise dans OnCreate :
listViewBT = (ListView) findViewById(R.id.listViewBT);
listBluetoothDevices = new ArrayList<BluetoothDevice>();
listBluetoothString= new ArrayList<String>();
adapterKnown = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,listBluetoothString);
listViewBT.setAdapter(adapterKnown);
listViewBT.setClickable(true);
listViewBT.setOnItemClickListener(listClick);
Je lance la découverte Bluetooth comme ceci :
void findBT()
{
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter == null)
{
Toast.makeText(getApplicationContext(), "Cet appareil ne dispose pas de bluetooth", Toast.LENGTH_SHORT).show();
}
if(!mBluetoothAdapter.isEnabled())
{
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQUEST_CODE_ENABLE_BLUETOOTH);
}
if (mBluetoothAdapter.isEnabled()){
mBluetoothAdapter.cancelDiscovery();
mBluetoothAdapter.startDiscovery();
}
}
Et enfin voici mon récepteur de diffusion (tous les filtres sont initialisés et enregistrés)
bluetoothReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
listBluetoothDevices.add(device);
listBluetoothString.add(device.getName()+"\n"+device.getAddress());
adapterKnown.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), device.getName(), Toast.LENGTH_SHORT);
}
else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){
Toast.makeText(getApplicationContext(), "Discovering devices", Toast.LENGTH_SHORT).show();
}
else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
//Toast.makeText(getApplicationContext(), "Discover finished", Toast.LENGTH_SHORT).show();
}
else if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){
if(mBluetoothAdapter.getState() == mBluetoothAdapter.STATE_OFF){
finish();
}
}
}
};
Une autre chose bizarre est que le Toast dans ACTION_FOUND n'apparaît pas du tout !
PS :Désolé pour mon anglais, ce n'est pas ma langue maternelle :)
Merci d'avance !:)
MODIFIER :
Ceci est ma mise en page XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:text="Input"/>
<EditText
android:id="@+id/et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Type here"
android:inputType="text"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/discover"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Discover"/>
<Button
android:id="@+id/getData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get"/>
<Button
android:id="@+id/disconnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close"/>
</LinearLayout>
<ListView
android:id="@+id/listViewBT"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
La solution
Je parierais que c'est un Context
problème connexe.Vous utilisez getApplicationContext()
ce qui est absolument faux à moins que vous sachiez vraiment ce que vous faites.Vous devriez utiliser le Activity
le contexte, ou Fragment
s getActivity()
méthode à la place (en tant que Context
pourrait être jeté à un Activity
).
Plus à ce sujet :