如何通过BLE设备在当前活动中发送的数据流来继续更新下一个活动中的Recyclerview?

发布于 2025-02-01 10:04:06 字数 2525 浏览 3 评论 0原文

我是Android的新手,并且刚刚学到了面向对象的编程。我的项目要求我在开源代码上构建一些东西。我真的很挣扎着这个特殊情况。两个片段在Activity_Main下,其中一个是终端frag fragment。如果terminalfragment在Activity_Main下活跃,则来自BLE设备的数据将不断流入Activity_Main。是否可以在不单击按钮的情况下将数据传递到下一个活动(在这种情况下为menuitem(r.id.plot))?我在Activity_Main2上添加了一个recyclerview,并希望它显示在Activity_Main中流动的数据。

在我当前方法的测试中,回收器视图不会自行更新,它只是在用户单击menuitem(id,plot)后,在Activity_Main的回收范围内捕获数据。我需要在方法中添加什么样的东西?我应该创建另一个片段而不是activy_main2吗?当我在互联网上查找这个问题时,似乎不可能在片段和下一个活动之间使用这种方式工作。非常感谢。

终端fragment of termaCtivity的终端

if (id == R.id.plot){
            Intent intent = new Intent(getActivity(), MainActivity2.class);
            intent.putExtra("output",output); //output
            startActivity(intent);
            return true;
        }

fragment()

private void receive(byte[] data) {
        if(hexEnabled) {
            receiveText.append("Hello" + TextUtil.toHexString(data) + '\n');
        } else {
            String msg = new String(data);
            if(newline.equals(TextUtil.newline_crlf) && msg.length() > 0) {
                // don't show CR as ^M if directly before LF
                msg = msg.replace(TextUtil.newline_crlf, TextUtil.newline_lf);
                // special handling if CR and LF come in separate fragments
                if (pendingNewline && msg.charAt(0) == '\n') {
                    Editable edt = receiveText.getEditableText();
                    if (edt != null && edt.length() > 1)
                        edt.replace(edt.length() - 2, edt.length(), "");
                }
                pendingNewline = msg.charAt(msg.length() - 1) == '\r';
            }
            receiveText.append(TextUtil.toCaretString(msg, newline.length() != 0)); //print out data
            output = receiveText.getText().toString(); // CharSequence to String
        }
    }

mainActivity 2, *receedata()是将数据添加到recyclerview

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Toolbar toolbar = findViewById(R.id.toolbar2);
        setSupportActionBar(toolbar);
        recyclerView = findViewById(R.id.dataflow);
        dataList = new ArrayList<>();
        String data;
        Bundle extras = getIntent().getExtras();
        if (extras != null)
        {
            data = extras.getString("output");
            receiveData(data);
        }
        setAdapter();
    }

I am completely new to Android and just learned Object-oriented programming. My project requires me to build something on open-source code. I am really struggling with this special case. Two fragments are under activity_main, one of them is TerminalFragment. If TerminalFragment is active under activity_main, data from a BLE device will keep flowing into activity_main. Is it possible to keep passing the data to the next activity without clicking a button (in this case, menuItem(R.id.plot))? I added a recyclerview on activity_main2 and want it display the data which is flowing in activity_main.

In my testing with my current method, the recycler view won't update itself, it just captures the data in the recyclerview of activity_main with TerminalFragment once the user clicked the menuItem (id,plot). What kind of thing I need to add in my method? Should I create another Fragment instead of activity_main2? As I looked this up in the internet, it seems not possible to work that way between a fragment and next activity. Much Appreciated.

onOptionsItemSelected of TerminalFragment

if (id == R.id.plot){
            Intent intent = new Intent(getActivity(), MainActivity2.class);
            intent.putExtra("output",output); //output
            startActivity(intent);
            return true;
        }

receive() of TerminalFragment

private void receive(byte[] data) {
        if(hexEnabled) {
            receiveText.append("Hello" + TextUtil.toHexString(data) + '\n');
        } else {
            String msg = new String(data);
            if(newline.equals(TextUtil.newline_crlf) && msg.length() > 0) {
                // don't show CR as ^M if directly before LF
                msg = msg.replace(TextUtil.newline_crlf, TextUtil.newline_lf);
                // special handling if CR and LF come in separate fragments
                if (pendingNewline && msg.charAt(0) == '\n') {
                    Editable edt = receiveText.getEditableText();
                    if (edt != null && edt.length() > 1)
                        edt.replace(edt.length() - 2, edt.length(), "");
                }
                pendingNewline = msg.charAt(msg.length() - 1) == '\r';
            }
            receiveText.append(TextUtil.toCaretString(msg, newline.length() != 0)); //print out data
            output = receiveText.getText().toString(); // CharSequence to String
        }
    }

OnCreate of mainActivity2, *receiveData() is to add data to the recyclerview

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Toolbar toolbar = findViewById(R.id.toolbar2);
        setSupportActionBar(toolbar);
        recyclerView = findViewById(R.id.dataflow);
        dataList = new ArrayList<>();
        String data;
        Bundle extras = getIntent().getExtras();
        if (extras != null)
        {
            data = extras.getString("output");
            receiveData(data);
        }
        setAdapter();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

姜生凉生 2025-02-08 10:04:07

我认为您的问题是未知的,如何动态地将Activity_Main数据转到Activity_Main2上,您可以用户“ EventBus”来动态求解数据更新,无论您使用其他片段或Attivation_Main2。

i think your problems is unknown how to get activity_main data onto activity_main2 dynamically , You can user " EventBus " to solve data updates dynamically , whatever you use another fragment or activity_main2 .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文