带有listview的searchview错误结果

时间:2018-09-04 18:36:20

标签: android listview user-interface searchview

我在使用Searchview时遇到麻烦。我正在开发一个应用程序,其中使用自定义视图从列表中进行搜索。为此,我正在使用searchview和listview。我能够从列表视图中过滤出文本,但是当我单击此过滤视图时,它将返回结果作为列表视图位置,而不是从过滤后的列表位置返回。我正在使用简单的ArrayList而不是自定义适配器。我知道这将是一个简单的答案,但我在任何地方都找不到。

这是我的代码:

 public class activitySignUp extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener, SearchView.OnQueryTextListener {

        //  Tag for logs...
        private final String TAG = getClass().getName();

        //  Opens up Popup list for Selection...
        private AppCompatTextView StateInput;

        //  Popup view for Selection...
        private AlertDialog builder;

        //  For Custom Views...
        private ListView listView;
        private SearchView searchView;

        //  Created Different Types Method and Adapters for different kinds of classes...
        private ArrayList<State> StateList;

        private ArrayAdapter<State> StateAdapter;

        //  This class will be used for storing View ID of the view, by clicking on we opened up the
        //  custom alert dialog. This class will be useful in Updating the Clicked UI.
        private LastViewClicked lastViewClicked;

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

            initView();
        }

        private void initView() {
            SignUpBar = findViewById(R.id.SignUpBar);
            setSupportActionBar(SignUpBar);

            //  Inside context i am passing the context of the view according to switchview...
            //  intentRelatedTasks are for Passing intents...
            //  For showing error there's show Error...
            builder = new AlertDialog.Builder(activitySignUp.this).create();
            View custom_view = LayoutInflater.from(activitySignUp.this).inflate(R.layout.popup_view, null);

            searchView = custom_view.findViewById(R.id.search_bar);
            searchView.setIconifiedByDefault(false);

            listView = custom_view.findViewById(R.id.list_all);
            listView.setTextFilterEnabled(true);

            builder.setView(custom_view);

            //  Initializing ArrayLists and Setting them up on Adapters...
            StateList = new ArrayList<>();
            StateAdapter = new ArrayAdapter<>(activitySignUp.this, android.R.layout
                    .simple_list_item_1, StateList);

            listView.setOnItemClickListener(this);
            searchView.setOnQueryTextListener(this);

            //  TextView on Click Listeners...
            StateInput.setOnClickListener(this);

            //  Initializing class...
            lastViewClicked = new LastViewClicked();
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.stateInput:
                    //  Storing the ID of the Last Clicked Item, which will be used in updating the UI on
                    //  ListItem Click...
                    getStates();
                    listView.setAdapter(StateAdapter);
                    builder.show();
                    break;
            }
        }

        private void getStates() {
            CheruvuApi.getStates(new Callback<BasicResponse>() {
                @Override
                public void onResponse(Call<BasicResponse> call, Response<BasicResponse> response) {
                    Log.i(TAG, "GetStates");
                    Log.i(TAG, String.valueOf(response.isSuccessful()));
                    Log.i(TAG, GsonUtils.toGson(response.body()));
                    if (response.isSuccessful() && response.isSuccessful()) {
                        BasicResponse basicResponse = response.body();
                        String res = basicResponse.getResponse();
                        Type listType = new TypeToken<ArrayList<State>>() {
                        }.getType();
                        ArrayList list = GsonUtils.fromGson(res, listType);
                        updateList(StateList, list);
                        StateAdapter.notifyDataSetChanged();
                    }
                }

                @Override
                public void onFailure(Call<BasicResponse> call, Throwable t) {
                    Alerts.showToast(getApplication(), getResources().getString(R.string.NetworkError), Toast.LENGTH_SHORT);
                    Log.i(TAG, t.getMessage());
                }
            });
        }


        private void updateList(ArrayList arrayList, ArrayList arrayList_new) {
            Log.d(TAG, "updateList: " + arrayList_new);
            arrayList.clear();

            if (arrayList_new != null)
                arrayList.addAll(arrayList_new);
        }

        //  Listview function that performs Action on OnItemClick...
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            Log.d(TAG, "onListItemClick : " + position);
            Function(position);
        }

        private void Function(int position){
            switch (lastViewClicked.getView().getId()) {
                case R.id.stateInput:
                    getDistrictsByStateId(StateList.get(position).getId());
                    break;
            }
            searchView.setQuery("", false);
        }

        @Override
        public boolean onQueryTextSubmit(String s) {
            switch (lastViewClicked.getView().getId()) {
                case R.id.stateInput:
                    StateAdapter.notifyDataSetChanged();
                    break;
            }
            return true;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            switch (lastViewClicked.getView().getId()) {
                case R.id.stateInput:
                    StateAdapter.getFilter().filter(s);
                    break;
            }
            return true;
        }
    }

它显示并过滤状态列表中的项目,但是当我单击项目时,它将以Listview位置而不是过滤后的列表位置获取数据。

0 个答案:

没有答案