英文:
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<?> 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
答案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 = "SELECT * FROM " + EMP_TABLE
+ " ORDER BY " + PROFIT + " DESC ";
break;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论