我是 Java 和 Android 新手,目前正在尝试更新 Fragment
之外的 TextView
。
// Update Location after processing location info
Activity activity = (Activity) mContext;
TextView textView = (TextView) activity.findViewById(R.id.location) ;
String updatedLocation = cityName+" - "+cityLatitude + " " + cityLongitude;
textView.setText(updatedLocation);
但是这不起作用,并且 TextView
不会更新。更新 onCreateView()
内的 textview
工作正常,但我试图在每次对数据库进行查询时动态更改内容。
我尝试在所述fragment
中创建一个方法,但没有成功,因为该方法必须是static
和getActivity()
不是一个静态
方法。
还有其他解决方法吗?
最佳答案
在你的 fragment 中创建这个函数
public void updatecount(int counter){
count = counter;
textView.setText(String.valueOf(count));
}
并通过启动 fragment 的对象从不同的 Activity 中调用它
LeftSliderFragment fragment = (LeftSliderFragment) getSupportFragmentManager()
.findFragmentById(R.id.menu_frame);
fragment.updatecount(1);
关于java - 从 Fragment 外部更新 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25133695/