如何动态更新TextView文本

时间:2012-10-31 01:33:36

标签: android

在BroadcastReceiver内部我每次下载列表时都会跟踪,每次我想要最后一次更新文本视图。我已完成以下操作,但时间永远不会显示。

@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>

5 个答案:

答案 0 :(得分:10)

您已编写以下行以在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

答案 1 :(得分:1)

尝试以下(对我有用):

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        tv.setText("Blah: " + time);
    }
});

答案 2 :(得分:0)

在run()方法内创建一个新线程,编写代码以更新所考虑的字段,然后启动循环。请务必记住,您必须在UI线程上执行更新操作。

答案 3 :(得分:0)

使用View参数添加了OnClickListener并实现了onClick并强制转换为TextView

@Override
public void onClick(View v) {
    TextView textView = (TextView) v;
    textView.setText(crotLoan.getReducedNumber());
}

使用onClick的想法来自How do you update the screen after you change the text with a setText?

答案 4 :(得分:0)

UI元素在android中的工作方式是,如果您要访问UI元素,则必须在UI线程中进行操作,并且我们从API或Nonui线程获取与Internet相关的数据,因此在获得nonui线程的结果后,我们可以轻松更新UI线程元素。您肯定已经在logcat中看到UI线程中有太多工作,这与该东西有关,只是不要深入研究如果要在完成数据解析后以编程方式更新它,请调用ui线程并更新元素。