Dave
Dave

Reputation: 97

Using Spinner to filter ListView

I am trying to create a filter for a ListView of Employees.

I am collecting the spinner value in the MainActivity which works as it is displayed using Toast

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            //first,  we have to retrieve the item position as a string
            // then, we can change string value into integer
            String item_position = String.valueOf(position);
            positonInt = Integer.valueOf(item_position);
            Toast.makeText(MainActivity.this, "value is "+ positonInt, Toast.LENGTH_SHORT).show();
        }

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

        }

    });

Later in the MainActivity I set the adapter by calling the methodgetEmployeeList

    List<Employee> employeeList = dataBaseHelper.getEmployeeList(positonInt);
    adaptor = new EmployeeAdaptor(MainActivity.this, employeeList);
    empListView.setAdapter(adaptor);

I am trying to achieve this by calling the method below, depending the Spinner position value is passed to the method getEmployeeList() which is then used in a switch statement to create the query to populate the ListView

public List<Employee> getEmployeeList(int spinnerPostion) {


    switch (spinnerPostion) {
        case 1:  spinnerPostion = 0;
            query = "SELECT * FROM " + EMP_TABLE
                    + " ORDER BY " + PROFIT + " DESC ";
            break;
    }

    List<Employee> employeesList = new ArrayList<>();

    //Referencing the active database to read infromation
    SQLiteDatabase db = this.getReadableDatabase();

    //Executing the query
    //  -Using a Cursor so we can iterate through all readable data in the db
    Cursor cursor = db.rawQuery(query, null);

    if (cursor.moveToFirst()) {
        //If there are results loop through and create a new Item
        //for every item in the db
        do {
            int employeeID = cursor.getInt(0);
            String employeeName = cursor.getString(1);
            String employeeSecondName = cursor.getString(2);
            int profit = cursor.getInt(3);
            Employee menu_emp = new Employee(employeeID, employeeName, employeeSecondName, profit);
            employeesList.add(menu_emp);

        } while (cursor.moveToNext());
    } else {
        // Empty List contains no Items
    }

    //Closing connection to the database and cursor
    cursor.close();
    db.close();

    return employeesList;
}  

The method getEmployeeList() works without the switch statement and populates the list when I just set String query but I am getting errors when I try to do this using the switch statement.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dropit/com.example.dropit.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference

Upvotes: 1

Views: 94

Answers (1)

Wowo Ot
Wowo Ot

Reputation: 1529

Are you sure the value you are getting from the Spinner is 1

or you if it's 0 change it to

case 0:
{
    spinnerPostion = 0;
        query = "SELECT * FROM " + EMP_TABLE
                + " ORDER BY " + PROFIT + " DESC ";
        break;
}

Upvotes: 1

Related Questions