我想在Android中创建搜索功能

时间:2015-07-01 06:15:42

标签: android json

我可以在Android中搜索第一次搜索,但在编辑框中按下后,搜索将无法工作ID不会显示第二个搜索结果。

请解决我的问题。结果函数给我搜索结果:

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
                                long id) {


            // We know the View is a <extView so we can cast it
            TextView clickedView = (TextView) view;

            Toast.makeText(Search.this, "Item with id [" + id + "] - Position [" + position + "] - Planet [" + clickedView.getText() + "]", Toast.LENGTH_SHORT).show();

        }
    });
    Runnable r2 = new Runnable() {
        @Override
        public void run() {
            finish();
            overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        }
    };
    Runnable r = new Runnable() {
        public void run() {

            finish();
            overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        }
    };
    // we register for the contextmneu
    registerForContextMenu(listView);

    // TextFilter
    listView.setTextFilterEnabled(true);
    ev1.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            pw.setVisibility(View.VISIBLE);
            System.out.println("Text [" + s + "] - Start [" + start + "] - Before [" + before + "] - Count [" + count + "]");
            if (count < before) {
                // We're deleting char so we need to reset the adapter data
               // listView.setAdapter(null);
                //adapter.resetData();
                adapter.getFilter().filter(s.toString());
                adapter.notifyDataSetChanged();
            }



        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            ;
        }

        public void afterTextChanged(Editable s) {
            searchre = ev1.getText().toString();
            results();

        }


    });
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            searchre = ev1.getText().toString();

            Toast.makeText(getApplicationContext(), "" + searchre, Toast.LENGTH_LONG).show();

            //listView.setAdapter(null);
           // results();


        }
    });

}

`

1 个答案:

答案 0 :(得分:0)

这只是因为如果文本被删除,你不会搜索。删除以下if语句

if (count < before)

并始终致电

adapter.getFilter().filter(s.toString());
adapter.notifyDataSetChanged();