我在这里对Android编程比较陌生,而且我仍处于观看教程和参考互联网资料的阶段,所以请原谅我,如果我听起来很吵! 我已经为我的公司应用程序创建了一个带有排序/过滤器的菜单,以使用gridLayoutManager在recyclerView中显示人员数据。 使用Collections.sort(...)进行数据排序效果很好,但是当我尝试使用复选框进行过滤时,我显示的gridView再次搞砸了。 截图下方是我在appBar中的过滤器/排序菜单。 我还在这里附上了我的全部代码。
我的过滤器菜单:
尝试实现:在管理部门中按职员的升序/降序排序。
public class MainPageFragment extends Fragment implements SortListener {
private RecyclerViewAdapter recyclerViewAdapter;
private RecyclerView recyclerView;
private VolleySingleton volleySingleton;
private ImageLoader imageLoader;
private RequestQueue requestQueue;
private AlertDialog.Builder filterDialog;
private ArrayList<Employee> employeeList = new ArrayList<>();
private static final String infoUrl = "http://xxxxxxx.com/";
private static final String imageUrl = "http://xxxxxxx.com/";
private boolean sortTag;
public static boolean nameCheck, ageCheck, idCheck;
private TextView sortButton;
private CheckBox adminChk, logisChk, salesChk;
private RadioButton nameButton, ageButton, idButton;
private EmployeeSorter sorter = new EmployeeSorter();
int employeeAge = 0; int employeeId = 0;
String employeeName, employeeDept, isAdmin, isLogistics, isSales;
public MainPageFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
final View view = inflater.inflate(R.layout.fragment_main_page, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.main_grid_recycler);
recyclerViewAdapter = new RecyclerViewAdapter(getActivity());
recyclerView.setAdapter(recyclerViewAdapter);
recyclerView.setLayoutManager(new GridLayoutManager(null, 5));
recyclerViewAdapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
@Override
public void onItemClick(View itemView, int position) {
long currEmpId = recyclerViewAdapter.getItemId(position);
EmployeeDetailsFragment fragmentSub = new EmployeeDetailsFragment();
Bundle bundle = new Bundle();
bundle.putLong("empID", currEmpId);
fragmentSub.setArguments(bundle);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.main_page_fragment, fragmentSub);
recyclerView.setVisibility(View.INVISIBLE);
transaction.addToBackStack(null);
transaction.commit();
}
});
volleySingleton = VolleySingleton.getsInstance();
requestQueue = volleySingleton.getRequestQueue();
sendJsonRequest();
return view;
}
private void sendJsonRequest(){
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, infoUrl,
(JSONObject) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
employeeList = parseJsonResponse(response);
recyclerViewAdapter.setEmployeeList(employeeList);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(request);
}
private ArrayList<Employee> parseJsonResponse(JSONObject response) {
ArrayList<Employee> employeeList = new ArrayList<>();
if (response !=null && response.length() > 0) {
// cast all the characters ID in the json file into jsonArray
JSONArray idArray = response.names();
for (int i=0; i< idArray.length(); i++){
try {
long employeeId = idArray.getLong(i);
JSONObject currentObject = response.getJSONObject(String.valueOf(employeeId));
JSONObject emp_Name = currentObject.getJSONObject("name");
employeeName = emp_Name.getString("value");
JSONObject emp_age = currentObject.getJSONObject("age");
employeeAge = emp_age.getInt("value");
String thumbUrl = null;
if (currentObject.has("photo")){ thumbUrl = currentObject.getString("photo"); }
employeeDept = currentObject.getString("dept");
if (employeeDept.equals(isAdmin) || employeeDept.equals(isLogistics) ||
employeeDept.equals(isSales) || employeeDept.equals("")){
Employee employee = new Employee();
employee.setId(employeeId);
employee.setName(employeeName);
employee.setAge(employeeAge);
employee.setUrlThumbnail(imageUrl + thumbUrl + ".png");
employee.setDepartment(employeeDept);
employeeList.add(employee);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
return employeeList;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
} else if (id == R.id.action_filter) {
callFilterAlert();
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.main_frag, menu);
}
private void callFilterAlert() {
View inflater = getLayoutInflater(null).inflate(R.layout.layout_filter, null);
sortButton = (TextView) inflater.findViewById(R.id.filter_sort_button);
nameButton = (RadioButton) inflater.findViewById(R.id.filter_radio_name);
ageButton = (RadioButton) inflater.findViewById(R.id.filter_radio_age);
idButton = (RadioButton) inflater.findViewById(R.id.filter_radio_id);
adminChk = (CheckBox) inflater.findViewById(R.id.filter_dept_admin);
logisChk = (CheckBox) inflater.findViewById(R.id.filter_dept_logistics);
salesChk = (CheckBox) inflater.findViewById(R.id.filter_dept_sales);
setSortListeners();
filterDialog = new AlertDialog.Builder(getActivity());
filterDialog.setView(inflater)
.setTitle("Choose")
.setNegativeButton("Back", null)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (nameButton.isChecked()){
sortByName();
} else if (ageButton.isChecked()){
sortByAge();
} else if (idButton.isChecked()){
sortById();
}
sendJsonRequest();
}
});
AlertDialog alertDialog = filterDialog.create();
alertDialog.show();
}
private void setFilterCriteria(){
if (adminChk.isChecked()){
isAdmin = "Admin";
} else {
isAdmin = "";
}
if (logisChk.isChecked()){
isLogistics = "Logistics";
} else {
isLogistics = "";
}
if (salesChk.isChecked()){
isSales = "Sales";
} else {
isSales = "";
}
}
private void setSortListeners() {
nameButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nameButton.setChecked(true);
ageButton.setChecked(false);
idButton.setChecked(false);
nameCheck = true;
ageCheck = false;
idCheck = false;
}
});
ageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nameButton.setChecked(false);
ageButton.setChecked(true);
idButton.setChecked(false);
nameCheck = false;
ageCheck = true;
idCheck = false;
}
});
idButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nameButton.setChecked(false);
ageButton.setChecked(false);
idButton.setChecked(true);
nameCheck = false;
ageCheck = false;
idCheck = true;
}
});
// setup for the ascending/descending tag in the sort menu
sortTag = false;
sortButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sortButton.getText() == "Ascending") {
sortButton.setText("Descending");
sortTag = false;
} else {
sortButton.setText("Ascending");
sortTag = true;
}
}
});
}
@Override
public void sortByName() {
sorter.sortEmpByName(employeeList);
}
@Override
public void sortByAge() {
sorter.sortEmpByAgeAscending(employeeList);
}
@Override
public void sortById() {
sorter.sortEmpByIdAscending(employeeList);
}
}
在callFilterAlert()方法中,我再次调用函数sendJsonRequest(),开始时是正确的还是请求jsonFile只应该在onCreate()期间执行一次?
我无法找到有关如何在互联网上过滤数据的任何教程,任何人都可以指导我在代码中出错的地方或我可以获得有关此主题的相关信息吗?