Question

I'm trying to make a simple listView to list every bluetooth devices. When I find one, I just add it in a list and send it to the listview by using an array adapter. Everything is working well but I can't see the text because it's white on a white background (see picture)

screenshot

There is two item in the list but I can't see them.

I declare my values like this :

private ListView listViewBT = null;
private List<String> listBluetoothString = null;    
private ArrayList<BluetoothDevice> listBluetoothDevices = null;

I initialize them into the 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);

I launch the bluetooth discovery like this :

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

And finally this is my broadcast receiver (all the filters are initialized and registered)

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

Another weird thing is that the Toast in the ACTION_FOUND doesn't appear at all !

PS : Sorry for my English, it's not my mother tongue :)

Thanks in advance ! :)

EDIT :

This is my layout 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>
Was it helpful?

Solution

I'd bet this is a Context related problem. You're using getApplicationContext() which is absolutely wrong unless you really know what you're doing. You should use the Activity's context, or Fragments getActivity() method instead (as a Context might be cast to an Activity).

More on this:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top