サーバーからメッセージを受信するバックグラウンド サービスがあり、それらのメッセージを使用して、ListView に表示されるオブジェクトの内部プロパティを更新します。
私は常に runOnUiThread メソッドを使用して listArrayAdapter.notifyOnDataSetChanged() コマンドを実行します。
何らかの理由で、ListView が更新され、プロパティの更新が表示される場合と表示されない場合があります。
テストのために、ListView に「更新」ボタンを追加しました。ボタンを押すと、listArrayAdapter.notifyOnDataSetChanged() が実行されます。
ボタンをクリックするたびに、ビューが完全に更新されます..
サービスから更新しようとすると、常に機能するとは限らない理由がよくわかりませんが、UIThreadで常に実行されるとは限らないと思います...
私は本当に絶望的で、喜んで助けを求めます..
マイコード
ServerConnectionManager.java - サービスを拡張します
//example of a command executed when a specific message received from the server:
//app is the Application variable
public void unFriend(int userId)
{
serverResponseManager.onUnFriend(app.getApplicationFriend(userId),false);
}
ServerResponseManager.java - サーバー メッセージに対するすべてのアプリケーション応答を処理するクラス:
public void onUnFriend(FacebookUser facebookUser, boolean isYouRemovedClient) {
//this is the property which will effect the ListView view when calling the
//arrayListAdataper.notifyOnDataSetChanged();
facebookUser.setApplicationFriend(false);
app.getApplicationFriends().remove(facebookUser);
app.getDatabaseManager().deleteApplicationFriend(facebookUser.getId());
//if the application is currently running in the UI (not on the background) it will run a method inside the BaseActivity
if (app.isApplicationInForeground())
{
app.getCurrentActivity().onUnFriend(facebookUser);
if (isYouRemovedClient)
app.showToast(facebookUser.getName() + " has removed from your friends", true);
else
app.showToast(facebookUser.getName() + " has removed you from friends", true);
}
}
BaseActivity.java - それを拡張するすべてのアクティビティのすべてのデフォルト構成を設定するアクティビティ
//in this exemple the BaseActivity method does nothing but the ListViewActivity.java method override it
public void onUnFriend(FacebookUser facebookUser)
{
}
ListViewActivity.java - BaseActivity を拡張し、ServerResponseManager の public void onUnFriend(FacebookUser facebookUser, boolean isYouRemovedClient) で行われた FacebookUser オブジェクト プロパティの変更を反映する必要がある ListView をその中に持ちます。
@Override
public void onUnFriend(FacebookUser facebookUser)
{
updateView();
}
private void updateView()
{
runOnUiThread(updateViewRunnable());
}
private Runnable updateViewRunnable()
{
Runnable run = new Runnable() {
@Override
public void run() {
listArrayAdapter.notifyDataSetChanged();
}
};
return run;
}