簡體   English   中英

Android Studio。 捆綁包僅返回所選中的最后一個值

[英]Android studio. Bundle returns only the last value from selected

我有一個帶自定義適配器的listView ,其中有一個checkBoxTextView ,當我選擇一些CheckBox時,我將它們添加到bundle並發送到另一個fragment 在該片段中,我有一個簡單的TextView,需要從bundle獲取值並在該TextView中顯示。 但是問題是,當我從包中獲取選定的值並在TextView中進行設置時,它僅顯示最后一個值。 我該如何解決? 謝謝。

這是我將選定值添加到包中的片段。

nextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            ArrayList<Profession> professionList = dataAdapter.getProfessionList();
            ArrayList<String> getProfessions = new ArrayList<>();
            personalInfoFragment = new PersonalInfoFragment();
            for (int i = 0; i < professionList.size(); i++) {
                Profession profession = professionList.get(i);
                if (profession.isSelected()) {
                    getProfessions.add(profession.getName());

                    Bundle setProfessions = new Bundle();
                    setProfessions.putStringArrayList("String", getProfessions);

                    personalInfoFragment.setArguments(setProfessions);
                }
            }
            replaceFragment();
        }
    });

好的,在這里觀看。 PersonalInfoFragment是我需要發送值的片段。 然后replaceFragment方法替換片段。 我完成調試,然后將所選值成功添加到捆綁包中。 nextButton單擊,它將選擇的數據保存到包中並替換片段。

現在是我需要獲取捆綁包並在textView中顯示的片段。

Bundle bundle = getArguments();

    if (bundle != null) {
        ArrayList<String> getProfessionName = bundle.getStringArrayList("String");
        if (getProfessionName != null) {
            for (int i = 0; i < getProfessionName.size(); i++) {
                profession_text.setTextColor(Color.BLACK);
                profession_text.setText(getProfessionName.get(i) + ",");
            }
        }
    }

ProfessionText是我的textView,需要在其中設置值。 但在該textView中,僅顯示捆綁商品中的最后一項。 如何解決這個問題? 感謝您的閱讀。

您可以更改代碼,例如

for (int i = 0; i < professionList.size(); i++) {
    Profession profession = professionList.get(i);
    if (profession.isSelected()) {
        getProfessions.add(profession.getName());
       }
}
Bundle setProfessions = new Bundle();
setProfessions.putStringArrayList("String", getProfessions);
personalInfoFragment.setArguments(setProfessions);
replaceFragment();

在下一個片段中

profession_text.setTextColor(Color.BLACK);
profession_text.setText("");
if (getProfessionName != null) {
    for (int i = 0; i < getProfessionName.size(); i++) {
        String txt = profession_text.getText().toString() + getProfessionName.get(i) + ", ";
        profession_text.setText(txt);
    }
}

從我所看到的..u需要正確顯示值的列表..發生的事情是在每次迭代中,您為專業文本設置了一個新值,該值將覆蓋以前的值。.這就是為什么您看到最后一個值..我想如果您想要一個文本視圖列表,則必須在循環中創建一個新的文本視圖。.在這種情況下,最好使用列表視圖或回收者視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM