2015-02-06 88 views
0

我正在編寫一個類似收件箱的活動的代碼,其中有一個導致消息的按鈕。此按鈕有一個文本字段,用於統計收件箱中的郵件數量。Android按鈕setText未更新

我的問題是,當消息的數量發生變化時,按鈕的文本字段不會改變。這不是應用程序沒有檢查更新的問題,並且帶有setText的代碼被調用並更新了正確的編號。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Log.v("onCreate", "Main"); 
    // Checking if there is login 
    if (ParseUser.getCurrentUser() == null) { 
     navigateToLogin(); 
    } else { 

    // Setting pointers for buttons. 
    // onClick methods follow. 
    askButton = (Button) findViewById(R.id.buttonAsk); 
    ansButton = (Button) findViewById(R.id.buttonAnswer); 
    inboxButton = (Button) findViewById(R.id.buttonCenter); 

    mUser = ParseUser.getCurrentUser(); 
    updateInbox(); 

} 

這是檢查新消息並更新按鈕的方法。

private void updateInbox() { 
    Log.v(TAG, "Updating inbox"); 

    ParseQuery responses = new ParseQuery(ParseConstants.CLASS_ANSWER); 
    responses.whereMatches(ParseConstants.KEY_SENDER_ID, mUser.getObjectId()); 

    try { 
     responsesCount = responses.count(); 
     Log.v("responses count" ,""+responsesCount); 
     if (responsesCount > 0) { 
      inboxButton.setText(String.valueOf(responsesCount)); 
     } 
     Log.v("InboxActivity","Label set to " + responsesCount); 
    } catch (ParseException e) { 
     Log.v("InboxActivity", e.getMessage()); 
    }   
} 

updateInbox被正確調用,並在正確的時刻,所以我只添加了它的代碼,使其儘可能乾淨。下面是XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#0099cc" 
tools:context=".MainActivity" 
android:clickable="false" 
> 

<Button 
    android:layout_width="80sp" 
    android:layout_height="80sp" 
    android:id="@+id/buttonCenter" 
    android:layout_gravity="center" 
    android:layout_centerInParent="true" 
    android:textSize="30sp" 
    android:text="" 
    android:textColor="@color/black_overlay" 
    android:background="@drawable/greenbutton"/> 

編輯:

大家好,感謝您的幫助。我找出了問題並將其作爲答案發布。這是一個邏輯錯誤,與Android無關。

回答

0

試試這個:

inboxButton.post(new Runnable(){ 
     @Override 
     public void run(){ 
      inboxButton.setText(String.valueOf(responsesCount)); 
     } 
}); 

(responsesCount應該是最後的)

0

試試下面的代碼

import android.widget.RemoteViews; 

if (responsesCount > 0) { 
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.my_layout); //where my_layout is the layout file where the button resides. 
remoteViews.setTextViewText(R.id.button, "Set button text here");// where button is id of the button. 
} 

findViewById不存在一個小部件。

0

下面的代碼會爲你工作

runOnUiThread(new Runnable() { 
       @Override 
        public void run() { 
         if (responsesCount > 0) { 
          inboxButton.setText(String.valueOf(responsesCount)); 
            } 
      } 
     }); 

您可以使用的AsyncTask的應用流量的平滑度。

0

我意識到文本只在響應數大於0時纔會更新。按鈕正確更新,否則我必須放一個else語句以確保按鈕始終得到更新。我在評論中用正確的答案編輯了問題。

responsesCount = responses.count(); 
    Log.v("responses count" ,""+responsesCount); 
    if (responsesCount > 0) { 
     inboxButton.setText(String.valueOf(responsesCount)); 

    /////////////////////////////////// 
    // MY EDIT HERE 
    //} else{ 
    // inboxButton.setText(""); 
    ///////////////////////////////////