无法在数组适配器内显示值

时间:2013-11-04 06:58:55

标签: android

 public class DeviceListActivity extends Activity {
        protected static final String TAG = "TAG";
        private BluetoothAdapter mBluetoothAdapter;
        private ArrayAdapter<String> mPairedDevicesArrayAdapter;

        @Override
        protected void onCreate(Bundle mSavedInstanceState) {
            super.onCreate(mSavedInstanceState);
            requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
            setContentView(R.layout.activity_device_list);

            setResult(Activity.RESULT_CANCELED);
            mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this,
                    R.layout.activity_device_list, RESULT_CANCELED);

            ListView mPairedListView = (ListView) findViewById(R.id.paired_devices);
            mPairedListView.setAdapter(mPairedDevicesArrayAdapter);
            mPairedListView.setOnItemClickListener(mDeviceClickListener);

            mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            Set<BluetoothDevice> mPairedDevices = mBluetoothAdapter
                    .getBondedDevices();

            if (mPairedDevices.size() > 0) {
                // findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
                for (BluetoothDevice mDevice : mPairedDevices) {
                    mPairedDevicesArrayAdapter.add(mDevice.getName() + "\n"
                            + mDevice.getAddress());
                }
            } else {
                String mNoDevices = "None Paired";// getResources().getText(R.string.none_paired).toString();
                mPairedDevicesArrayAdapter.add(mNoDevices);
            }
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            if (mBluetoothAdapter != null) {
                mBluetoothAdapter.cancelDiscovery();
            }
        }

        private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
            public void onItemClick(AdapterView<?> mAdapterView, View mView,
                    int mPosition, long mLong) {
                mBluetoothAdapter.cancelDiscovery();
                String mDeviceInfo = String.valueOf(mPairedDevicesArrayAdapter
                        .getItem(mPosition).toString());
                String mDeviceAddress = mDeviceInfo
                        .substring(mDeviceInfo.length() - 17);
                Log.v(TAG, "Device_Address " + mDeviceAddress);

                Bundle mBundle = new Bundle();
                mBundle.putString("DeviceAddress", mDeviceAddress);
                Intent mBackIntent = new Intent();
                mBackIntent.putExtras(mBundle);
                setResult(Activity.RESULT_OK, mBackIntent);
                finish();
            }
        };

    }

device_list.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/title_paired_devices"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#666"
            android:paddingLeft="5dip"
            android:text="My Text"
            android:textColor="#fff"
            android:visibility="gone" />

        <ListView
            android:id="@+id/paired_devices"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:stackFromBottom="true" />

    </LinearLayout>

我写上面的代码来显示当前在线的ListView中的所有蓝牙设备。但是当我尝试这个代码时,我得到以下异常。这个代码工作正常而不使用数组适配器

 11-04 12:03:48.504: E/AndroidRuntime(8902): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

3 个答案:

答案 0 :(得分:1)

更改此

mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this,
                R.layout.activity_device_list, RESULT_CANCELED);

mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, RESULT_CANCELED);

以上使用内置布局android.R.layout.simple_list_tiem1作为列表视图

11-04 12:03:48.504: E/AndroidRuntime(8902): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

你的适配器有错误的参数。

mPairedDevicesArrayAdapte = new ArrayAdapter<String>(this, R.layout.list_item, RESULT_CANCELED);

list_item.xml //仅xml中的textview

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="test"
    />

或者

    mPairedDevicesArrayAdapte= new ArrayAdapter<String>(this, R.layout.list_item,R.id.textView1, RESULT_CANCELED);

拥有list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="test"
    />

</RelativeLayout>

答案 1 :(得分:0)

这里传递的是你的布局ID,但是arrayadapter只接受textview资源id ..

 mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this,
                R.layout.activity_device_list, RESULT_CANCELED);

这里你必须传递textview资源id ..比如..

 mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, RESULT_CANCELED);

答案 2 :(得分:0)

错误表明ArrayAdapter requires the resource ID to be a TextView

The docs on ArrayAdapter

  

public ArrayAdapter(Context context,int resource,int textViewResourceId)

     

构造

     

参数
  context当前的上下文。

     

资源
  布局文件的资源ID,包含实例化视图时使用的布局。

     

textViewResourceId
  要填充的布局资源中 TextView 的ID

RESULT_CANCELED似乎不是布局中TextView的ID。您也将它设置为活动内容视图的布局相同,也可能不正确。

我建议您阅读Building Layouts with an Adapter以获取更多信息。