android中的对话框中未显示ListView
我试图在我的android应用程序中打开一个对话框,显示附近蓝牙设备的选定列表。我在对话框中使用android中的对话框中未显示ListView,android,listview,baseadapter,android-dialog,android-adapterview,Android,Listview,Baseadapter,Android Dialog,Android Adapterview,我试图在我的android应用程序中打开一个对话框,显示附近蓝牙设备的选定列表。我在对话框中使用ListView来显示列表,并为其适配器扩展BaseAdapter。问题是myBaseAdapter的getView()方法即使在getCount()函数返回大于0的值时也不会被调用。这是我的密码: public class ScanNearbyDevicesDialog extends Dialog { // UI: LoginActivity activity; Cons
ListView
来显示列表,并为其适配器扩展BaseAdapter
。问题是myBaseAdapter
的getView()
方法即使在getCount()
函数返回大于0的值时也不会被调用。这是我的密码:
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的父对象是对话框时,
getView()
才会被调用。有没有办法解决这个问题?我终于找到了问题所在。由于对话框没有特定的高度,因此会缩小到其内容高度。我将列表视图的高度设置为匹配约束
,使其收缩为零。在我将其高度设置为wrap\u content
之后,一切都很顺利。这是一个愚蠢的错误,但我写我的经验与任何人分享谁会遇到类似的问题
<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>