全局变量值未及时更新

时间:2014-06-16 18:29:58

标签: java android variables

所以我在android中使用xml

设置了onclicklistener的textview
<TextView
    android:id="@+id/usernameView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:onClick="userNameClicked"
    android:clickable="true"/>

; userNameClicked&#34;方法我有

String usernamePointsDialog = "ITSSTILLNULL"; // this is the global variable that doesn't update until clicked a second time

    public void userNameClicked(View v) {

    global_position = postsView.getPositionForView(v);


    Log.i(TAG, "value" + global_position);

    int global_position2 = global_position - 1;

    String userObjectId2 = posts.getItem(global_position2).getUserString();

    Log.i(TAG, "value" + userObjectId2);

    // code that queries the database here
    {
    String userPointsFromDatabase = String.valueOf(userPoints2.getInt("userPoints"));
    // this is the code that returns the correct value but doesn't update the GV in    time

    Log.i(TAG, "value" + userPointsFromDatabase);

    usernamePointsDialog = userPointsFromDatabase;

    Log.i(TAG, "value" + userPointsFromDatabase);

    Log.i(TAG, "value1" + usernamePointsDialog);

    }

   }

});
        createDialog2().show();

   // this creates a dialog box that shows username (which is always the correct value, and the second value which always lags behind by one).

}

所以我可以点击的用户名显示在列表视图中,除了全局变量usernamePointsDialog的值落后于1之外,代码的所有内容都能正常工作,所以如果我点击一个用户名,会弹出正确的用户名,但是对数据库的查询显示&#34; ITSSTILLNULL&#34;直到它第二次被点击,当它显示正确的值时。所有日志都显示正在从DB查询正确的值,只是全局变量未及时更新,以便createialog2方法将(usernamepointsdialog)全局变量显示为正确的值。 / p>

由于各种原因,我无法使用最终标记/本地变量,我不知道如何解决此问题。您提供的任何帮助都非常感谢!

1 个答案:

答案 0 :(得分:0)

解决了这个问题,由于某种原因,在方法中的代码运行之前调用了createialog2()方法。我只是将createdialog2方法调用移到了最后一个日志语句的正下方,它就像一个魅力。