3
在BroadcastReceiver內部我跟蹤每次下載列表時每次都是我想用上次更新textview。我做了以下,但時間永遠不會顯示。如何動態更新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>
對不起,應該提到,我已經嘗試過,並沒有喜悅。 – da3m0n
哦,我還沒有看到,你是動態添加視圖,讓我更新我的答案,現在檢查它,你需要調用.invalidate()方法刷新 – Lucifer
添加了xml,希望正確:) – da3m0n