在 BroadcastReceiver 内部,我跟踪每次下载列表以及每次我想用上次更新 textview 的时间。我已完成以下操作,但从未显示时间。
@Override
public void onReceive(Context context, Intent intent) {
_context = context;
Toast.makeText(context, "ListDownloadReceiver....", Toast.LENGTH_SHORT)
.show();
boolean force = intent.getBooleanExtra("FORCE", false);
long nextUpdateTime = getUpdateTime();
if (force || nextUpdateTime < System.currentTimeMillis()) {
if (isNetworkAvailable()) {
Log.i(LIST_DOWNLOAD_RECEIVER,
"Going to be downloading the list...");
DownloadListData downloadList = new DownloadListData(context);
downloadList.execute();
} else {
Log.i(LIST_DOWNLOAD_RECEIVER, "Not connected...");
context.registerReceiver(this, new IntentFilter(
ConnectivityManager.CONNECTIVITY_ACTION));
}
}
DatabaseHandler db = DatabaseHandler.getInstance(context);
String timeFormat = "d/M/yyyy 'at' k:mm:ss";
long lastUpdate = Long.valueOf(db.getPreference("LAST_UPDATED_TIME",
"0"));
CharSequence time = DateFormat.format(timeFormat, lastUpdate);
Log.i(LIST_DOWNLOAD_RECEIVER, "Time is: " + time);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.activity_main, null);
TextView tv = (TextView) view.findViewById(R.id.lastRefreshed);
tv.append("Blah: " + time);
}
这是我的 .xml 文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left|top"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/noShowsLayout"
android:layout_width="match_parent"
android:layout_height="115dp"
android:orientation="horizontal"
android:background="@color/blue" >
<TextView
android:id="@+id/noShowsTextView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="@string/no_added_shows"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="18dip" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_gravity="bottom"
android:background="@color/blue"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/refreshButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:contentDescription="@string/refresh"
android:src="@drawable/refresh"/>
<TextView
android:id="@+id/lastRefreshed"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:text="@string/lastRefreshTime"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ImageButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:contentDescription="@string/remove_tv_show"
android:src="@drawable/content_discard"
android:clickable="false"/>
</LinearLayout>
</LinearLayout>
最佳答案
您已经编写了以下行以在 TextView 中显示文本,
TextView tv = (TextView) view.findViewById(R.id.lastRefreshed);
tv.append("Blah: " + time);
我建议你使用 .setText() 方法而不是 .append() 方法,如下所示,
TextView tv = (TextView) view.findViewById(R.id.lastRefreshed);
tv.setText("Blah: " + time);
view.invalidate(); // for refreshment
关于android - 如何动态更新TextView文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13150073/