【发布时间】:2018-09-06 13:43:01
【问题描述】:
我正在尝试在我的 android 应用程序中打开一个对话框,其中显示附近的蓝牙设备的选定列表。我在对话框中使用ListView 来显示列表并为其适配器扩展BaseAdapter。问题是我的BaseAdapter 的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 项目的布局:
<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>
还有一件事,我尝试将此对话框更改为新活动,然后它可以正常工作。问题是 getView() 只有在 ListView 的父级是对话框时才会被调用。关于如何解决这个问题的任何想法?
【问题讨论】:
标签: android listview baseadapter android-dialog android-adapterview