Search code examples
androidandroid-listviewandroid-arrayadapter

ListView not populating from ArrayList data


I am trying to populate my ListView with all Bluetooth devices that have been connected to the device. I went through a debug and the ArrayAdapter was receiving the data but not passing it through to the ListView

My code is shown below

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>

Solution

  • Very silly mistake, see the return statement

    You are not returning the view on which listview was loaded. You are returning a completely new view.

    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 v; // CHANGED THIS LINE ONLY
    }