Подскажите пожалуйста, нужно передать ListView со списком Блютуз в DialodFragment, создание списка в layout работает корректно? но вот в Fragment открывается пустой прямоугольник без списка.
public class MainActivity extends AppCompatActivity {
private BluetoothAdapter bluetoothAdapter;
private ListView listView;
private ArrayList pairedDeviceArrayList;
private ArrayAdapter pairedDeviceAdapter;
public static BluetoothSocket clientSocket;
private ImageView btnImageBluetooth;
DialogFragment bltDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_device);
btnImageBluetooth = (ImageView) findViewById(R.id.btnImageBluetooth);
btnImageBluetooth.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyDialogFragment myDialogFragment = new MyDialogFragment();
FragmentManager manager = getSupportFragmentManager();
//myDialogFragment.show(manager, "dialog");
FragmentTransaction transaction = manager.beginTransaction();
myDialogFragment.show(transaction, "dialog");
if (permissionGranted()) {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothEnabled()) {
findBluetooth();
}
}
}
});
}
private boolean bluetoothEnabled() {
if (bluetoothAdapter.isEnabled()) {
return true;
} else {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 0);
return false;
}
}
private boolean permissionGranted() {
if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.BLUETOOTH)
== PermissionChecker.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.BLUETOOTH_ADMIN) == PermissionChecker.PERMISSION_GRANTED) {
return true;
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN}, 0);
return false;
}
}
public void findBluetooth() {
Set pairedDevice = bluetoothAdapter.getBondedDevices();
if (pairedDevice.size() > 0) {
pairedDeviceArrayList = new ArrayList<>();
for (BluetoothDevice device : pairedDevice) {
pairedDeviceArrayList.add(device.getAddress() + "/" + device.getName());
}
pairedDeviceAdapter = new ArrayAdapter(this,
R.layout.item_device, R.id.item_device_textView, pairedDeviceArrayList);
Dialog dialog = new Dialog(MainActivity.this);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
final String itemMAC = listView.getItemAtPosition(i).toString().split("/", 2)[0];
new Thread() {
@Override
public void run() {
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(itemMAC);
try {
BluetoothSocket socket = device.createRfcommSocketToServiceRecord
(device.getUuids()[0].getUuid());
socket.connect();
if (socket.isConnected()) {
Log.e("eee", "socket get");
clientSocket = socket;
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
});
}
}
}
public class MyDialogFragment extends DialogFragment {
ListView listView;
@SuppressLint("ResourceType")
public Dialog onCreateDialog(Bundle savedInstanceState) {
// MainActivity ;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// builder.setView(R.id.list_device);
builder.setTitle("Выберите ")
.setItems(R.layout.item_device, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}
}
Вопрос задан
более трёх лет назад
44 просмотра