0
在我的「FilterChoice」活動中,我動態地創建了佈局。我創建一個Radio組並根據ArrayList的大小在其中添加Radio Buttons。我的「過濾器」活動中有一個列表。單選按鈕根據點擊的過濾器而不同。當我點擊「價格」過濾器時,它會在「FilterChoice」活動中查看六個項目。然後按回來按鈕,當我點擊「Categories」過濾器時,onCheckedChanged()中的checkedId從7開始,而不是從1開始。爲什麼它不是從新的活動呼叫中開始的?如何解決它?這是我的「FilterChoice」活動。無線電組記得舊物品
`
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);
}
});
}
}
`
我被困在這個很簡單的東西很長一段時間。謝謝! – alu