ListView中的SearchView不起作用

时间:2016-12-11 16:40:24

标签: android listview searchview

我不知道为什么,但它无法在我的ListView中搜索:(请,帮助......

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_users);

    usersList = (ListView)findViewById(R.id.usersList);
    noUsersText = (TextView)findViewById(R.id.noUsersText);
    searchView = (SearchView)findViewById(R.id.searchView);

    pd = new ProgressDialog(Users.this);
    pd.setMessage("Загрузка...");
    pd.show();

    String url = "https://android-messenger-2f255.firebaseio.com/users.json";

    StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>(){
        @Override
        public void onResponse(String s) {
            doOnSuccess(s);
        }
    },new Response.ErrorListener(){
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            System.out.println("" + volleyError);
        }
    });

    RequestQueue rQueue = Volley.newRequestQueue(Users.this);
    rQueue.add(request);

    usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            UserDetails.chatWith = al.get(position);
            startActivity(new Intent(Users.this, Chat.class));
        }
    });

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, al);
    usersList.setAdapter(adapter);

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            adapter.getFilter().filter(newText);

            return false;
        }
    });
}



public void doOnSuccess(String s){
    try {
        JSONObject obj = new JSONObject(s);

        Iterator i = obj.keys();
        String key = "";

        while(i.hasNext()){
            key = i.next().toString();

            if(!key.equals(UserDetails.username)) {
                al.add(key);
            }

            totalUsers++;
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

    if(totalUsers <=1){
        noUsersText.setVisibility(View.VISIBLE);
        usersList.setVisibility(View.GONE);
    }
    else{
        noUsersText.setVisibility(View.GONE);
        usersList.setVisibility(View.VISIBLE);
        usersList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, al));
    }

    pd.dismiss();
}





@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.action_log_out:
            Intent Logout = new Intent(this,Login.class);
            Logout.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(Logout);
            return true;
        case R.id.action_refresh:
            Intent Refresh = new Intent(this,Users.class);
            Refresh.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(Refresh);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public void onBackPressed (){
    moveTaskToBack(true);
}

XML:

<SearchView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:queryHint="Поиск..."
    android:layout_marginBottom="10dp"
    android:background="@drawable/edittextstyle"
    android:id="@+id/searchView" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Пока пользователей нет:("
    android:id="@+id/noUsersText"
    android:visibility="gone"/>

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/usersList"
    android:background="@drawable/edittextstyle"
    android:headerDividersEnabled="false"
    android:footerDividersEnabled="false" />

我现在不知道为什么,但如果我在SearchView中写任何文字,没有任何反应:( 这是我的代码,如果可以的话,请找错误:))

0 个答案:

没有答案