为什么onItemSelected不能使用setOnCheckedChangeListener?

时间:2018-06-16 19:47:21

标签: android android-studio

这是我在这里发表的第一篇文章:)

我想用两个RadioButton和Spinner创建简单的活动。 Spinner从数据库中获取数据,显示数据,并在单击所选项目后运行任务。在添加两个RadioButton以对我的数据进行过滤之类的操作后,我遇到了令人讨厌的错误 - 显示了数据,但是onItemSelected没有运行任务(启动下一个活动)。所以我不知道如何解决这个问题。

public class TrackSpinner extends Activity{

class MyRadioButton {
    private List<String> routes = new ArrayList<String>();

    public List<String> getRoutes() {
        return (routes);
    }

    public List<String> addRoutes(List<String> a) {
        routes.clear();
        routes.addAll(a);
        routes.add(0, getString(R.string.trackSpinner1));
        return routes;
    }

    public List<String> noRoutes() {
        routes.clear();
        routes.add(0, getString(R.string.trackSpinner2));
        return routes;
    }
}

public void trackInfo(String jpg, String describtion) {
    Intent i = new Intent(TrackSpinner.this, ShowTrack.class);
    i.putExtra("obrazek", jpg);
    i.putExtra("opis", describtion);
    startActivity(i);
}

public String[] getDataAboutRoute (String sdisr){
    DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
    databaseAccess.open();
    ArrayList<String> route = databaseAccess.getAllDescriptionsFromRouteArray(sdisr);
    databaseAccess.close();
    String desr = route.get(0);
    String img = route.get(1);
    return new String[] {img, desr};
}

private boolean isSpinnerTouched = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.object_list_for_track);
    DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
    databaseAccess.open();
    final List<String> routes = databaseAccess.getAvailableRoute();
    databaseAccess.close();
    final RadioButton r1 = (RadioButton) findViewById(R.id.radioButton1);
    final RadioButton r2 = (RadioButton) findViewById(R.id.radioButton2);
    final RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup) ;
    final MyRadioButton r = new MyRadioButton();


    Log.e("4---->", String.valueOf(r.getRoutes()));

    final Spinner trackSpinner1 = (Spinner) findViewById(R.id.dynamic_spinner);
    ArrayAdapter<String> adapter1 = new ArrayAdapter<>(this, android.R.layout.select_dialog_item, r.getRoutes());
    trackSpinner1.setAdapter(adapter1);

    trackSpinner1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.performClick();
            isSpinnerTouched = true;
            return false;
        }
    });

    trackSpinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (!isSpinnerTouched) return;
            String short_name = trackSpinner1.getSelectedItem().toString();
            Log.e("5---->", String.valueOf(short_name));
            getDataAboutRoute(short_name);
            String result[] = getDataAboutRoute(short_name);
            trackInfo(result[0], result[1]);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub
        }
    });

    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(r1.isChecked())
            {
                r.addRoutes(routes);
                //Log.e("---->", String.valueOf(r.getRoutes()));
            }
            else if (r2.isChecked())
            {
                r.noRoutes();
                //Log.e("---->", String.valueOf(r.getRoutes()));
            }
            else {
                Log.e("---->", "OK, here I'm");
            }
        }
    });

}

}

And how it's look

0 个答案:

没有答案