简体   繁体   中英

How to send a "ListView" value to a "RecycleView" and save them. I'm using Android Studio using JAVA?

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.

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
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM