2015-01-31 78 views
1

我試圖用已連接到設備的所有藍牙設備填充我的ListView。我通過調試去和ArrayAdapter被接收的數據,但不能將其通過向ListViewListView未從ArrayList數據填充

我的代碼如下所示

Connect2.java

public class Connect2 extends ListFragment { 

private LayoutInflater classInflater; 
private ViewGroup classContainer; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 

    View v = inflater.inflate(R.layout.connect2,container,false); 
    final ListView listview = (ListView) v.findViewById(android.R.id.list); 

    BluetoothAdapter myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    Set<BluetoothDevice> pairedDevices = myBluetoothAdapter.getBondedDevices(); 

    List<String> previousDevices = new ArrayList<String>(); 
    for(BluetoothDevice bt : pairedDevices) { 
     previousDevices.add(bt.getName()); 
    } 

    ArrayAdapter BTListAdapter = new ArrayAdapter<String>(getActivity(), R.layout.connect2, previousDevices); 

    listview.setAdapter(BTListAdapter); 

    return inflater.inflate(R.layout.connect2, container, false); 
} 
} 

Connect2.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:baselineAligned="false"> 


    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:paddingLeft="10dp" 
      android:layout_weight="1" 
      android:id="@+id/connectNewBtn" 
      android:text="@string/newBT"/> 

     <Switch 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:paddingRight="10dp" 
      android:text="@string/BTtoggle" 
      android:id="@+id/BTSwitch" 
      android:layout_weight="0" 
      android:layout_gravity="end"/> 

    </LinearLayout> 


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

</LinearLayout> 
+0

您是否收到任何錯誤? – Rohit5k2 2015-01-31 22:20:03

+0

請看我的答案。您沒有返回加載哪個列表視圖的視圖。您正在返回一個全新的視圖。 – Rohit5k2 2015-01-31 22:22:21

回答

0

很愚蠢的錯誤,看到th e返回語句

您沒有返回已加載listview的視圖。您正在返回一個全新的視圖。

+0

美麗!我唯一需要改變的是將'R.layout.connect2'改爲'android.R.simple_list_item_1' – henryjarend 2015-01-31 23:17:40

+0

哦,是的,我沒有注意到。反正很高興它的工作。 – Rohit5k2 2015-01-31 23:20:40