所以,当我在Android模拟器上运行我的项目时 - 一切都很完美。但是当我在两部智能手机上测试项目时 - 这部分代码总是不能正常工作。此片段必须显示有关服务器事务的一些信息。那是来自模拟器的screenshot。这是我的一部智能手机的screenshot。两个测试都有相同的.apk文件。有时,在真实设备上所有都可以正常运行,但我不知道,它是如何工作的。我曾尝试清理/重建项目。其他片段工作正常,因此它与服务器或sid无关。
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.Penok.API.APIUtils;
import com.Penok.API.Model;
import com.Penok.API.PenyokService;
import com.Penok.API.TransactionsListModel;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class TransactionsFragment extends Fragment {
private PenyokService transactionsService;
private RecyclerView transactionsRecyclerView;
private TransactionsAdapter transactionsAdapter;
private ArrayList<String> authorNames;
private ArrayList<String> payeeNames;
private ArrayList<String> payerNames;
private ArrayList<TransactionsListModel> transactions;
private int authorCounter = 0;
private int payeeCounter = 0;
private int payerCounter = 0;
private int size = 0;
public TransactionsFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
transactionsRecyclerView = (RecyclerView) inflater.inflate(R.layout.fragment_recycle, container, false);
transactionsService = APIUtils.getPService();
transactions = new ArrayList<>();
authorNames = new ArrayList<>();
payeeNames = new ArrayList<>();
payerNames = new ArrayList<>();
transactionsRecyclerView = (RecyclerView) inflater.inflate(R.layout.fragment_recycle, container, false);
transactionsAdapter = new TransactionsAdapter(new ArrayList<TransactionsListModel>(0),
new ArrayList<String>(0), new ArrayList<String>(0), new ArrayList<String>(0));
transactionsRecyclerView.setAdapter(transactionsAdapter);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
transactionsRecyclerView.setLayoutManager(layoutManager);
loadTransactions();
return transactionsRecyclerView;
}
private void loadTransactions() {
transactionsService.getTransactionsHistory(MainActivity.getSid()).enqueue(new Callback<Model>() {
@Override
public void onResponse(Call<Model> call, Response<Model> response) {
if(response.isSuccessful()) {
if (response.body().getTransactionsList() != null) {
Toast.makeText(getActivity(), "not null", Toast.LENGTH_SHORT).show();
transactions.addAll(response.body().getTransactionsList());
size = transactions.size();
for (int i = 0; i < transactions.size(); i++) {
loadNames(transactions.get(i).getAuthor(), "author");
loadNames(transactions.get(i).getPayee(), "payee");
loadNames(transactions.get(i).getPayer(), "payer");
}
}
else
Toast.makeText(getActivity(), "null", Toast.LENGTH_SHORT).show();
needToUpdate(authorCounter, payeeCounter, payerCounter,
size, transactions, authorNames, payeeNames, payerNames);
// transactionsAdapter.updateTransactions(response.body().getTransactionsList());
} else
Toast.makeText(getActivity(), "loadTransactions", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<Model> call, Throwable t) {
Toast.makeText(getActivity(), t.toString(), Toast.LENGTH_SHORT).show();
}
});
}
private void loadNames(final int id, final String key){
transactionsService.getUserName(id, MainActivity.getSid()).enqueue(new Callback<Model>() {
@Override
public void onResponse(Call<Model> call, Response<Model> response) {
if(response.isSuccessful()) {
switch (key) {
case "author":
authorNames.add(response.body().getNameFor());
authorCounter++;
break;
case "payee":
payeeNames.add(response.body().getNameFor());
payeeCounter++;
break;
case "payer":
payerNames.add(response.body().getNameFor());
payerCounter++;
break;
}
//transactionsAdapter.updateTransactions(response.body().getTransactionsList());
needToUpdate(authorCounter, payeeCounter, payerCounter, size,
transactions, authorNames, payeeNames, payerNames);
} else
Toast.makeText(getActivity(), "loadNames", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<Model> call, Throwable t) {
Toast.makeText(getActivity(), t.toString(), Toast.LENGTH_SHORT).show();
}
});
}
private void needToUpdate(int authorCounter,
int payeeCounter,
int payerCounter,
int size,
List<TransactionsListModel> response,
ArrayList<String> authorNames,
ArrayList<String> payeeNames,
ArrayList<String> payerNames){
if ((authorCounter == size) && (payeeCounter == size) && (payerCounter == size)) {
transactionsAdapter.updateTransactions(response, authorNames, payeeNames, payerNames);
}
}
}
此外,在真实设备上显示两个toast(已添加以测试此问题):