繁体   English   中英

ListView未显示在Android的对话框中

[英]ListView not Showing in a Dialog in android

我正在尝试在我的Android应用程序中打开一个对话框,该对话框显示附近的蓝牙设备的选定列表。 我在对话框中使用ListView来显示列表并为其适配器扩展BaseAdapter 问题是,即使getCount()函数返回的值大于0,我的BaseAdaptergetView()方法也不会被调用。这是我的代码:

public class ScanNearbyDevicesDialog extends Dialog {

    // UI:
    LoginActivity activity;
    ConstraintLayout layout;
    ProgressBar progressBar;
    ImageView replayImageView;
    ListView devicesListView;
    ImageView addImageView;
    TextView addTextView;
    TextView noDeviceTextView;
    Button cancelButton;
    LeDeviceListAdapter devicesAdapter;

    // Bluetooth:
    BluetoothAdapter bluetoothAdapter;
    boolean scanning;

    Handler handler;

    ScanNearbyDevicesDialog(LoginActivity activity, BluetoothAdapter bluetoothAdapter){
        super(activity);
        this.activity = activity;
        this.bluetoothAdapter = bluetoothAdapter;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // UI:
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.scan_nearby_devices_dialog);
        layout = findViewById(R.id.constraintLayout);

        // set screen transition animation:
        TransitionManager.beginDelayedTransition(layout);

        setCanceledOnTouchOutside(false);

        // devices list view:
        devicesAdapter = new LeDeviceListAdapter();
        devicesListView.post(new Runnable() {
            @Override
            public void run() {
                devicesListView.setAdapter(devicesAdapter);
            }
        });

        // initialization:
        handler = new Handler();


    }

    @Override
    protected void onStart() {
        super.onStart();
        scanLeDevices(true);
    }

    // Device scan callback.
    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {

            if (device == null) return;

            // check some filter, then add it to the list view:
            String deviceAddress = device.getAddress();
            if (deviceAddress != null && someCondition){
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        devicesAdapter.addDevice(device, someName);
                    }
                });
            }

        }
    };


    /**
     * turn ble scan on or off.
     * @param enable on or off
     */
    private void scanLeDevices(final boolean enable) {
        if (enable) {
            // Stops scanning after a pre-defined scan period.
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (scanning) {

                        // scanning timed out
                        scanning = false;
                        bluetoothAdapter.stopLeScan(mLeScanCallback);

                    }
                }
            }, SCANNING_PERIOD);
            scanning = true;
            devicesAdapter.clear();
            bluetoothAdapter.startLeScan(mLeScanCallback);
        } else {
            scanning = false;
            bluetoothAdapter.stopLeScan(mLeScanCallback);
        }
    }

这是我的适配器, getView()方法不对其进行调用:

/**
 * Adapter for holding devices found through scanning.
 */
public class LeDeviceListAdapter extends BaseAdapter {

    private ArrayList<BluetoothDevice> mLeDevices;
    private ArrayList<String> names;
    private LayoutInflater mInflator;

    LeDeviceListAdapter() {
        super();
        mLeDevices = new ArrayList<>();
        names = new ArrayList<>();
        mInflator = getLayoutInflater();
    }

    void addDevice(BluetoothDevice device, String name) {
        if (!mLeDevices.contains(device)) {
            mLeDevices.add(device);
            names.add(name);
        }
        notifyDataSetChanged();
    }

    BluetoothDevice getDevice(int position) {
        return mLeDevices.get(position);
    }

    String getName(int position){ return names.get(position); }

    void clear() {
        mLeDevices.clear();
        names.clear();
        notifyDataSetChanged();
    }

    public int getCount() {
        return names.size();
    }

    public Object getItem(int i) {
        return names.get(i);
    }

    public long getItemId(int i) {
        return i;
    }

    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder viewHolder;

        // General ListView optimization code.
        // todo why doesnt it stop here?
        if (view == null) {
            view = mInflator.inflate(R.layout.list_item_ble_device, viewGroup, false);
            viewHolder = new ViewHolder();
            viewHolder.deviceName = view.findViewById(R.id.device_name);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        // set the UI:
        String deviceName = names.get(i);
        viewHolder.deviceName.setText(deviceName);

        return view;
    }

    class ViewHolder {
        TextView deviceName;
    }
}

这是ListView项的布局:

<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:orientation="vertical">

<TextView
    android:id="@+id/device_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="16dp"
    android:layout_marginTop="8dp"
    android:background="@android:color/transparent"
    android:singleLine="false"
    android:text="device name"
    android:textColor="@android:color/black"
    android:textSize="16sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.499"
    app:layout_constraintVertical_chainStyle="packed" />

</android.support.constraint.ConstraintLayout>

还有一件事,我试图将该对话框更改为一个新活动,然后它可以正常工作。 问题在于,当ListView的父级是对话框时,不会仅调用getView() 关于如何解决这个任何想法?

我终于找到了问题所在。 由于对话框没有特定的高度,因此它们会缩小到其内容的高度。 我已经将ListView的高度设置为match_constraint ,这使其缩小match_constraint 在将其高度设置为wrap_content一切正常。 这是一个愚蠢的错误,但是我正在写我的经验,以便与遇到类似问题的任何人分享。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM