私はAndroidとJavaにかなり慣れていません。リストビューの項目をクリックしてインターネットからデータを取得していますが、必要な情報を取得するには2回の呼び出しが必要です。最初の呼び出しからint値を取得すると、それを2番目のメソッドに渡します。このメソッドは、独自の呼び出しを実行し、値を取得して、SharedPrefに配置します。最初のメソッドでデータを取得しようとすると、最初はデフォルト値の「」が返されますが、2番目の項目をクリックすると、最初に呼び出されたはずの結果が表示され、3回目のクリックで表示されます。 2番目の結果など...
データベースを試してみましたが、SharedPrefを試してみましたが、常に同じ結果になりました。メソッド呼び出しをスレッドに入れてみましたが、それでも同じです...
これは、spendingCategoryと呼ばれる2番目を呼び出す私の最初のメソッドです
private void listViewFunction() {
lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int userId = arrayList.get(position).idUser;
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
TrikoderAPI trikoderAPI = retrofit.create(TrikoderAPI.class);
Call<SingleFeed> call = trikoderAPI.getSingleFeed(userId);
call.enqueue(new Callback<SingleFeed>() {
@Override
public void onResponse(Call<SingleFeed> call, Response<SingleFeed> response) {
SingleFeed data = response.body();
final int categoryId = data.getData().getRelationships().getSpendingCategory().getData().getId();
spendingCategory(categoryId);
String info;
info = sp.getString(CATEGORY_NAME, "");
String result = getString(R.string.type) + data.getData().getType() + "\n"
+ getString(R.string.id) + data.getData().getId() + "\n"
+ getString(R.string.amount) + data.getData().getAttributes().getAmount() + "\n"
+ getString(R.string.remark) + data.getData().getAttributes().getRemark() + "\n"
+ getString(R.string.name) + data.getData().getAttributes().getName() + "\n"
+ getString(R.string.date) + data.getData().getAttributes().getDate() + "\n"
+ getString(R.string.category) + info;
popUpWindow(result);
editor.clear();
editor.commit();
}
@Override
public void onFailure(Call<SingleFeed> call, Throwable t) {
Toast.makeText(MainActivity.this, getString(R.string.somethingWrong) + t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
});
}
そしてこれは2番目の方法です
private void spendingCategory(int categoryId) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
TrikoderAPI trikoderAPI = retrofit.create(TrikoderAPI.class);
Call<SingleCategory> call = trikoderAPI.getCategoryFeed(categoryId);
call.enqueue(new Callback<SingleCategory>() {
@Override
public void onResponse(Call<SingleCategory> call, Response<SingleCategory> response) {
SingleCategory data = response.body();
String result = data.getData().getAttributes().getName();
Log.d(TAG, "onResponse: " + result);
sp.edit().putString(CATEGORY_NAME, result).apply();
@Override
public void onFailure(Call<SingleCategory> call, Throwable t) {
Toast.makeText(MainActivity.this, getString(R.string.somethingWrong), Toast.LENGTH_SHORT).show();
}
});
}
最初の出力は結果の文字列の値であり、SharedPrefから取得するデフォルト値ではないと思います
SharedPreference.apply()-このメソッドは非同期的に(後で)変更を保存します。
SharedPreference.commit()-このメソッドは同期的に(即座に)。
Retrofit enqueue()は非同期呼び出しです。そのため、実行の順序を確認することはできません。
あなたの問題については、2番目のメソッド呼び出し応答の後にアクションを実行する必要があります。つまり、spendingCategory
メソッドをに変更します
spendingCategory(int categoryId,SingleFeed data)
onResponse()内に以下のコードを追加します
...
String result = data.getData().getAttributes().getName();
Log.d(TAG, "onResponse: " + result);
sp.edit().putString(CATEGORY_NAME, result).commit();
String info;
info = sp.getString(CATEGORY_NAME, "");
String result = getString(R.string.type) + data.getData().getType() + "\n"
+ getString(R.string.id) + data.getData().getId() + "\n"
+ getString(R.string.amount) +
data.getData().getAttributes().getAmount() + "\n"
+ getString(R.string.remark) +
data.getData().getAttributes().getRemark() + "\n"
+ getString(R.string.name) +
data.getData().getAttributes().getName() + "\n"
+ getString(R.string.date) +
data.getData().getAttributes().getDate() + "\n"
getString(R.string.category) + info;
popUpWindow(result);
editor.clear();
editor.commit();
上記の実装により、上記の呼び出しに共有設定は必要ないと思います。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加