这是我的带有搜索功能的主页:
public class Admin extends AppCompatActivity {
ListView _admin_listview;
Admin_Adapter AA;
Homepage_ListView homepageListView;
private final Handler handler = new Handler();
public static ArrayList<Homepage_ListView> UserArrayList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Admin Homepage");
_admin_listview = (ListView) findViewById(R.id.admin_listview);
retrieveData();
doTheAutoRefresh();
AA = new Admin_Adapter(this, UserArrayList);
_admin_listview.setAdapter(AA);
_admin_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, final int position, final long Username) {
startActivity(new Intent(Admin.this, job_detail_admin.class).putExtra("position", position));
}
});
AA.notifyDataSetChanged();
//Initialise and assign variable
BottomNavigationView bottomNavigationView = findViewById(R.id.bot_navigator);
//Set Home Select
bottomNavigationView.setSelectedItemId(R.id.nav_home);
//Perform ItemSelectedListener
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_home:
return true;
case R.id.nav_add_user:
startActivity(new Intent(getApplicationContext(), add_user_function.class));
overridePendingTransition(0,0);
return true;
case R.id.nav_add_job:
startActivity(new Intent(getApplicationContext(), add_job_function.class));
overridePendingTransition(0,0);
return true;
case R.id.nav_logout:
startActivity(new Intent(getApplicationContext(), MainActivity.class));
overridePendingTransition(0,0);
return true;
}
return false;
}
});
}
public void retrieveData() {
StringRequest request = new StringRequest(Request.Method.POST, "http://localhost/taskapp00/User_joblist.php", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
UserArrayList.clear();
try{
JSONObject jObject = new JSONObject(response);
String success = jObject.getString("success");
JSONArray jArray = jObject.getJSONArray("phpjob");
if(success.equals("1")) {
for(int i=0;i<jArray.length();i++) {
JSONObject object = jArray.getJSONObject(i);
String username = object.getString("Username");
String usertype = object.getString("Usertype");
String house = object.getString("House");
String assignment = object.getString("Assignment");
String location = object.getString("Location");
String tenant_tel_no = object.getString("Tenant_tel_no");
String job = object.getString("Job");
String location_in_house = object.getString("Location_in_house");
String message = object.getString("Message");
String status = object.getString("Status");
String confirmation = object.getString("Confirmation");
String image0 = object.getString("Image0");
String image1 = object.getString("Image1");
String image2 = object.getString("Image2");
homepageListView = new Homepage_ListView(username, usertype, assignment, house, location, tenant_tel_no, job, location_in_house, message, status, confirmation, image0, image1, image2);
UserArrayList.add(homepageListView);
AA.notifyDataSetChanged();
}
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
Toast.makeText(getApplicationContext(), "Communication Error!", Toast.LENGTH_SHORT).show();
} else if (error instanceof AuthFailureError) {
Toast.makeText(getApplicationContext(), "Authentication Error!", Toast.LENGTH_SHORT).show();
} else if (error instanceof ServerError) {
Toast.makeText(getApplicationContext(), "Server Side Error!", Toast.LENGTH_SHORT).show();
} else if (error instanceof NetworkError) {
Toast.makeText(getApplicationContext(), "Network Error!", Toast.LENGTH_SHORT).show();
} else if (error instanceof ParseError) {
Toast.makeText(getApplicationContext(), "Parse Error!", Toast.LENGTH_SHORT).show();
}
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_function, menu);
//E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) searchItem.getActionView();//returns the object of class that is specified within the "actionviewclass"
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
//it gets called with every new IP string. newText=Ip string
ArrayList<Homepage_ListView> results = new ArrayList<>();
for (Homepage_ListView x: UserArrayList) {
if (x.getUsername().toLowerCase().contains(newText) || x.getUsertype().toLowerCase().contains(newText) || x.getLocation().toLowerCase().contains(newText))
results.add(x);
}
((Admin_Adapter)_admin_listview.getAdapter()).update(results);//refresh listview
return false;
}
});
return true;
}
private void doTheAutoRefresh() {
if (UserArrayList!=null && UserArrayList.size()>0) {
UserArrayList.clear();
}
Runnable refresh = new Runnable() {
@Override
public void run() {
retrieveData();
handler.postDelayed(this, 1000);
}
};
handler.postDelayed(refresh, 1000);
}
}
这是我的适配器:
public class Admin_Adapter extends ArrayAdapter<Homepage_ListView> implements Filterable {
List<Homepage_ListView> arrayhpJobList;
public CardView _admin_cardview;
public Admin_Adapter(@NonNull Context context, List<Homepage_ListView> arrayhpJobList) {
super(context, R.layout.cardview_admin, arrayhpJobList);
this.arrayhpJobList = arrayhpJobList;
notifyDataSetChanged();
}
//**must have for search function**
@Override
public int getCount() {//return the no. of list items
return arrayhpJobList.size();
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
//inflate layout and pass
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_admin, null, true);
_admin_cardview = view.findViewById(R.id.admin_cardview);
TextView _house = view.findViewById(R.id.House_AL);
TextView _username = view.findViewById(R.id.Username_AL);
TextView _assigned = view.findViewById(R.id.Assigned_AL);
if (Admin.UserArrayList.get(position).getStatus().matches("1") && Admin.UserArrayList.get(position).getConfirmation().matches("0")) {
_admin_cardview.setCardBackgroundColor(0xFFFFFF8D);
}
else if (Admin.UserArrayList.get(position).getStatus().matches("1") && Admin.UserArrayList.get(position).getConfirmation().matches("1")) {
_admin_cardview.setCardBackgroundColor(0xFFFA5D50);
}
else if (Admin.UserArrayList.get(position).getConfirmation().matches("2")) {
_admin_cardview.setCardBackgroundColor(0xFFCCFF90);
}
else {
_admin_cardview.setCardBackgroundColor(0xFFE7E7E7);
}
_house.setText(" House: " + arrayhpJobList.get(position).getHouse());
_username.setText(" Assigner: " + arrayhpJobList.get(position).getUsername());
_assigned.setText(" Assigned To: " + arrayhpJobList.get(position).getAssignment());
return view;
}
public void update(ArrayList<Homepage_ListView> results) {
arrayhpJobList = new ArrayList<>();
arrayhpJobList.addAll(results);
notifyDataSetChanged();
}
}
搜索功能能够整理/获取我需要的项目,但是...单击它会获取不正确的项目。例如,搜索输入之前的列表视图中有 6 个项目 [1,2,3,4,5,6]。在搜索输入之后,我们说“5”。它仅在列表视图中显示“5”,其他都消失了。单击它后,它会给我项目“1”。我认为它根据列表视图中“5”的位置获取项目,如何解决这个问题?