Android Button setText未更新

时间:2015-02-06 16:00:27

标签: android android-button

我正在为类似收件箱的活动编写代码,该活动有一个导致邮件的按钮。此按钮具有一个文本字段,用于计算收件箱中的邮件数量。

我的问题是,当消息数量发生变化时,按钮的文本字段不会改变。应用程序不检查更新不是问题,并且使用正确的数字调用带有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无关。

4 个答案:

答案 0 :(得分:0)

试试这个:

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

(respondCount应该是最终的)

答案 1 :(得分: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。

答案 2 :(得分:0)

以下代码适用于您

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

您可以使用AsyncTask来平滑应用流程。

答案 3 :(得分:0)

我意识到只有当回复数量为&gt;时才会更新文字。 0.该按钮正确更新,否则我必须发出一个else语句,以确保按钮始终更新。我在评论中用正确的答案编辑了这个问题。

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

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