只有当我点击搜索按钮时,才能通过WEB将Filter应用于ListView。但我希望在没有单击“搜索”按钮的情况下填充EditText时过滤数据。我使用两个editText:startdate和endDate。当我点击搜索按钮时,过滤器正在工作,但我希望过滤数据,而不管在editText中输入日期的顺序。
AttendanceStudentFragment
public class AttendanceStudentFragment extends Fragment {
ListView listView;
String Navigation_URL = "http://192.168.100.5:84/api/academics/attendance";
String Navigation_URL_FILTER = "http://192.168.100.5:84/api/academics/searchAttndByDate";
String master_id, date, status, remarks;
EditText editTextStartDate, editTextEndDate;
Button buttonSearch;
Calendar myCalendar = Calendar.getInstance();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.student_attendance_listview, container, false);
setHasOptionsMenu(true);
SessionManagement sessionManagement = new SessionManagement(getContext());
master_id = sessionManagement.getMasterId();
listView = (ListView) view.findViewById(R.id.list_student_attendance);
editTextStartDate = (EditText) view.findViewById(R.id.student_attendance_startDate);
editTextEndDate = (EditText) view.findViewById(R.id.student_attendance_endDate);
buttonSearch = (Button) view.findViewById(R.id.student_attendance_searchbutton);
// editTextStartDate.setInputType(InputType.TYPE_NULL);
// editTextStartDate.requestFocus();
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, month);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabelStartDate();
}
};
final DatePickerDialog.OnDateSetListener date1 = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, month);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabelEndDate();
}
};
editTextStartDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(getContext(), date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
// collegeAdmissionStartDatePicker.show();
editTextEndDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(getContext(), date1, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
getAttendanceData();
buttonSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAttendanceDataWithFilter();
}
});
if(editTextEndDate.length()!=0 && editTextStartDate.length()!=0){
getAttendanceDataWithFilter();
}
return view;
}
private void updateLabelStartDate() {
String myFormat = "MM/dd/yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
editTextStartDate.setText(sdf.format(myCalendar.getTime()));
//editTextEndDate.setText(sdf.format(myCalendar.getTime()));
}
private void updateLabelEndDate() {
String myFormat = "MM/dd/yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
// editTextStartDate.setText(sdf.format(myCalendar.getTime()));
editTextEndDate.setText(sdf.format(myCalendar.getTime()));
}
public void getAttendanceData() {
String URL = Navigation_URL + "?StdID=" + master_id;
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
ArrayList<StudentAttendancePojo> student_attendance_list = new ArrayList<>();
System.out.println(student_attendance_list.size());
JSONArray jArray = new JSONArray(response);
// studentFeeInformation = new StudentFeeInformation(response);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
date = jsonObject.getString("DateOfAttendance").substring(0, 10);
status = jsonObject.getString("STATUS");
remarks = jsonObject.getString("Remarks");
// student_list.add(new StudentFeeInformation(status, DateofReceiptIssued, ReceiptNumber, FeeReceivedDate));
//
//JSONArray jArray1 = jsonObject.getJSONArray("Description");
StudentAttendancePojo studentAttendancePojo = new StudentAttendancePojo(date, status, remarks);
System.out.println("total lengthArray" + jArray.length());
student_attendance_list.add(studentAttendancePojo);
}
System.out.println("student_list size:" + student_attendance_list.size());
StudentAttendanceAdapter studentAttendanceAdapter = new StudentAttendanceAdapter(getActivity(), student_attendance_list);
System.out.println(student_attendance_list.size());
listView.setAdapter(studentAttendanceAdapter);
} catch (JSONException e) {
System.out.println("This is not good");
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Toast.makeText(view.Fee.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
return headers;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}
public void getAttendanceDataWithFilter() {
// editTextStartDate.getText().toString();
// editTextEndDate.getText().toString();
// String URL = Navigation_URL_FILTER + "?StdID=" + master_id + "&fromDate=" + "2017-06-01" + "&toDate=" + "2017-06-10";
String URL = Navigation_URL_FILTER + "?StdID=" + master_id + "&fromDate=" + editTextStartDate.getText() + "&toDate=" + editTextEndDate.getText();
//String URL = Navigation_URL + "?fromDate=" + editTextStartDate + "&toDate" + editTextEndDate + "StdID" + master_id;
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
ArrayList<StudentAttendancePojo> student_attendance_list = new ArrayList<>();
System.out.println(student_attendance_list.size());
JSONArray jArray = new JSONArray(response);
// studentFeeInformation = new StudentFeeInformation(response);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
date = jsonObject.getString("DateOfAttendance").substring(0, 10);
status = jsonObject.getString("STATUS");
remarks = jsonObject.getString("Remarks");
// student_list.add(new StudentFeeInformation(status, DateofReceiptIssued, ReceiptNumber, FeeReceivedDate));
//
//JSONArray jArray1 = jsonObject.getJSONArray("Description");
StudentAttendancePojo studentAttendancePojo = new StudentAttendancePojo(date, status, remarks);
System.out.println("total lengthArray" + jArray.length());
student_attendance_list.add(studentAttendancePojo);
}
System.out.println("student_list size:" + student_attendance_list.size());
StudentAttendanceAdapter studentAttendanceAdapter = new StudentAttendanceAdapter(getActivity(), student_attendance_list);
System.out.println(student_attendance_list.size());
listView.setAdapter(studentAttendanceAdapter);
} catch (JSONException e) {
System.out.println("This is not good");
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Toast.makeText(view.Fee.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
return headers;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.dashboard, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
// do s.th.
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
如何解决这个问题?
答案 0 :(得分:3)
试试这个只需添加你的edittext的addTextChangedListener就可以帮到你..
editTextEndDate.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(editTextStartDate.getText.toString() != ""){
getAttendanceDataWithFilter();
}
}
});
答案 1 :(得分:0)
一:使用像OnFocusChangeListener这样的监听器,当Edittext失去焦点时,你可以刷新日期! 二:在Edittext上添加TextWatcher,当文本改变时,检查并更新日期!