Arrays 搜索功能无法获取正确的项目位置

Arrays 搜索功能无法获取正确的项目位置,arrays,android-studio,sorting,listview,search,Arrays,Android Studio,Sorting,Listview,Search,这是我的主页,具有搜索功能: public class Admin extends AppCompatActivity { ListView _admin_listview; Admin_Adapter AA; Homepage_ListView homepageListView; private final Handler handler = new Handler(); public static ArrayList<Homepage_Lis

这是我的主页,具有搜索功能:

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);
    }
}
公共类管理扩展AppCompative活动{
ListView _admin_ListView;
管理适配器AA;
主页\列表视图主页页面列表视图;
私有最终处理程序=新处理程序();
public static ArrayList UserArrayList=new ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
ActionBar ActionBar=getSupportActionBar();
actionBar.setTitle(“管理员主页”);
_admin_listview=(listview)findViewById(R.id.admin_listview);
检索数据();
doTheAutoRefresh();
AA=新的管理适配器(这是UserArrayList);
_admin_listview.setAdapter(AA);
_admin_listview.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView AdapterView、视图视图、最终整型位置、最终长用户名){
startActivity(新的意图(管理本、工作细节、管理类).putExtra(“职位”,职位));
}
});
AA.notifyDataSetChanged();
//初始化并分配变量
BottomNavigationView BottomNavigationView=findViewById(R.id.bot\u navigator);
//设置主页选择
bottomNavigationView.setSelectedItemId(R.id.nav_home);
//执行ItemSelectedListener
bottomNavigationView.setOnNavigationItemSelectedListener(新的bottomNavigationView.OnNavigationItemSelectedListener(){
@凌驾
公共布尔值onNavigationItemSelected(@NonNull MenuItem MenuItem){
开关(menuItem.getItemId()){
案例R.id.nav_主页:
返回true;
案例R.id.nav\u添加用户:
startActivity(新意图(getApplicationContext(),add_user_function.class));
覆盖转换(0,0);
返回true;
案例R.id.nav_添加_作业:
startActivity(新意图(getApplicationContext(),add_job_function.class));
覆盖转换(0,0);
返回true;
案例R.id.nav_注销:
startActivity(新意图(getApplicationContext(),MainActivity.class));
覆盖转换(0,0);
返回true;
}
返回false;
}
});
}
公共无效检索数据(){
StringRequest=新的StringRequest(request.Method.POST)http://localhost/taskapp00/User_joblist.php,新的响应。侦听器(){
@凌驾
公共void onResponse(字符串响应){
UserArrayList.clear();
试一试{
JSONObject jObject=新JSONObject(响应);
String success=jObject.getString(“success”);
JSONArray jArray=jObject.getJSONArray(“phpjob”);
如果(成功等于(“1”)){
对于(int i=0;i0){
UserArrayList.clear();
}
Runnable refresh=new Runnable(){
@凌驾
公开募捐{
检索数据();
handler.postDelayed(这个,1000);
}
};
handler.postDelayed(刷新,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();
    }
}

公共类管理_适配器扩展ArrayAdapter实现可过滤{
工作清单;
公共CardView _admin_CardView;
公共管理适配器(@NonNull上下文上下文,列表arrayhpJobList){
超级(上下文,R.layout.cardwiew_admin,arrayhpJobList);
this.arrayhpJobList=arrayhpJobList;
notifyDataSetChanged();
}
//**必须具有搜索功能**
@凌驾
public int getCount(){//返回列表项的数量
返回arrayhpJobList.size();
}
@非空
@凌驾
公共视图getView(int位置,@Nullable视图convertView,@NonNull视图组父级){
//充气布局和通道
View View=LayoutInflater.from(parent.getContext()).flate(R.layout.cardwiew_admin,null,true);
_admin\u cardwiew=view.findviewbyd(R.id.admin\u cardwiew);
TextView _house=view.findViewById(R.id.house_AL);
TextView\u username=view.findviewbyd(R.id.username\u AL);
TextView\u assigned=view.findviewbyd(R.id.assigned\u AL);
if(Admin.UserArrayList.get(position).getStatus()匹配(“1”)和&Admin.UserArrayList.get(position).getConfirmation()匹配(“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);
}
否则{
_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());
返回视图;
}
公共作废更新(ArrayList结果){
arrayhpJobList=新的ArrayList();
arrayhpJobList.addAll(结果);
notifyDataSetChanged();
}
}
搜索功能能够排序/获取我需要的项目,但是…单击它会获取不正确的项目。例如,在搜索输入之前的listview中有6个项目[1,2,3,4,5,6]。在搜索输入之后,让我们说“5”。它