TextView不更新 [英] TextView not updating
问题描述
使用处理程序类更新 TextView
中的文本时存在一个愚蠢的问题。我在Google地图中设置了自定义信息窗口。
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter(){
@Override
public View getInfoWindow(Marker标记){
返回null;
}
@覆盖
public View getInfoContents(最后标记标记){
//设置标记为最终** *
View v = getLayoutInflater()。inflate(R.layout.my_layout,null);
TextView name =(TextView)v.findViewById(R.id.name);
$ b (MyItem mi:myItems)
$ final TextView status =(TextView)v.findViewById(R.id.status);
$ b $ {if(mi.getName ).equals(marker.getTitle())){
name.setText(mi.getName()); //这个正常更新
Handler handler = new Handler ();
handler.postDelayed(new Runnable(){
@Override
public void run(){
//这个设置文本,但不会更新UI
status.setText(Text);
//在这里我打电话给我的更新文本函数
updateTv(status,Text);
//刷新窗口***
marker.showInfoWindow();
}
},5000);
休息;
}
}
return v;
}
});
}
TextView
名称正在更新,而状态 TextView
只更新其内容,并且不在屏幕上直观更新。我试过无效, runOnUiThread()
,但似乎没有任何工作。我知道解决方案可能非常明显,但我没有看到它。
感谢您提前给予帮助!
编辑
以下是我尝试runOnUiThread()的方法。
<$ c
$ b runOnUiThread(new Runnable(){
@Override
public void run(){$ b $ public void updateTv(final TextView tv,final String s){
runOnUiThread b
tv.setText(s);
tv.invalidate();
}
});
我的XML:
<?xml version =1.0encoding =utf-8?>
< LinearLayout xmlns:android =http://schemas.android.com/apk/res/android
android:orientation =vertical
android:layout_width =wrap_content
android:layout_height =wrap_content>
< TextView
android:layout_width =match_parent
android:layout_height =wrap_content
android:textAppearance =?android:attr / textAppearanceLarge
android:text =Large Text
android:id =@ + id / name
android:layout_gravity =center
android:gravity =center_horizontal
android:padding =10dp/>
< TextView
android:layout_width =match_parent
android:layout_height =wrap_content
android:textAppearance =?android:attr / textAppearanceSmall
android:text =...
android:id =@ + id / status
android:gravity =center_horizontal/>
< / LinearLayout>
解决方案
我通过刷新窗口在上面的代码中调用命令注释并用***签名。我感谢clownba0t
所以你试图从返回后更新视图, getInfoContents()
并且不能用作其实时视图,请参阅此。
有些东西可能正在调用:
status.invalidate();
I have a silly problem updating text in a TextView
using a handler class. I'm setting custom Info window in Google Maps.
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(final Marker marker) {
//setting marker as final***
View v = getLayoutInflater().inflate(R.layout.my_layout, null);
TextView name = (TextView) v.findViewById(R.id.name);
final TextView status = (TextView) v.findViewById(R.id.status);
for(MyItem mi : myItems){
if(mi.getName().equals(marker.getTitle())){
name.setText(mi.getName()); //This one updates normaly
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//this one sets the text, but doesnt update on UI
status.setText("Text");
//Here I call my update text function
updateTv(status, "Text");
//Refreshing the window***
marker.showInfoWindow();
}
}, 5000);
break;
}
}
return v;
}
});
}
TextView
"name" is updating normally, while "status" TextView
is only updating its content and not updating visually on the screen. I've tried invalidating, runOnUiThread()
, but nothing seems to work. I know the solution might be really obvious, but I don't see it.
Thank you for your help in advance!
EDIT
Here's how i tried runOnUiThread().
public void updateTv(final TextView tv, final String s){
runOnUiThread(new Runnable() {
@Override
public void run() {
tv.setText(s);
tv.invalidate();
}
});
}
My XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/name"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:padding="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="..."
android:id="@+id/status"
android:gravity="center_horizontal"/>
</LinearLayout>
SOLUTION
I refreshed my window by calling commands commented and signed with *** in the code above. My thanks goes to "clownba0t"
So you are trying to update the view after it returned from getInfoContents()
and that won't work as its not a live view, see this.
Something that might work is calling:
status.invalidate();
这篇关于TextView不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!