如何使用“addTextChangedListener(new TextWatcher()”将字符串解析为Json feed Url,以搜索editText上用户键入的任何Item

时间:2016-01-21 22:01:29

标签: android json search textwatcher rotten-tomatoes

MainActivity中的loadToolBar方法

public void loadToolBarSearch() {


        /*ArrayList<String> countryStored = SharedPreference.loadList(Search_Activity.this, Utils.PREFS_NAME, Utils.KEY_COUNTRIES);*/

        View view = MainActivity.this.getLayoutInflater().inflate(R.layout.view_toolbar_search, null);
        LinearLayout parentToolbarSearch = (LinearLayout) view.findViewById(R.id.parent_toolbar_search);
        ImageView imgToolBack = (ImageView) view.findViewById(R.id.img_tool_back);
        final EditText edtToolSearch = (EditText) view.findViewById(R.id.edt_tool_search);
        ImageView imgToolMic = (ImageView) view.findViewById(R.id.img_tool_mic);
        /*final ListView listSearch = (ListView) view.findViewById(R.id.list_search);*/
        /*final TextView txtEmpty = (TextView) view.findViewById(R.id.txt_empty);*/

        /*Utils.setListViewHeightBasedOnChildren(listSearch);*/

        edtToolSearch.setHint("Search your country");

        final Dialog toolbarSearchDialog = new Dialog(MainActivity.this, R.style.MaterialSearch);
        toolbarSearchDialog.setContentView(view);
        toolbarSearchDialog.setCancelable(false);
        toolbarSearchDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        toolbarSearchDialog.getWindow().setGravity(Gravity.BOTTOM);
        toolbarSearchDialog.show();

        toolbarSearchDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);


        edtToolSearch.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) {
edtToolSearch.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {


    }
});

            }
        });

        imgToolBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toolbarSearchDialog.dismiss();
            }
        });

        imgToolMic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                edtToolSearch.setText("");

            }
        });

Search UrlClass

  public class Endpoints   {

    public static String getRequestUrlSearchMovies(int limit) {


        return "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=54wzfswsa4qmjg8hjwa64d4c&q="+"**StringToBeSearched**" +"&limit=30";

    }
}

这是执行所有搜索功能的地方

public class MovieUtils {

    public static ArrayList<Movie> loadSearchMovies(RequestQueue requestQueue) {
        JSONObject response = Requestor.sendRequestBoxOfficeMovies(requestQueue, Endpoints.getRequestUrlSearchMovies(30));
        ArrayList<Movie> listMovies = Parser.parseMoviesJSON(response);
        /*MyApplication.getWritableDatabase().insertMovies(MovieDatabase.UPCOMING, listMovies, true);*/
        return listMovies;
    }
}

我需要知道如何使这段代码工作,以便当用户输入要搜索的项目时,搜索将在键入时触发

  

(动态搜索)

,以及如何在用户按

后搜索项目
  

DONE

键盘上的

。 有没有更好的想法如何使这个搜索逻辑工作? 先感谢您。

1 个答案:

答案 0 :(得分:0)

这里有一些严重的问题!您应该删除onClickListener生成。您应该只响应文本更改事件:

edtToolSearch.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) {
           //DO SEARCH HERE (MAY NEED TO LIMIT HTTP REQUESTS THOUGH
       }

        @Override
        public void afterTextChanged(Editable s) {
           //OR DO SEARCH HERE AFTER USER STOPS TYPING (LESS HTTP REQUESTS),
        }

   });