我有一个愚蠢的问题,使用处理程序类更新TextView
中的文本。我在Google地图中设置了自定义信息窗口。TextView不更新
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
“名”正常更新,而“状态” TextView
只更新其内容,而不是在屏幕上直观地更新。我试过无效,runOnUiThread()
,但似乎没有任何工作。我知道解决方案可能非常明显,但我没有看到它。
非常感谢您的帮助!
编辑
以下是我试过runOnUiThread()。
public void updateTv(final TextView tv, final String s){
runOnUiThread(new Runnable() {
@Override
public void run() {
tv.setText(s);
tv.invalidate();
}
});
}
我的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
我通过调用上面的代码注释,并与***签署命令刷新我的窗口。我要感谢“clownba0t”
你应该在主线程中更新UI元素。显示你说你尝试runOnUiThread()时使用的代码。 – HelloSadness
你介意发布'R.layout.my_layout'吗? – clownba0t
你的布局xml?由于名称textView正在工作,它可能与更新机制无关,所以映射可能有问题! –