我正在使用微调器中的值来帮助筛选将成为RecyclerView数据源的列表。当我使用微调器时,似乎从未调用过OnItemSelect方法
这是一个使用Java构建的Android应用程序,使用Pie作为OS。该项目是使用“基本活动模板”创建的。
活动类
package com.logicscrew.android;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import com.logicscrew.android.helpers.APIHelper;
import com.logicscrew.android.helpers.EmployeeAdapter;
import com.logicscrew.android.models.Department;
import com.logicscrew.android.models.Employee;
import java.util.ArrayList;
import java.util.List;
public class ViewByDepartment extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private List<Employee> employees;
private List<Employee> filteredList;
private List<Department> deparments;
private RecyclerView rcView;
private EmployeeAdapter adapter;
private Context context;
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
employees = new ArrayList<>();
deparments = new ArrayList<>();
filteredList = new ArrayList<>();
setContentView(R.layout.activity_view_by_department);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
spinner =(Spinner)findViewById(R.id.spnDepartments);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
context = this;
FetchTask task = new FetchTask(new AsyncResponse() {
@Override
public void processFinished(List<Employee> output) {
Log.i("Employees", "Finished");
}
});
DeptTask deptTask = new DeptTask(new DeptResponse() {
@Override
public void processFinished(List<Department> output) {
Log.i("Department", "Finished");
}
});
task.execute();
deptTask.execute();
rcView = findViewById(R.id.rcEmp);
adapter = new EmployeeAdapter(context, filteredList);
rcView.setLayoutManager(new LinearLayoutManager(context));
rcView.setAdapter(adapter);
ArrayAdapter<Department> deptAdapter = new ArrayAdapter<>(
this,
R.layout.spinner,
deparments
);
deptAdapter.setDropDownViewResource(
R.layout.spinner
);
spinner.setAdapter(deptAdapter);
spinner.setOnItemSelectedListener(this);
}
private void filter(){
filteredList.clear();
filteredList.addAll(employees);
for(Employee e : filteredList){
if(e.department != spinner.getSelectedItem()){
filteredList.remove(e);
}
}
adapter.swapDataSet(filteredList);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
filter();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
private class FetchTask extends AsyncTask<Void, Void, List<Employee>> {
public AsyncResponse delegate = null;
public FetchTask(AsyncResponse asyncResponse){
delegate = asyncResponse;
}
@Override
protected List<Employee> doInBackground(Void... voids) {
return new APIHelper().fetchItems();
}
@Override
protected void onPostExecute(List<Employee> items) {
employees.addAll(items);
filteredList.addAll(items);
adapter.notifyDataSetChanged();
delegate.processFinished(items);
}
}
private class DeptTask extends AsyncTask<Void, Void, List<Department>>{
public DeptResponse delegate;
public DeptTask(DeptResponse response){
delegate = response;
}
@Override
protected List<Department> doInBackground(Void... voids) {
return new APIHelper().fetchDepartments();
}
@Override
protected void onPostExecute(List<Department> items){
deparments.addAll(items);
delegate.processFinished(items);
}
}
public interface AsyncResponse{
void processFinished(List<Employee> output);
}
public interface DeptResponse{
void processFinished(List<Department> output);
}
}
应该发生的是,当事件被调用时,它将过滤列表,删除列表中不在特定部门中但从未调用该事件的任何人。
答案 0 :(得分:0)
您尝试输入onCreate:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int myPosition, long myID) {
//your method
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
答案 1 :(得分:0)
问题是异步争用条件。通过将Spinner设置移至DeptTask中的processFinished方法中进行了修复。
DeptTask deptTask = new DeptTask(new DeptResponse() {
@Override
public void processFinished(List<Department> output) {
Log.i("Department", "Finished");
ArrayAdapter<Department> deptAdapter = new ArrayAdapter<Department>(
getApplicationContext(),
R.layout.spinner,
deparments
);
deptAdapter.setDropDownViewResource(
R.layout.spinner
);
spinner.setAdapter(deptAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Log.i("Selected Item", "I AM INDEED WORKING");
filter((Department)spinner.getSelectedItem());
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
});