当我通过查看评分选择“优先级”单选按钮(id = radioButton)进行排序时,应用程序会自行关闭。当我删除RadioGroup.OnCheckedChangeListener(){ @Override public void onCheckedChanged(RadioGroup group,int CheckedId){ rb.findViewById(checkedId); if(rb == rb1){ Collections.sort(phonelist); } } }); 在这部分上,除排序外,其他都很好。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
attachViews();
this.initializeViews();
/*RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
phoneAdapter = new PhoneAdapter(this, phonelist);
recyclerView.setAdapter(phoneAdapter);*/
updateTotal();
}
public void sortData(boolean asc) {
//SORT ARRAY ASCENDING AND DESCENDING
if (asc) {
Collections.sort(phonelist);
} else {
Collections.reverse(phonelist);
}
}
private void initializeViews()
{
rg=findViewById(R.id.radiogroup1);
rb1=findViewById(R.id.radioButton);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
rb.findViewById(checkedId);
if(rb==rb1){
Collections.sort(phonelist);
}
}
});
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView= (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
phoneAdapter = new PhoneAdapter(this, phonelist);
recyclerView.setAdapter(phoneAdapter);
答案 0 :(得分:0)
将侦听器更改为此:
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.radioButton){
Collections.sort(phonelist);
}
}
我猜R.id.radioButton
是被单击正确的RadioButton的ID吗?