私は2つの断片を持っています。 フラグメントAは断片Bにつながる - フラグメントBに私は、外部データベースからデータを取得し、共有設定の値を保存し、フラグメントにこのバックを渡しています。 フラグメントBでset_idはIDを繰り返しループしています
、Iは、JSON配列を処理して、以下、を有する:断片で
public ArrayList getAll_question_ids(){
return all_question_ids;
}
@Override
public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {
ServerResponse resp = response.body();
//Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();
if (resp.getResult().equals(Constants.SUCCESS)) {
SharedPreferences.Editor editor = pref.edit();
Log.d("Question_IDs", "getAllQuestionID() = " + response.body().getQuestion().getAll_question_ids());
editor.putString(Constants.All_QUESTION_IDS,((resp.getQuestion().getAll_question_ids().toString())));
editor.apply();
String questionNumber = pref.getString(Constants.All_QUESTION_IDS, "");
Toast.makeText(getActivity(), "Question ID = " + questionNumber,
Toast.LENGTH_LONG).show();
goToCreateQuestionFragment();
}
progress.setVisibility(View.INVISIBLE);
}
を以下に示すようにIは、次いでonViewCreated方法を介してメソッドを呼び出す:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
pref = getActivity().getPreferences(0);
if ((!pref.getString(Constants.All_QUESTION_IDS, "").equals(null) && !pref.getString(Constants.All_QUESTION_IDS, "").equals(""))) {
createQuestionButton();
}
}
createQuestionButtonメソッドは私が混乱しているところです。私は、共有設定に保存された値をダイナミックに作成されたボタンに割り当てるためにループを使用しています。これはリニアレイアウトに追加されます。
ボタンのユニークなIDを設定し、ボタンのアクションを作成できるように、onClickListenerをアタッチします。
API17以上を使用しているデバイスにはView.generateViewId()
を使用できますが、これは素晴らしいことです。
しかし、各ボタンに割り当てられている値が正しく作成されているかどうかを確認するために、1つのIDを割り当てるのではなく、コードが4/5回実行されていることに気づきました。ボタンごとに生成されます!
これは、私がonViewCreatedのメソッドを呼び出しているという事実と関係していると思いますが、メソッドを呼び出す方法がわかりません。
と呼ばれている方法は以下の通りです:
@TargetApi(Build.VERSION_CODES.M)
public void createQuestionButton() {
//get all the question_ids from shared pref, that have been stored from the SetQuestion Activity
//in the allQuestionIDS() method
String questionNumber = pref.getString(Constants.All_QUESTION_IDS, "");
//converting the above String back into a List
questionNumber = questionNumber.substring(1, questionNumber.length() - 1);
//split the array using the comma
String[] array = questionNumber.split(", ");
//Converting questionArray array to a list using Arrays.asList()
List list = Arrays.asList(array);
if (!questionNumber.equals("") && !questionNumber.equals(null)) {
for (Object value : list) {
try {
/*Dynamically create new Button which includes the question number
*/
AppCompatButton btn_question = new AppCompatButton(getActivity());
/*LayoutParams (int width, int height,float weight)
As LayoutParams defaults in px, I have called a method called dpToPX to make sure the dynamically added EditText is the same size on all devices.
*/
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(dpToPx(280), dpToPx(45), 1);
btn_question.setBackgroundColor(Color.parseColor("#3B5998"));
btn_question.setTextColor(Color.WHITE);
btn_question.setText("Question " + value);
btn_question.setGravity(Gravity.CENTER);
//generate unique ID for each new EditText dynamically created
btn_question.setId(View.generateViewId());
params.setMargins(0, dpToPx(10), 0, dpToPx(10));
btn_question.setPadding(0, 0, 0, 0);
btn_question.setLayoutParams(params);
allEds.add(btn_question);
mLayout.addView(btn_question);
Toast.makeText(getActivity(), "Question ID = " + btn_question.getId(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.d(TAG, "Failed to create new button");
}
}
}
}
これが原因とされており、回避するためのベストプラクティスは、いただければ幸い方法上の任意のガイダンス。私はフラグメントBにトーストに合格し、結果がサーバから全体で来て表示されます
EDIT
は正しいですが、それは同じ情報を乾杯されているかのようにトーストは、上にぶら下がっているように見えます複数回。
私はインタープリタをチェックして、渡されたデータが正しいので、なぜループ内でスタックしているように見えるのか分かりません。特にフラグメントBにループがないときは!次のように
JSONを取得しているが、次のとおりです。
{
"result": "success",
"message": "All Questions Have Been Selected",
"question": {
"all_question_ids": ["1","2","3"]
}
}
私はあなたが正しいと思いますし、自分自身のループ自体が何度か実行されていて、それは単に 'btn_question.setId(View.generateViewId()); 。ループに渡されるデータは正確ですが、ループの回数が間違っているため、なぜこれが問題になるのでしょうか? –