Search code examples
androidandroid-radiobutton

Radio Group remembers old items


In my "FilterChoice" activity, I create the layout dynamically. I create a Radio Group and add Radio Buttons in it depending on the size of an ArrayList. There's a list in my "Filters" activity. The radio buttons are different depending on the filters clicked. When I click "Prices" filter, it views six items on the "FilterChoice" activity. Then pressing back button, when I click "Categories" filter, the checkedId's in the onCheckedChanged() starts from seven, instead of one. Why does it not start from one in the new activity call? How to solve it? Here's my "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);
            }
        });

    }
}

`


Solution

  • I have not yet figured out the behavior you describe, but I would point out that you can set the IDs of the RadioButtons you're creating dynamically. For example, in your "Categories" block:

    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);
    }
    

    This will set IDs from 1 to SearchResults.cate.size(), which I believe is what you're going for.