使用listview和arrayadapter显示蓝牙配对设备

时间:2015-08-03 19:55:38

标签: android listview bluetooth android-arrayadapter

我正在尝试在列表视图中显示已配对的设备,我尝试了谷歌蓝牙指南,但很难:(我的代码无法运行,应用程序崩溃和logcat ArrayAdapter requires the resource ID to be a TextView这里是我的代码:

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

所以,我也尝试将arrayadapter分配给R.id.textview,但是textview没有显示出来......

我的xml文件:

    <ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/list" />

1 个答案:

答案 0 :(得分:1)

执行以下步骤(这可能包括ListView上的图像):

1-在您的布局中使用ListView:

<ListView
    android:id="@+id/paired_devices_listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

2-为适配器创建新布局(名称示例: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-创建一个扩展String的ArrayAdapter的新类:

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-包含在您的活动中:

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
    }

}

如果您不想获取地址,并且想要获取设备名称,请使用String value = names.get(position)代替String value = (String) mListView.getItemAtPosition(position)

注意:
你得到代码的例子也揭示了地址,“只得到名字或只有地址”可能有点困难。