Android:当我点击十字按钮时,searchView不会删除文本

时间:2016-03-08 12:23:28

标签: android listview searchview

这是searchView和listView的图片。 当我开始搜索标题: Adeeieva 时,它会更新listView。 但是,当我点击十字按钮时,它不会将listView挂在那里。 我想要的是,当我点击十字按钮时,它会重新创建listView并带来其初始状态。 有谁可以帮助我吗? enter image description here enter image description here

public class MainActivity extends ActionBarActivity {

    ListView listView;
    Books booksObj;
    BooksAdapter booksAdapter;
    BooksAdapter_1 mBooksAdapter;
    String url="http://google.com";
    Gson gson;
    AsyncHttpClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView=(ListView)findViewById(R.id.listView);
        client=new AsyncHttpClient();
        client.get(MainActivity.this, url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                String responsestr=new String(responseBody);
                gson=new Gson();

                booksObj=gson.fromJson(responsestr, Books.class);

                mBooksAdapter= new BooksAdapter_1(MainActivity.this, booksObj.getBooks());
                listView.setAdapter(mBooksAdapter);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });

        listView.setOnItemClickListener( new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                TextView description=(TextView)view.findViewById(R.id.book_description);
                TextView title_= (TextView) view.findViewById(R.id.book_title);
                TextView isbn_= (TextView) view.findViewById(R.id.book_isbn);



                String _title=title_.getText().toString();
                String _isbn=isbn_.getText().toString();
                String _description=description.getText().toString();

                Intent myIntent = new Intent(MainActivity.this, ResultActivity.class);
                myIntent.putExtra("title", _title);
                myIntent.putExtra("isbn", _isbn);
                myIntent.putExtra("description", _description);

                startActivity(myIntent);

            }
        });

        SearchView sv= (SearchView)findViewById(R.id.search);
        sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String query) {
                mBooksAdapter.getFilter().filter(query);

                return false;
            }
        });

    }

}

BookAdapter_1

public class BooksAdapter_1 extends BaseAdapter implements Filterable {
    private List<Books.BooksEntity> mBooksitem;
    private LayoutInflater inflater;
    private Context mContext;
    BooksFilter mBooksFilter;

    public BooksAdapter_1(Context context, List<Books.BooksEntity> mBooksitem){
        this.mContext= context;
        this.mBooksitem= mBooksitem;
    }

    @Override
    public int getCount() {
        return mBooksitem.size();
    }

    @Override
    public Object getItem(int position) {
        return mBooksitem.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater=(LayoutInflater)mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView=inflater.inflate(R.layout.book_layout_item, parent, false);

        Books.BooksEntity item=(Books.BooksEntity)getItem(position);
        TextView id=(TextView)rowView.findViewById(R.id.book_id);
        TextView title=(TextView)rowView.findViewById(R.id.book_title);
        TextView description=(TextView)rowView.findViewById(R.id.book_description);
        TextView isbn=(TextView)rowView.findViewById(R.id.book_isbn);
        ImageView book_url=(ImageView)rowView.findViewById(R.id.book_url);

        id.setText(String.valueOf(item.getId()));
        title.setText("Title: "+item.getTitle());
        description.setText("Description: "+item.getDescription());
        isbn.setText("isbn: "+item.getIsbn());

        String imageUrl=item.getCover_url();
        Picasso.with(mContext).load(imageUrl).into(book_url);

        return rowView;
    }

    @Override
    public Filter getFilter() {
        if(mBooksFilter==null)
            mBooksFilter= new BooksFilter();
        return mBooksFilter;
    }

    private class BooksFilter extends Filter{
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults results= new FilterResults();

            if(constraint==null || constraint.length()==0){
                results.values=mBooksitem;
                results.count=mBooksitem.size();
            } else {
                ArrayList<Books.BooksEntity> filteredBooks= new ArrayList<Books.BooksEntity>();

                for(Books.BooksEntity b: mBooksitem){
                    if(b.getDescription().toUpperCase().contains(constraint.toString().toUpperCase())){
                        filteredBooks.add(b);
                    }
                }
                results.values=filteredBooks;
                results.count=filteredBooks.size();
            }

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            mBooksitem=(ArrayList<Books.BooksEntity>) results.values;
            notifyDataSetChanged();


        }
    }

}

1 个答案:

答案 0 :(得分:1)

您必须为关闭(x)按钮声明操作

如果您想清除搜索文字,只需将搜索查询设置为&#34;&#34; =无。

您还必须重置自定义适配器过滤器通知它已更改某些内容,在本例中为搜索查询。

sv.setOnCloseListener(new OnCloseListener() {
            @Override
            public boolean onClose() {

            sv.setQuery("", false);

            mBooksAdapter.getFilter().filter(sv.getQuery());

            mBooksAdapter.notifyDataSetChanged();

            return false;
            }
        });