如何将 "ListView" 值发送到 "RecycleView" 并保存它们。我正在使用 Android Studio 使用 JAVA?
How to send a "ListView" value to a "RecycleView" and save them. I'm using Android Studio using JAVA?
这就是我在“第二个 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();
}
}
我想移动单击的值以在“MainActivity”中创建一个 RecicleView。
谢谢
我认为您正在尝试列出扫描 activity 中的蓝牙设备,然后当用户 select 的 ListView
中的一台设备您想将其发送回MainActivity.
如果是这种情况,那么您需要修改 MainActivity
而不是使用 startActivity()
方法启动 SecondActivity
,您必须使用 startActivityForResult()
来当用户select连接他的设备
时返回结果
MainActivity.java
int BLUETOOTH_DEVICE_REQUEST = 1
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, BLUETOOTH_DEVICE_REQUEST);
然后在您的 SecondActivity
中我们有两种情况,用户将 select 设备与否。因此,如果他 select 编辑了设备,您可以像这样将其发回。
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");
// Use it as you like
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
这就是我在“第二个 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();
}
}
我想移动单击的值以在“MainActivity”中创建一个 RecicleView。 谢谢
我认为您正在尝试列出扫描 activity 中的蓝牙设备,然后当用户 select 的 ListView
中的一台设备您想将其发送回MainActivity.
如果是这种情况,那么您需要修改 MainActivity
而不是使用 startActivity()
方法启动 SecondActivity
,您必须使用 startActivityForResult()
来当用户select连接他的设备
MainActivity.java
int BLUETOOTH_DEVICE_REQUEST = 1
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, BLUETOOTH_DEVICE_REQUEST);
然后在您的 SecondActivity
中我们有两种情况,用户将 select 设备与否。因此,如果他 select 编辑了设备,您可以像这样将其发回。
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");
// Use it as you like
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}