-6

我创建了一个列表视图。list 的每一行都有两个字段。一个是图像,另一个是书名。我如何在 android 中的一个列表字段上应用搜索功能。我添加了一个编辑文本文本。

setContentView(R.layout.activity_my_purchase);

    TextView headerText = (TextView) findViewById(R.id.heading);
    headerText.setText(R.string.my_purchase);

    ImageView refreshImg = (ImageView) findViewById(R.id.img_right);
    refreshImg.setImageResource(R.drawable.refresh);

    ImageView searchImg = (ImageView) findViewById(R.id.img_middle);
    searchImg.setImageResource(R.drawable.top_icn_search);
    searchImg.setOnClickListener(searchImageClickListener);

    searchBox = (EditText) findViewById(R.id.searchBox);

    ImageView imgGidIcon = (ImageView) findViewById(R.id.img_left);
    imgGidIcon.setImageResource(R.drawable.top_icn_grid);
    imgGidIcon.setOnClickListener(imgGidIconClickListener);

    // Restore preferences
    SharedPreferences settings = EBookReaderApplication.getApplicationInstance().getAppPreferences();
    String userName = settings.getString(Constants.TAG_USERNAME, null);

    new PurchasedBooksListTask(MyPurchasesActivity.this).execute(
            Constants.API_URL, userName);

    // Set list view to the screen
    bookListView = (ListView) findViewById(R.id.list_books);

    bookListView.setOnItemClickListener(bookListItemClickListener);
    searchResults = new ArrayList<HashMap<String, Object>>(originalValues);
    searchBox.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2,
                int arg3) {

            String searchString = searchBox.getText().toString().trim();
            int textLength = searchString.length();
            listAdapter.clear();
            // When user changed the Text
        /// MyPurchasesActivity.this.listAdapter.getFilter().filter(cs);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });
}

/**
 * 
 */
private OnClickListener imgGidIconClickListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent = new Intent().setClass(MyPurchasesActivity.this,
                LibraryActivity.class);
        startActivity(intent);
    }
};

/**
 * 
 */
private OnClickListener searchImageClickListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        searchBox.setVisibility(View.VISIBLE);

    }
};

private OnItemClickListener bookListItemClickListener = new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arview, View parent,
            int position, long id) {

    }
};

/**
 * 
 * @author Manoj
 * 
 */
private class PurchasedBooksListTask extends
        AsyncTask<String, Void, Boolean> {

    /** progress dialog to show user that the backup is processing. */
    private ProgressDialog dialog;

    /* Constructor for the Asynchronous task */
    public PurchasedBooksListTask(MyPurchasesActivity activity) {
        dialog = new ProgressDialog(activity);
    }

    @Override
    protected void onPreExecute() {
        dialog.setMessage(getResources().getString(R.string.loading));
        dialog.show();
    }

    @Override
    protected Boolean doInBackground(String... params) {
        try {
            listPurchasedBooks = new JSONReader()
                    .getPurchasedBooksList(new JSONParser()
                            .getJSONFromList(params[0], params[1]));

            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if (result) {
            dialog.dismiss();
            // Sets the adapter to the list view
            if (listPurchasedBooks != null && listPurchasedBooks.size() > 0) {
                listAdapter = new PurchasedBookListAdapter(
                        MyPurchasesActivity.this, listPurchasedBooks);
                bookListView.setAdapter(listAdapter);
            }
        }

    }
}

}

4

1 回答 1

0

使用setTextFilterEnabled(boolean)

或者

您可以定义自己的过滤器,如在此处描述。

于 2013-06-05T09:29:16.033 回答