调用时未从 ArrayList 中提取正确的对象

发布于 2024-10-26 18:48:02 字数 1440 浏览 3 评论 0原文

我有一个程序,它以字节的形式获取数据流。然后它获取字节并用有效字节生成字符串。 如下。messages 是一个 ArrayList,position 是索引,readMessage 是一个字符串对象。

 case MESSAGE_READ:
            byte[] readBuf = (byte[]) msg.obj;
            // construct a string from the valid bytes in the buffer
            String readMessage = new String(readBuf, 0, msg.arg1);
           // rpmView.setText(readMessage);
            messages.add(readMessage);

            int position;       
            position = 0; //position < messages.size();){
            int position1;
            int position2;
            int position3;
            int position4;
            int position5;
                  rpmView.setText(messages.get(position));
                  position1 = position++;
                  tempView.setText(messages.get(position1));
                  position2 = position1++;
                  mphView.setText(messages.get(position2));
                  position3 = position2++;
                  gearView.setText(messages.get(position3));
                  position4 = position3++;
                  batteryVoltageView.setText(messages.get(position4));
                  position5 = position4++;
                  fuelLevelView.setText(messages.get(position5));

然后将这些字符串放入 arraylist 中,并使用 arraylist 类的 view.setText(arraylistname.get(int index) 进行调用。代码 问题是当读取流时,所有字段/视图显示相同的信息并且不更新,请帮助我做了很多关于列表视图和数组适配器的研究,但我不需要垂直滚动列表,我想要文本视图。随着数据的变化而更新,但布局保持不变,除了值发生变化。

I have a program that takes a stream of data in in the form of bytes. It then takes the bytes and makes strings out of the valid bytes. Then those strings are placed in an arraylist and called with view.setText(arraylistname.get(int index) of the arraylist class. Here is the code. messages is an ArrayList, position is the index, and readMessage is a string object.

 case MESSAGE_READ:
            byte[] readBuf = (byte[]) msg.obj;
            // construct a string from the valid bytes in the buffer
            String readMessage = new String(readBuf, 0, msg.arg1);
           // rpmView.setText(readMessage);
            messages.add(readMessage);

            int position;       
            position = 0; //position < messages.size();){
            int position1;
            int position2;
            int position3;
            int position4;
            int position5;
                  rpmView.setText(messages.get(position));
                  position1 = position++;
                  tempView.setText(messages.get(position1));
                  position2 = position1++;
                  mphView.setText(messages.get(position2));
                  position3 = position2++;
                  gearView.setText(messages.get(position3));
                  position4 = position3++;
                  batteryVoltageView.setText(messages.get(position4));
                  position5 = position4++;
                  fuelLevelView.setText(messages.get(position5));

The problem is when the stream is read, all fields/views display the same info and don't update. Please help. I did a lot of research about listviews and arrayadapters, but i don't want a vertical scrolling list, I want textviews that update as the data changes, but the layout stays the same with the exception of the values changing.

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

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

发布评论

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

评论(2

梦旅人picnic 2024-11-02 18:48:03

您需要调用 Adapter 实例的 notificationDataSetChanged() 。在处理程序中执行此操作

you need to call the notifyDataSetChanged() over to the instance of the Adapter. Do this in a Handler

蹲在坟头点根烟 2024-11-02 18:48:03

这可能是您使用后自增运算符的问题。

这种情况正在发生:

// Using the post-increment operator.    
int position = 0;
int position1, position2, position3;
position1 = position++; // position1 = 0, position = 1
position2 = position1++; // position2 = 0, position1 = 1
position3 = position2++; // position3 = 0, position2 = 1

您可能想要这样:

// Using addition.    
int position = 0;
int position1, position2, position3;
position1 = position + 1; // position1 = 1, position = 0
position2 = position1 + 1; // position2 = 2, position1 = 1
position3 = position2 + 1; // position3 = 3, position2 = 2

或者这样:

// Using the pre-increment operator.    
int position = 0;
int position1, position2, position3;
position1 = ++position; // position1 = 1, position = 1
position2 = ++position1; // position2 = 2, position1 = 2
position3 = ++position2; // position3 = 3, position2 = 3

It could be a problem with your use of the post-increment operator.

This is happening:

// Using the post-increment operator.    
int position = 0;
int position1, position2, position3;
position1 = position++; // position1 = 0, position = 1
position2 = position1++; // position2 = 0, position1 = 1
position3 = position2++; // position3 = 0, position2 = 1

You might want this:

// Using addition.    
int position = 0;
int position1, position2, position3;
position1 = position + 1; // position1 = 1, position = 0
position2 = position1 + 1; // position2 = 2, position1 = 1
position3 = position2 + 1; // position3 = 3, position2 = 2

Or this:

// Using the pre-increment operator.    
int position = 0;
int position1, position2, position3;
position1 = ++position; // position1 = 1, position = 1
position2 = ++position1; // position2 = 2, position1 = 2
position3 = ++position2; // position3 = 3, position2 = 3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文