我在我的应用程序中创建了一个布局,其中有多个复选框,一旦用户选中一些并单击"确定",我就会通过jsonHttpResponse
进行搜索(每个迭代都是选中的项目(:
for (int i = 0; i < items.size(); i++) {
String itemID = items.get( i ).getitemID();
MyitemClient client = new MyitemClient();
client.getitems( itemID, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
if (response != null) {
final Myitem items = Myitem.fromJson( response );
itemNames = itemNames + items.getTitle() + "n";
}
}
@Override
public void onFinish() {
String TimeFormat = "dd.MM.yyyy";
SimpleDateFormat df2 = new SimpleDateFormat( TimeFormat );
String dateText = df2.format( dateInMillis );
db = FirebaseFirestore.getInstance();
CollectionReference usernamedb = db.collection( "Users" ).document( auth.getUid() ).collection( "UserData" );
usernamedb.get().addOnCompleteListener( task -> {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
String NewMessage = document.getString( "username" ) + " wants these items: nn" + itemNames + "nUntil " + dateText + "nnDo you confirm?";
chatOrganizer.addMessageToChat( chatID, auth.getUid(), receiverID, NewMessage, true );
}
}
} );
}
} );
}
客户端为:
public class MyitemClient {
private static final String API_BASE_URL = "https://www.somesiteinhere..../";
private AsyncHttpClient client;
public MyitemClient() {
this.client = new AsyncHttpClient();
}
public void getitems(final String query, JsonHttpResponseHandler handler) {
try {
client.get(API_BASE_URL + URLEncoder.encode(query, "utf-8"), handler);
} catch (UnsupportedEncodingException ignored) {
}
}
}
我的目标是从该客户端获得itemTitle,一旦获得所有标题,就启动一条消息,如chatOrganizer
所示。
我的问题是,在这个实现中,它进入onFinish
的次数与迭代的次数一样多,因此它为每个项目打印这个消息。
我如何确保只有在所有迭代完成后,它才会进入onFinish?
谢谢
我认为一个解决方案可以是:
- 您编写了两个处理程序,一个使用dummy-onFinish(该方法什么都不做(,另一个使用您当前的实现onFinish
- 检查i的索引。如果i不是最后一个项,则将伪处理程序作为第二个参数传递给client.getitems。如果i是最后一项,则将另一项传递给client.getItems(…(
我希望这个想法有帮助=(
if (i != items.size() -1) {
client.getItems (itemID, new JsonHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
if (response != null) {
final Myitem items = Myitem.fromJson( response );
itemNames = itemNames + items.getTitle() + "n";
//TODO save item names
}
}
@Override
public void onFinish(int statusCode, Header[] headers, JSONObject response) {
//DO nothing
}
})
} else {
client.getItems (itemID, new JsonHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
if (response != null) {
final Myitem items = Myitem.fromJson( response );
itemNames = itemNames + items.getTitle() + "n";
//TODO save item names
}
}
@Override
public void onFinish(int statusCode, Header[] headers, JSONObject response) {
String TimeFormat = "dd.MM.yyyy";
SimpleDateFormat df2 = new SimpleDateFormat( TimeFormat );
String dateText = df2.format( dateInMillis );
db = FirebaseFirestore.getInstance();
CollectionReference usernamedb = db.collection( "Users" ).document( auth.getUid() ).collection( "UserData" );
...
}
})
}