调用时未从 ArrayList 中提取正确的对象
我有一个程序,它以字节的形式获取数据流。然后它获取字节并用有效字节生成字符串。 如下。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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要调用 Adapter 实例的 notificationDataSetChanged() 。在处理程序中执行此操作
you need to call the notifyDataSetChanged() over to the instance of the Adapter. Do this in a Handler
这可能是您使用后自增运算符的问题。
这种情况正在发生:
您可能想要这样:
或者这样:
It could be a problem with your use of the post-increment operator.
This is happening:
You might want this:
Or this: