为什么单选按钮不能使用此代码?

时间:2019-04-07 15:48:02

标签: java android

当我通过查看评分选择“优先级”单选按钮(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);

1 个答案:

答案 0 :(得分:0)

将侦听器更改为此:

public void onCheckedChanged(RadioGroup group, int checkedId) {
    if(checkedId == R.id.radioButton){
        Collections.sort(phonelist);
    }
}

我猜R.id.radioButton是被单击正确的RadioButton的ID吗?

相关问题