2010-05-29 15 views
8

Tôi đang đặt ra để tạo hoạt động cài đặt cho ứng dụng của mình. Tôi đã xác định PreferenceActivity với bố cục đẹp bao gồm đối tượng ListPreference để người dùng chọn thiết bị bluetooth. Tôi gặp sự cố khi tự động điền danh sách.Có thể tải các mục ListPreference từ bộ điều hợp không?

Tôi muốn điền ListPreference với các giá trị từ bộ điều hợp mảng (mà tôi sẽ tạo và điền với tên thiết bị bluetooth có liên quan).

Nếu đây là chế độ xem xoay tròn, tôi chỉ có thể gọi setAdapter(). Tuy nhiên với đối tượng ListPreference tôi không thể tìm ra cách gắn bộ điều hợp (findviewByID sẽ không truyền từ View To ListPreference, vì vậy tôi thậm chí không thể xử lý đối tượng).

Tôi muốn đính kèm bộ điều hợp và sau đó điền vào bộ điều hợp với các giá trị, do đó sẽ điền các giá trị ListPreference.

Trả lời

7

ListPreference không hoạt động với bộ điều hợp, nó hoạt động với chuỗi. Xem setEntries()setEntryValues()

Để có được tại ListPreference của bạn, hãy gọi findPreference() trên PreferenceActivity của bạn. Truyền số Preference bạn quay lại ListPreference.

+0

Tốt, đẹp. Cảm ơn bạn! –

10

Đối với trường hợp đặc biệt của một danh sách các thiết bị bluetooth, bạn có thể sử dụng lớp sau:

package de.duenndns; 

import android.bluetooth.*; 
import android.content.Context; 
import android.preference.ListPreference; 
import android.util.AttributeSet; 

import java.util.Set; 

public class BluetoothDevicePreference extends ListPreference { 

    public BluetoothDevicePreference(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter(); 
     Set<BluetoothDevice> pairedDevices = bta.getBondedDevices(); 
     CharSequence[] entries = new CharSequence[pairedDevices.size()]; 
     CharSequence[] entryValues = new CharSequence[pairedDevices.size()]; 
     int i = 0; 
     for (BluetoothDevice dev : pairedDevices) { 
      entries[i] = dev.getName(); 
      if (entries[i] == null) entries[i] = "unknown"; 
      entryValues[i] = dev.getAddress(); 
      i++; 
     } 
     setEntries(entries); 
     setEntryValues(entryValues); 
    } 

    public BluetoothDevicePreference(Context context) { 
     this(context, null); 
    } 

} 

Nó có thể được trực tiếp tham gia từ XML prefs của bạn để lưu trữ các MAC như là một chuỗi prefs:

<de.duenndns.BluetoothDevicePreference 
    android:key="bluetooth_mac" 
    android:title="Bluetooth Device" 
    android:dialogTitle="Choose Bluetooth Device" /> 
+0

Trước hết: cảm ơn tất cả vì đoạn mã đó. Nó hoạt động tốt. nhưng trong tôi đã có một vấn đề dựng hình trong bản xem trước trong AS. 'Rendering Problems java.lang.NullPointerException tại de.jphjung.speedcamssiegerland.BluetoothDevicePreference. (BluetoothDevicePreference.java:20) tại java.lang.reflect.Constructor.newInstance (Constructor.java:423) tại android.preference.GenericInflater.createItem (GenericInflater.java:385) [... và một số thông tin khác ... quá nhiều cho giới hạn ký tự đó ở đây] – beta

4

Chỉ cần cập nhật trong trường hợp người khác đi cùng, câu trả lời của ge0rg hoạt động nhưng đã thay đổi một chút để xem xét nhiều tùy chọn chứ không chỉ tùy chọn và cũng không có thiết bị được ghép nối để bạn không nhận được một lỗi với một mảng null.

ListPreference BTList = (ListPreference) findPreference("your preference key"); 
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); 
    CharSequence[] entries = new CharSequence[1]; 
    CharSequence[] entryValues = new CharSequence[1]; 
    entries[0] = "No Devices"; 
    entryValues[0] = ""; 
    if(pairedDevices.size() > 0){ 
     entries = new CharSequence[pairedDevices.size()]; 
     entryValues = new CharSequence[pairedDevices.size()]; 
     int i=0; 
     for(BluetoothDevice device : pairedDevices){ 
      entries[i] = device.getName(); 
      entryValues[i] = device.getAddress(); 
      i++; 
     } 
    } 
    BTList.setEntries(entries); 
    BTList.setEntryValues(entryValues); 

'Hy vọng điều này sẽ giúp người ... oh và điều này được đặt theo phương thức onCreate cho hoạt động ưu tiên

4

Một cách khác sẽ được ghi đè onPrepareDialogBuilder của ListPreference và để khởi setSingleChoiceItems của AlertDialog.Builder trực tiếp với bộ chuyển đổi của bạn:

public class AdapterListPreference extends ListPreference 
{ 
    @Override 
    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) 
    { 
     // don't call super.onPrepareDialogBuilder() because it'll check 
     // for Entries and set up a setSingleChoiceItems() for them that 
     // will never be used 

     final ListAdapter adapter = …; 

     builder.setSingleChoiceItems(
      adapter, 
      0, 
      new DialogInterface.OnClickListener() 
      { 
       @Override 
       public void onClick(DialogInterface dialog, int which) 
       { 
        // adapter.getItemId(which) 

        dialog.dismiss(); 
       } 
      }); 

     builder.setPositiveButton(null, null); 
    } 
} 

Nếu bạn nhìn vào các nguồn Android, bạn sẽ thấy rằng onPrepareDialogBuilder() không gọi:

public AlertDialog.Builder setSingleChoiceItems (CharSequence[] items, int checkedItem, DialogInterface.OnClickListener listener) 

với các mảng nhập đó. Để làm ListPreference sử dụng một số bộ chuyển đổi (ví dụ ArrayAdaper, CursorAdapter), bạn chỉ cần gọi:

public AlertDialog.Builder setSingleChoiceItems (ListAdapter adapter, int checkedItem, DialogInterface.OnClickListener listener) 

để thay thế.

Cách này ListPreference sẽ hoạt động trên bộ điều hợp trực tiếp và bạn không cần phải sao chép dữ liệu từ bộ điều hợp để đưa chúng vào các mảng mục nhập.

Tìm mẫu đang hoạt động here.

Các vấn đề liên quan