Java 在TextView中,打印出错
我的问题是:Java 在TextView中,打印出错,java,android,android-intent,textview,Java,Android,Android Intent,Textview,我的问题是:onReceive方法从Intent收集字符串消息。我将此消息放入ArrayList并在actualTextView中打印。然后我将我的ArrayList转换为String[]。最后,我使用append将String[]historyaa中的字符串打印到historyTextView中为了理解,我提供了代码片段: public class NotificationCenter extends BroadcastReceiver { @Override public void onRe
onReceive
方法从Intent
收集字符串消息。我将此消息放入ArrayList
并在actual
TextView中打印。然后我将我的ArrayList
转换为String[]
。最后,我使用append
将String[]historyaa
中的字符串打印到history
TextView中为了理解,我提供了代码片段:
public class NotificationCenter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(EXTRA_MESSAGE)) {
int counter = 0;
ArrayList<String> historiaAL = new ArrayList<String> ();
historiaAL.add(counter,intent.getExtras().getString("Message"));
TextView actual = (TextView)findViewById(R.id.received_string);
actual.setText(historiaAL.get(counter));
TextView history = (TextView)findViewById(R.id.history_string);
String[] historiaA = historiaAL.toArray(new String[historiaAL.size()]);
history.append(historiaA[counter]);
history.append("\n");
counter++;
}
}
}
Hello
World
!!!
我真正想要实现的是:
public class NotificationCenter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(EXTRA_MESSAGE)) {
int counter = 0;
ArrayList<String> historiaAL = new ArrayList<String> ();
historiaAL.add(counter,intent.getExtras().getString("Message"));
TextView actual = (TextView)findViewById(R.id.received_string);
actual.setText(historiaAL.get(counter));
TextView history = (TextView)findViewById(R.id.history_string);
String[] historiaA = historiaAL.toArray(new String[historiaAL.size()]);
history.append(historiaA[counter]);
history.append("\n");
counter++;
}
}
}
Hello
World
!!!
我做错了什么?我把你写在纸上,它会给我提供我想要的结果
编辑:问题已解决。结果是,每次点击按钮,我都在创建新的
mNotificationCenter=newnotificationcenter()每次导致故障的代码>。
每次调用onReceive()时,您都在初始化一个值为0的计数器。
因此,使用行historaal.add(counter,intent.getExtras().getString(“Message”))
可以将新字符串添加到ArrayList中的位置0。在方法的末尾,您可以递增计数器,但
计数器
只是一个局部变量。当您再次调用onReceive()时,计数器将再次被新创建并每次设置为0
下面的代码按照您的意图处理消息,没有不必要的计数器等。
如果输出中仍然出现不需要的重复单词,那么这是因为您多次发送消息,或者多次接收一条消息
public class NotificationCenter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(EXTRA_MESSAGE)) {
String receivedString = intent.getExtras().getString("Message");
TextView actual = (TextView)findViewById(R.id.received_string);
actual.setText(receivedString);
TextView history = (TextView)findViewById(R.id.history_string);
String historiaA = (String) history.getText() + "\n" + receivedString;
history.setText(historiaA);
}
}
}
当然听起来很合理。不管怎样,没有使用计数器
我仍然得到了我得到的东西。即使我使用historaal.add(intent.getExtras().getString(“Message”))
它也不会改变任何事情。您能显示调用onReceive()的代码吗?我不认为是方法本身导致了这个输出。我更新了整个类。我创建mNotificationCenter=newnotificationcenter()
在一个fragments类中:公共类ReceiveSectionFragment扩展Fragment
好的,它是一个BroadcastReceiver。使用断点运行调试,在断点处设置额外的“消息”。检查每次通过时此处设置的值。它是否为后面的字符串多次发送意图?public final static String EXTRA\u MESSAGE=“com.receive.intent.MESSAGE”代码>它只是一个常量,因此onReceive
可以运行分配给所需对象的代码。还有,我不知道你的确切意思。。。