使用Spinner来筛选ListView。

huangapple go评论69阅读模式
英文:

Using Spinner to filter ListView

问题

我正在尝试为Employees的ListView创建一个筛选器。

我在MainActivity中收集spinner的值,它作为显示使用Toast的方式起作用。

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // 首先,我们需要将项目位置检索为字符串
        // 然后,我们可以将字符串值转换为整数
        String item_position = String.valueOf(position);
        positonInt = Integer.valueOf(item_position);
        Toast.makeText(MainActivity.this, "值是 " + positonInt, Toast.LENGTH_SHORT).show();
    }

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

    }
});

然后在MainActivity中通过调用getEmployeeList方法设置适配器。

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

我试图通过调用下面的方法来实现这一点,取决于Spinner的位置值是否传递给getEmployeeList()方法,然后在switch语句中使用它来创建查询以填充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<>();

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(query, null);

    if (cursor.moveToFirst()) {
        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 {
        // 空列表不包含任何项
    }

    cursor.close();
    db.close();

    return employeesList;
}

当我只设置String query而不使用switch语句时,getEmployeeList()方法可以正常工作并填充列表,但是当我尝试使用switch语句时,我会遇到错误。

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
英文:

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&lt;?&gt; 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, &quot;value is &quot;+ positonInt, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView&lt;?&gt; adapterView) {

        }

    });

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

    List&lt;Employee&gt; 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&lt;Employee&gt; getEmployeeList(int spinnerPostion) {


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

    List&lt;Employee&gt; employeesList = new ArrayList&lt;&gt;();

    //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 &#39;java.lang.String java.lang.String.trim()&#39; on a null object reference

答案1

得分: 1

你确定从Spinner获取的值是1吗?

或者如果它是0,请将其更改为

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

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 = &quot;SELECT * FROM &quot; + EMP_TABLE
                + &quot; ORDER BY &quot; + PROFIT + &quot; DESC &quot;;
        break;
}

huangapple
  • 本文由 发表于 2020年10月4日 22:19:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/64195779.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定