Radio Group 记住旧项目

Radio Group remembers old items

在我的 "FilterChoice" activity 中,我动态创建布局。我创建了一个单选组并根据 ArrayList 的大小在其中添加单选按钮。我的 "Filters" activity 中有一个列表。单选按钮因单击的过滤器而异。当我单击 "Prices" 过滤器时,它会查看 "FilterChoice" activity 上的六个项目。然后按下后退按钮,当我单击 "Categories" 过滤器时,onCheckedChanged() 中的 checkedId 从 7 开始,而不是 1。为什么它不从新的 activity 调用中的一个开始?如何解决?这是我的 "FilterChoice" activity.

`

public class FilterChoice extends AppCompatActivity {
    RadioButton rb;
    String filter;
    String selection, domain, temp;
    RadioGroup rg;
    public static ArrayList<KeyValuePair> cate_old;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        LinearLayout linearLayout = new LinearLayout(FilterChoice.this);
        LinearLayout.LayoutParams lrp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(lrp);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        // rb = new RadioButton(FilterChoice.this);
        // rb.setText("All");
        // rg.addView(rb);

        Intent intent = getIntent();
        filter = intent.getExtras().getString("filter");

        if (filter.equals("Price Range")) {
            rg = new RadioGroup(FilterChoice.this);
            rg.setLayoutParams(lp);
            if (SearchResults.prices.size() == 1) {

                rb = new RadioButton(FilterChoice.this);
                rb.setText(SearchResults.price_s_sel + "-"
                        + SearchResults.price_e_sel + " ("
                        + SearchResults.price_val + ")");
                rg.addView(rb);

            } else {
                int it;
                for (it = 0; it < SearchResults.prices.size() - 1; it++) {
                    rb = new RadioButton(FilterChoice.this);
                    rb.setText(SearchResults.prices.get(it).key + "-"
                            + SearchResults.prices.get(it + 1).key + " ("
                            + SearchResults.prices.get(it).value + ")");
                    rg.addView(rb);
                }
                rb = new RadioButton(FilterChoice.this);
                rb.setText("Above" + SearchResults.prices.get(it).key + " ("
                        + SearchResults.prices.get(it).value + ")");
                rg.addView(rb);
            }

        } else if (filter.equals("Sites")) {
            rg = new RadioGroup(FilterChoice.this);
            rg.setLayoutParams(lp);
            if (SearchResults.site.size() == 1) {
                rb = new RadioButton(FilterChoice.this);
                rb.setText(SearchResults.site_sel + " ("
                        + SearchResults.site_val + ")");
                rg.addView(rb);
                // }
            } else {
                StringTokenizer st;
                for (int i = 0; i < SearchResults.site.size(); i++) {
                    st = new StringTokenizer(SearchResults.site.get(i).key, ".");
                    if (st.hasMoreTokens()) {
                        domain = st.nextToken();
                    }
                    rb = new RadioButton(FilterChoice.this);
                    rb.setText(domain + " (" + SearchResults.site.get(i).value
                            + ")");
                    rg.addView(rb);
                }
            }
        } else if (filter.equals("Categories")) {
            rg = new RadioGroup(FilterChoice.this);
            rg.setLayoutParams(lp);
            if (SearchResults.cate.size() == 1) {
                rb = new RadioButton(FilterChoice.this);
                rb.setText(SearchResults.cate_sel + " ("
                        + SearchResults.cate_val + ")");
                rg.addView(rb);
                // }
            } else {
                for (int i = 0; i < SearchResults.cate.size(); i++) {
                    rb = new RadioButton(FilterChoice.this);
                    rb.setText(SearchResults.cate.get(i).key + " ("
                            + SearchResults.cate.get(i).value + ")");
                    rg.addView(rb);
                }
            }

        } else if (filter.equals("Colors")) {
            rg = new RadioGroup(FilterChoice.this);
            rg.setLayoutParams(lp);
            if (SearchResults.cols.size() == 1) {
                rb = new RadioButton(FilterChoice.this);
                rb.setText(SearchResults.cols_sel + " ("
                        + SearchResults.cols_val + ")");
                rg.addView(rb);
            } else {
                for (int i = 0; i < SearchResults.cols.size(); i++) {
                    rb = new RadioButton(FilterChoice.this);
                    rb.setText(SearchResults.cols.get(i).key + " ("
                            + SearchResults.cols.get(i).value + ")");
                    rg.addView(rb);
                }
            }

        } else if (filter.equals("Brands")) {
            rg = new RadioGroup(FilterChoice.this);
            rg.setLayoutParams(lp);
            if (SearchResults.brands.size() == 1) {
                rb = new RadioButton(FilterChoice.this);
                rb.setText(SearchResults.brand_sel + " ("
                        + SearchResults.brand_val + ")");
                rg.addView(rb);
            } else {
                for (int i = 0; i < SearchResults.brands.size(); i++) {
                    rb = new RadioButton(FilterChoice.this);
                    rb.setText(SearchResults.brands.get(i).key + " ("
                            + SearchResults.brands.get(i).value + ")");
                    rg.addView(rb);
                }
            }
        } else if (filter.equals("Sub Categories")) {
            rg = new RadioGroup(FilterChoice.this);
            rg.setLayoutParams(lp);
            if (SearchResults.subcate.size() == 1) {
                rb = new RadioButton(FilterChoice.this);
                rb.setText(SearchResults.subcate_sel + " ("
                        + SearchResults.subcate_val + ")");
                rg.addView(rb);
            } else {
                for (int i = 0; i < SearchResults.subcate.size(); i++) {
                    rb = new RadioButton(FilterChoice.this);
                    rb.setText(SearchResults.subcate.get(i).key + " ("
                            + SearchResults.subcate.get(i).value + ")");
                    rg.addView(rb);
                }
            }
        }

        linearLayout.addView(rg);
        setContentView(linearLayout);

        rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if (filter.equals("Price Range")) {

                } else if (filter.equals("Sites")) {
                    Log.e("checkedId", "" + checkedId);
                    if (SearchResults.site.size() > 1) {
                        selection = SearchResults.site.get(checkedId - 1).key;
                        temp = SearchResults.site.get(0).key;

                        SearchResults.site_sel = selection;
                        SearchResults.site_val = SearchResults.site
                                .get(checkedId - 1).value;
                        SearchResults.site_id = checkedId;
                    }

                } else if (filter.equals("Categories")) {
                    Log.e("checkedId", "" + checkedId);

                    if (SearchResults.cate.size() > 1) {

                        selection = SearchResults.cate.get(checkedId - 1).key;
                        temp = SearchResults.cate.get(0).key;
                        SearchResults.cate_sel = selection;
                        SearchResults.cate_val = SearchResults.cate
                                .get(checkedId - 1).value;
                        SearchResults.cate_id = checkedId;
                    }

                } else if (filter.equals("Colors")) {

                    Log.e("checkedId", "" + checkedId);
                    if (SearchResults.cols.size() > 1) {

                        selection = SearchResults.cols.get(checkedId - 1).key;
                        temp = SearchResults.cols.get(0).key;
                        SearchResults.cols_sel = selection;
                        SearchResults.cols_val = SearchResults.cols
                                .get(checkedId - 1).value;
                        SearchResults.cols_id = checkedId;
                    }
                } else if (filter.equals("Brands")) {
                    Log.e("checkedId", "" + checkedId);
                    if (SearchResults.brands.size() > 1) {

                        selection = SearchResults.brands.get(checkedId - 1).key;
                        temp = SearchResults.brands.get(0).key;
                        SearchResults.brand_sel = selection;
                        SearchResults.brand_val = SearchResults.brands
                                .get(checkedId - 1).value;
                        SearchResults.brand_id = checkedId;
                    }
                } else if (filter.equals("Sub Categories")) {
                    Log.e("checkedId", "" + checkedId);
                    if (SearchResults.subcate.size() > 1) {
                        selection = SearchResults.subcate.get(checkedId - 1).key;
                        temp = SearchResults.subcate.get(0).key;
                        SearchResults.subcate_sel = selection;
                        SearchResults.subcate_val = SearchResults.subcate
                                .get(checkedId - 1).value;
                        SearchResults.subcate_id = checkedId;
                    }

                }

                Intent intent = new Intent(FilterChoice.this, Filters.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });

    }
}

`

我还没有弄清楚您描述的行为,但我要指出的是,您可以设置动态创建的 RadioButton 的 ID。例如,在您的 "Categories" 块中:

for (int i = 0; i < SearchResults.cate.size(); i++) {
    rb = new RadioButton(FilterChoice.this);
    rb.setId(i + 1);
    rb.setText(SearchResults.cate.get(i).key + " ("
               + SearchResults.cate.get(i).value + ")");
    rg.addView(rb);
}

这会将 ID 设置为 1 到 SearchResults.cate.size(),我相信这就是您想要的。