gpt4 book ai didi

java - Spinner OnItemSelected 事件未被调用

转载 作者:行者123 更新时间:2023-12-02 05:51:09 26 4
gpt4 key购买 nike

我使用微调器中的值来帮助过滤将成为 RecyclerView 数据源的列表。当我使用微调器时,OnItemSelect 方法似乎永远不会被调用

它是一个 Android 应用程序,用 Java 构建,使用 Pie 作为操作系统。该项目是使用基本 Activity 模板创建的。

Activity 类

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);
}

}

应该发生的是,当事件被调用时,它会过滤列表,删除列表中不属于特定部门的任何人,但事件永远不会被调用。

最佳答案

你尝试放入 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) {

}

});

关于java - Spinner OnItemSelected 事件未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56047352/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com