带有蓝牙设备列表视图的意外文本颜色(白色)

时间:2014-04-12 17:02:33

标签: android listview android-listview bluetooth textcolor

我正在尝试制作一个简单的listView来列出每个蓝牙设备。当我找到一个时,我只需将其添加到列表中并使用数组适配器将其发送到列表视图。一切都运作良好,但我看不到文字,因为它在白色背景上是白色的(见图)

screenshot

列表中有两个项目,但我看不到它们。

我宣布我的价值观如下:

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

我将它们初始化为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);

我发布了这样的蓝牙发现:

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

最后这是我的广播接收器(所有过滤器都已初始化并注册)

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

另一个奇怪的事情是ACTION_FOUND中的Toast根本没有出现!

PS:对不起我的英语,这不是我的母语:)

提前致谢! :)

编辑:

这是我的布局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>

1 个答案:

答案 0 :(得分:2)

我敢打赌这是Context相关的问题。您正在使用getApplicationContext()这绝对是错误的,除非您真的知道自己在做什么。您应该使用Activity的上下文或Fragment getActivity()方法(因为Context可能会转换为Activity)。

更多相关内容: