英文:
How to send a "ListView" value to a "RecycleView" and save them. I'm using Android Studio using JAVA?
问题
这是我在“第二个活动”中处理列表的方式。
private void setList() {
ArrayList<BluetoothLE> aBleAvailable = new ArrayList<>();
if (ble.getListDevices().size() > 0) {
for (int i = 0; i < ble.getListDevices().size(); i++) {
aBleAvailable.add(new BluetoothLE(
ble.getListDevices().get(i).getName(),
ble.getListDevices().get(i).getMacAddress(),
ble.getListDevices().get(i).getRssi(),
ble.getListDevices().get(i).getDevice()));
}
BasicList mAdapter = new BasicList(this, R.layout.simple_row_list, aBleAvailable) {
@Override
public void onItem(Object item, View view, int position) {
TextView txtName = view.findViewById(R.id.txtText);
String aux = ((BluetoothLE) item).getName() + " " + ((BluetoothLE) item).getMacAddress();
txtName.setText(aux);
}
};
listBle.setAdapter(mAdapter);
listBle.setOnItemClickListener((parent, view, position, id) -> {
BluetoothLE itemValue = (BluetoothLE) listBle.getItemAtPosition(position);
ble.connect(itemValue.getDevice(), bleCallbacks());
});
} else {
dAlert = setDialogInfo("Ups", "We do not find active devices", true);
dAlert.show();
finish();
}
}
我想将点击的值移动到“MainActivity”中的一个RecyclerView中。谢谢。
英文:
This is how I'm doing the list in the "second activity".
private void setList(){
ArrayList<BluetoothLE> aBleAvailable = new ArrayList<>();
if(ble.getListDevices().size() > 0){
for (int i=0; i<ble.getListDevices().size(); i++) {
aBleAvailable.add(new BluetoothLE(ble.getListDevices().get(i).getName(), ble.getListDevices().get(i).getMacAddress(), ble.getListDevices().get(i).getRssi(), ble.getListDevices().get(i).getDevice()));
}
BasicList mAdapter = new BasicList(this, R.layout.simple_row_list, aBleAvailable) {
@Override
public void onItem(Object item, View view, int position) {
TextView txtName = view.findViewById(R.id.txtText);
String aux = ((BluetoothLE) item).getName() + " " + ((BluetoothLE) item).getMacAddress();
txtName.setText(aux);
}
};
listBle.setAdapter(mAdapter);
listBle.setOnItemClickListener((parent, view, position, id) -> {
BluetoothLE itemValue = (BluetoothLE) listBle.getItemAtPosition(position);
ble.connect(itemValue.getDevice(), bleCallbacks());
});
}else{
dAlert = setDialogInfo("Ups", "We do not find active devices", true);
dAlert.show();
finish();
}
}
I want to move the clicked values to create a RecicleView present in "MainActivity".
Thx.
答案1
得分: 1
我认为您正在尝试在扫描活动中列出蓝牙设备,然后当用户从“ListView”中选择一个设备时,您想将其发送回到MainActivity。
如果是这种情况,您需要修改MainActivity,不要使用startActivity()
方法来启动SecondActivity,而是要使用startActivityForResult()
方法,在用户选择连接设备时获取返回结果。
MainActivity.java
int BLUETOOTH_DEVICE_REQUEST = 1;
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, BLUETOOTH_DEVICE_REQUEST);
然后在您的SecondActivity
中,有两种情况,用户要么选择一个设备,要么不选择。所以,如果他选择了一个设备,您可以这样发送回结果。
SecondActivity.java
Intent intent = new Intent();
intent.putExtra("result", result);
setResult(Activity.RESULT_OK, intent);
finish();
如果您需要中止操作并什么都不返回,可以这样做
SecondActivity.java
Intent intent = new Intent();
setResult(Activity.RESULT_CANCELED, intent);
finish();
之后,您可以使用onActivityResult()
回调在您的MainActivity
中观察结果
MainActivity.java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == BLUETOOTH_DEVICE_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
String result = data.getStringExtra("result");
// 根据需要使用结果
}
if (resultCode == Activity.RESULT_CANCELED) {
// 如果没有结果,编写您的代码
}
}
}
英文:
I think you are trying to list the Bluetooth devices in a scanning activity and then when the user selects one device from the ListView
you want to send it back to the MainActivity.
If this is the case so you need to modify the MainActivity
and instead of launching the SecondActivity
using the startActivity()
method, you have to use startActivityForResult()
to get back the result when the user selects his device for connection
MainActivity.java
int BLUETOOTH_DEVICE_REQUEST = 1
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, BLUETOOTH_DEVICE_REQUEST);
Then in your SecondActivity
we have two cases, either the user will select a device or not. So, in case he selected a device, you can send it back like this.
SecondActivity.java
Intent intent= new Intent();
intent.putExtra("result",result);
setResult(Activity.RESULT_OK,intent);
finish();
If you need to abort the operation and send back nothing you can do it this way
SecondActivity.java
Intent intent = new Intent();
setResult(Activity.RESULT_CANCELED, intent);
finish();
After that, you can observe the result in your MainActivity
using the onActivityResult()
callback
MainActivity.java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == BLUETOOTH_DEVICE_REQUEST ) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
// Use it as you like
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论