jeudi 28 janvier 2016

sqlite query not working android

These are the methods of spinners

mSpinnerModel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { selectedModel = parent.getItemAtPosition(position).toString();

                Toast.makeText(parent.getContext(), "Selected Model: " + selectedModel, Toast.LENGTH_LONG).show();

                String sp1 = String.valueOf(mSpinnerModel.getSelectedItem());
                if (sp1.contentEquals("College1")) {
                    List<String> list = new ArrayList<String>();
                    list.add("MAHARANI UNIVERSITY ");
                    //list.add("MANIPAL ");
                  //  list.add("ITM UNIVERSITY ");
                   // list.add("UNIVERSITY1");
                    // list.add("MAHARANI UNIVERSTY");


                    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, list);
                    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    dataAdapter.notifyDataSetChanged();


                    // selectedMake = parent.getItemAtPosition(position).toString();

                    mSpinnerMake.setAdapter(dataAdapter);
                }

                if (sp1.contentEquals("College2")) {
                    List<String> list1 = new ArrayList<String>();
                    list1.add("ITM UNIVERSITY");
                    //list1.add("UNIVERSITY2");
                   // list1.add("UNIVERSITY3");

                  //  selectedMake = parent.getItemAtPosition(position).toString();


                    ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, list1);
                    dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    dataAdapter2.notifyDataSetChanged();
                    mSpinnerMake.setAdapter(dataAdapter2);
                }


                if (sp1.contentEquals("College3")) {
                    List<String> list = new ArrayList<String>();
                    list.add("MANIPAL ");
                    //list.add("UNIVERSITY4");
                    //list.add("UNIVERSITY5");


                    ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, list);
                    dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    dataAdapter3.notifyDataSetChanged();
                    mSpinnerMake.setAdapter(dataAdapter3);
                   // selectedMake = parent.getItemAtPosition(position).toString();
                }


            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

mSpinnerMake.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        selectedMake = parent.getItemAtPosition(position).toString();

    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
});

        mSpinnerYear.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                selectedYear = parent.getItemAtPosition(position).toString();
                // Showing selected spinner item
                Toast.makeText(parent.getContext(), "Selected Year: " + selectedYear, Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        mButtonShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String eligibleBattery=fetchEligibleBattery(mSqLiteDatabase);
                mTextViewResult.setText(eligibleBattery);
            }
        });

    }

And this is method for data and query for fetching data but it is not working. when i select data in spinners it shows in logcat but it is not fetching data the query right below is not working please help.

 private String fetchEligibleBattery(SQLiteDatabase db) {

        String battery="";
        String SELECT_BATTERY_QUERY = "SELECT "+ DatabaseConstants.KEY_BATTERY+" FROM " + DatabaseConstants.TABLE_CAR_DETAILS + " WHERE " + DatabaseConstants.KEY_CAR_MAKE + " ='" +selectedMake +
                "' AND "+ DatabaseConstants.KEY_CAR_MODEL +" = '"+ selectedModel+ "' AND "+ DatabaseConstants.KEY_CAR_YEAR+"= '"+selectedYear +"' ;";
        Log.d("Database", "Battery Select Query : " + SELECT_BATTERY_QUERY);
        Cursor cursor = db.rawQuery(SELECT_BATTERY_QUERY, null);
        try {
            if (cursor.moveToFirst()) {
                battery=cursor.getString(cursor.getColumnIndex(DatabaseConstants.KEY_BATTERY));
            }
        } catch (Exception e) {
            Log.d("Database", "Error while trying to get icons from database");
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }
        return battery;
    }

    public void fetchDataFromDBForSpinners(SQLiteDatabase db) {


        String SELECT_MODELS_QUERY = "SELECT * FROM " + DatabaseConstants.TABLE_CAR_DETAILS + ";";

        String model, make, year;
        Cursor cursor = db.rawQuery(SELECT_MODELS_QUERY, null);
        try {
            if (cursor.moveToFirst()) {
                do {
                    model = cursor.getString(cursor.getColumnIndex(DatabaseConstants.KEY_CAR_MODEL));
                    make = cursor.getString(cursor.getColumnIndex(DatabaseConstants.KEY_CAR_MAKE));
                    year = cursor.getString(cursor.getColumnIndex(DatabaseConstants.KEY_CAR_YEAR));

                    models.add(model);
                     makes.add(make);
                    years.add(year);

                } while (cursor.moveToNext());
            }
        } catch (Exception e) {
            Log.d("DATABASE", "Error while trying to get events from database");
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }

Aucun commentaire:

Enregistrer un commentaire