我有一个搜索视图,它在类 basesearch.java 中定义如下:
protected EditText mSearchView;
(它指的是基本的文本框搜索视图)。
Now in class searchfragment.java, I have the following method:
private void runSearch(final String searchTerm) {
if(searchTerm.contains(mSearchView.getText().toString().toLowerCase() + " " + mSearchView.getText().toString().toLowerCase())){
AWebViewFragment.newInstanceForSearch(getFragmentManager(), null, null);
}else {
SearchResultsFragment.newInstance(getFragmentManager(), searchTerm);
}
}
注意: mSearchView.getText().toString().toLowerCase() 正在获取输入的搜索值。现在,我想使用这个值“mSearchView.getText().toString().toLowerCase()”并将其传递给在第一个 if 子句中称为“AWebViewFragment”的另一个类。我将 AwebViewfragment 定义为:
public static String OUTPUT_ENVIRONMENT = "https://mobile13.cp.com/msf1.0/fwd/answers/service/v1/?q="+mSearchView.getText().toString().toLowerCase()+"%20revenue&ui.theme=novadark&uuid=PADACT-002&userAgent=iphone";
但是它会在 mSearchView 上引发错误,除非我设置了一些虚拟值来定义它。我希望它能够从以前的课程中获取输入的搜索词的信息。我想知道我做错了什么。我希望 url 与动态键入的搜索词一起显示。有可能以某种方式做同样的事情吗?随意修改代码并引导我完成新的更改,我将尝试任何有关新代码或改进的建议。非常感谢任何帮助。谢谢!贾斯汀另外,我已经在使用 Awebviewfragment 中的捆绑包:
private static AWebViewFragment __newInstance(final AnswersWebViewFragment fragment, final FragmentManager manager,
final String searchTerm, final String symbolType, int containerViewId, final int inAnimation, final int outAnimation, final int popInAnimation, final int popOutAnimation) {
final Bundle bundle = new Bundle();
bundle.putString(SEARCH_TERM, searchTerm);
bundle.putString(AnswersWebViewFragment.SYMBOL_TYPE, symbolType);
bundle.putInt(AnswersWebViewFragment.CONTAINER_ID, containerViewId);
fragment.setArguments(bundle);
FragmentInfo fragmentInfo = new FragmentInfo(TransactionMethods.ADD, containerViewId);
fragmentInfo.setAnimation(inAnimation, outAnimation);
fragmentInfo.setPopAnimation(popInAnimation, popOutAnimation);
fragmentInfo.setFragmentTag(TAG_A_FRAGMENT_WEBVIEW);
fragmentInfo.setActionBarTitle(Application.getAppResources().getString(R.string.nav_option_quotes));
FragmentStackManager.getInstance().transitionFragment(manager, fragment, fragmentInfo);
return fragment;
}
并且 OUTPUT_ENVIRONMENT 是在 bundle 方法之外定义的,我如何检索从上一个类传递的搜索词?和 oncreate 我正在使用:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSearchTerm = getArguments().getString(SEARCH_TERM);
}
整个代码如下:
AbssearchAutisuggestFragment:
public abstract class AbsSearchAutoSuggestFragment extends AbsLoaderFragment implements SearchAutoSuggestObserver, SearchAutoSuggestItemClickListener{
protected EditText mSearchView;
protected ListView mSearchListView;
private SearchAutoSuggestsTask mSearchAutoSuggestTask;
private List<SearchAutoSuggestRow> mSearchAutoSuggestRows;
public void startSearchAutoSuggestTask(final String searchTerm) {
killLastSearchAutoSuggestTask();
mSearchAutoSuggestTask = new SearchAutoSuggestsTask(this);
final String params[] = {searchTerm};
mSearchAutoSuggestTask.execute(params);
}
public boolean killLastSearchAutoSuggestTask() {
if (mSearchAutoSuggestTask != null) {
mSearchAutoSuggestTask.cancel();
return true;
}
return false;
}
@Override
public void onSearchAutoSuggestAvailable(final SearchAutoSuggests searchAutoSuggests) {
final Activity activity = getActivity();
if (activity == null) {
return;
}
mSearchAutoSuggestRows = populateSearchAutoSuggestRows(searchAutoSuggests);
final SearchAutoSuggestAdapter adapter = new SearchAutoSuggestAdapter(activity, R.layout.item_search_history_auto_suggest, mSearchAutoSuggestRows);
adapter.setOnItemClickedListener(this);
mSearchListView.setAdapter(adapter);
onHasResults();
}
private List<SearchAutoSuggestRow> populateSearchAutoSuggestRows(final SearchAutoSuggests searchAutoSuggests) {
final List<SearchAutoSuggestRow> searchAutoSuggestRows = new ArrayList<SearchAutoSuggestRow>();
for (final SearchAutoSuggest searchAutoSuggest : searchAutoSuggests.getSearchAutoSuggests()) {
final SearchAutoSuggestHeader header = new SearchAutoSuggestHeader(searchAutoSuggest.getName().toUpperCase());
searchAutoSuggestRows.add(header);
int i = 1;
for (final SearchAutoSuggestHits searchAutoSuggestHits : searchAutoSuggest.getHits()) {
final String id = searchAutoSuggestHits.getId();
final String title = searchAutoSuggestHits.getTitle();
final String subTitle = searchAutoSuggestHits.getSubtitle();
final String symbol = searchAutoSuggestHits.getSymbol();
final String st = searchAutoSuggestHits.getSt();
final Spanned formattedTitle = formatAutoSuggestText(title);
final Spanned formattedSymbol = formatAutoSuggestText(symbol);
final SearchAutoSuggestItem searchAutoSuggestItem = new SearchAutoSuggestItem(id, formattedTitle, subTitle, formattedSymbol,st);
searchAutoSuggestRows.add(searchAutoSuggestItem);
if(i==3){
break;
}
i++;
}
}
return searchAutoSuggestRows;
}
private Spanned formatAutoSuggestText(final String autoSuggestText) {
if (autoSuggestText == null) {
return Html.fromHtml("");
}
try {
String modifiedAutoSuggestText= "" ;
final String searchText = mSearchView.getText().toString();
final Pattern pattern = Pattern.compile(StringUtils.INSENSITIVE_CASE + searchText);
final Matcher matcher = pattern.matcher(autoSuggestText); // TODO: figure out why this line throws NPE "at java.util.regex.Pattern.matcher(Pattern.java:290)"
int end = 0;
while (matcher.find()) {
final String subStringMatchFound = autoSuggestText.substring(end, matcher.end());
final String stringToBeReplaced = autoSuggestText.substring(matcher.start(), matcher.end());
final String stringToReplace = "<b><font color='" + getResources().getColor(R.color.search_autosuggest_highlighted_text) + "'>" +matcher.group()+ "</font></b>";
modifiedAutoSuggestText += subStringMatchFound.replace(stringToBeReplaced,stringToReplace);
end = matcher.end();
}
modifiedAutoSuggestText += autoSuggestText.substring(end);
return Html.fromHtml(modifiedAutoSuggestText);
}
catch (final Exception e){
return Html.fromHtml(autoSuggestText);
}
}
public void onHasResults() {
if (getView() == null) {
Log.d("Search Issues", "view is null");
return;
}
LayoutUtils.showResults(getView(), R.id.search_history_auto_suggest_list);
}
public void onLoading() {
LayoutUtils.showLoading(getView(), R.id.search_history_auto_suggest_list);
}
public void onNoResults() {
View view = getView().findViewById(R.id.overlay);
if (view != null) {
view.setVisibility(View.GONE);
}
}
}
搜索历史自动建议片段:
公共类 SearchHistoryAutoSuggestFragment 扩展 AbsSearchAutoSuggestFragment 实现 ViewBinder、SearchHistoryItemClickListener、OnClickListener、MainActivity.BackPressListener {
public final static String TAG_SEARCH_HISTORY_AUTO_SUGGEST_FRAGMENT = "SearchHistoryAutoSuggestFragment";
private SearchHistoryListAdapter mAdapter;
private TextWatcher mTextWatcher;
private static final String[] COLUMN_NAMES = {
SearchHistory.Columns.SEARCH_TERM,
SearchHistory.Columns.SEARCH_HISTORY_META_DATA,
SearchHistory.Columns.SEARCH_SYMBOL
};
private static final int[] VIEW_IDS = {
R.id.search_item,
R.id.search_history_metadata,
R.id.search_symbol
};
public static SearchHistoryAutoSuggestFragment newInstance(final FragmentManager manager) {
final SearchHistoryAutoSuggestFragment fragment = new SearchHistoryAutoSuggestFragment();
final FragmentInfo fragmentInfo = new FragmentInfo(TransactionMethods.ADD);
fragmentInfo.setAnimation(R.anim.slide_in_from_top, 0);
fragmentInfo.setFragmentTag(TAG_SEARCH_HISTORY_AUTO_SUGGEST_FRAGMENT);
FragmentStackManager.getInstance().transitionFragment(manager, fragment, fragmentInfo);
return fragment;
}
public static void removeInstance(final FragmentManager manager) {
final SearchHistoryAutoSuggestFragment fragment = (SearchHistoryAutoSuggestFragment) manager.findFragmentByTag(TAG_SEARCH_HISTORY_AUTO_SUGGEST_FRAGMENT);
if (fragment == null) {
return;
}
final FragmentInfo fragmentInfo = new FragmentInfo(TransactionMethods.REMOVE);
fragmentInfo.setAnimation(R.anim.slide_out_to_top, FragmentInfo.NO_ANIMATION);
fragmentInfo.setFragmentTag(TAG_SEARCH_HISTORY_AUTO_SUGGEST_FRAGMENT);
final FragmentStackManager stackManager = FragmentStackManager.getInstance();
if (stackManager.getTopFragment() instanceof SearchHistoryAutoSuggestFragment) {
stackManager.popTopFragment();
}
}
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
mSearchView = (EditText) getActivity().findViewById(R.id.search_text);
mAdapter = new SearchHistoryListAdapter(getActivity(), R.layout.item_search_history_auto_suggest, null, COLUMN_NAMES, VIEW_IDS, 0);
mAdapter.setViewBinder(this);
final View view = inflater.inflate(R.layout.fragment_search_history_auto_suggest, container, false);
mSearchListView = (ListView) view.findViewById(R.id.search_history_auto_suggest_list);
mAdapter.setOnItemClickedListener(this);
mSearchListView.setAdapter(mAdapter);
setSearchLinkOnclickListener(view);
setSearchViewTextChangedListener();
setSearchViewKeyListener();
return view;
}
@Override
public Uri onCreateContentUri() {
return SearchContentProvider.SEARCH_HISTORY_URI;
}
@Override
public void onOperationStarted(final Uri uri) {
onLoading();
}
@Override
public void onCursorLoaded(final Uri uri, final Cursor cursor) {
super.onCursorLoaded(uri, cursor);
if (!isOperationExecuting()){
if (cursor != null && cursor.getCount() > 0) {
onHasResults();
}
}
}
@Override
public boolean setViewValue(final View view, final Cursor cursor, final int columnIndex) {
Log.d("SearchHistoryIssue", "setting view value");
switch (view.getId()) {
case R.id.search_symbol:
String symbol = cursor.getString(columnIndex);
View parent = (View) view.getParent();
View dash = parent.findViewById(R.id.search_dash);
TextView symbolTextView = (TextView) parent.findViewById(R.id.search_symbol);
if (StringUtils.isEmpty(symbol)) {
symbolTextView.setText("");
view.setVisibility(View.GONE);
if (dash != null) {
dash.setVisibility(View.GONE);
}
return true;
} else {
view.setVisibility(View.VISIBLE);
if (dash != null) {
dash.setVisibility(View.VISIBLE);
}
return false;
}
}
return false;
}
private void setSearchViewTextChangedListener(){
mTextWatcher = new TextWatcher() {
@Override
public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
handleTextChanged();
}
@Override
public void beforeTextChanged(final CharSequence s, final int start, final int count,final int after) {
}
@Override
public void afterTextChanged(final Editable s) {
handleAfterTextChanged(s);
}
};
mSearchView.addTextChangedListener(mTextWatcher);
}
private boolean enterButtonClicked = false;
private void setSearchViewKeyListener(){
mSearchView.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(final View v, final int keyCode, final KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_ENTER && !enterButtonClicked) {
final String searchString = mSearchView.getText().toString();
if(!searchString.isEmpty()){
showSearchResults(searchString);
}
enterButtonClicked = true;
return true;
}
enterButtonClicked = false;
return false;
}
});
}
private void handleTextChanged() {
final View clearButton = ((View) mSearchView.getParent()).findViewById(R.id.clear_search_textbox);
if (mSearchView.getText().length() > 0) {
clearButton.setVisibility(View.VISIBLE);
} else {
handleTextClear(clearButton);
}
displaySearchLink();
}
private void handleAfterTextChanged(final Editable s) {
if (s.length() != 0) {
Log.d("AutoSuggest", "Auto Suggest - text pressed on " + System.currentTimeMillis());
startSearchAutoSuggestTask(s.toString());
} else {
displaySearchHistory();
}
}
private void handleTextClear(final View clearButton) {
clearButton.setVisibility(View.GONE);
final boolean autoSuggestActive = killLastSearchAutoSuggestTask();
if (!autoSuggestActive) {
displaySearchHistory();
}
}
private void displaySearchHistory() {
final Activity activity = getActivity();
if (activity == null) {
return;
}
mAdapter = new SearchHistoryListAdapter(activity, R.layout.item_search_history_auto_suggest, null, COLUMN_NAMES, VIEW_IDS, 0);
mAdapter.setOnItemClickedListener(this);
mAdapter.setViewBinder(this);
mSearchListView.setAdapter(mAdapter);
Log.d("SearchHistoryIssue", "About to restart content loader");
restartContentLoader();
}
public void showSearchResults(final String searchString) {
runSearch(searchString);
saveSearchHistory(searchString, null, null);
}
private void saveSearchHistory(final String searchString, final String historyMetaData, final String symbol) {
final SearchHistory searchHistory = new SearchHistory(searchString, historyMetaData, symbol);
DatabaseUtils.addEntryToSearchHistoryTable(searchHistory);
}
private void runSearch(final String searchTerm) {
killLastSearchAutoSuggestTask();
hideSearchView();
if(searchTerm.contains(mSearchView.getText().toString().toLowerCase() + " " + mSearchView.getText().toString().toLowerCase())){
AnswersWebViewFragment.newInstanceForSearch(getFragmentManager(), null, null);
}else {
SearchResultsFragment.newInstance(getFragmentManager(), searchTerm);
}
}
private void displaySearchLink() {
final View view = getView();
if (view != null) {
final String searchText = mSearchView.getText().toString();
final TextView searchLink = (TextView) view.findViewById(R.id.search_link);
if (StringUtils.isNotEmpty(searchText)) {
searchLink.setText(getResources().getString(R.string.search_for)+" \"" + searchText + "\"");
searchLink.setVisibility(View.VISIBLE);
}else {
searchLink.setVisibility(View.GONE);
}
}
}
@Override
public void onClick(final View view) {
switch (view.getId()) {
case R.id.search_link:
final String searchText = mSearchView.getText().toString();
showSearchResults(searchText);
break;
default:
break;
}
}
@Override
public void onHistoryItemClicked(final int viewId, final String searchHistoryTerm, final String searchHistoryMetadata, final String searchHistorySymbol) {
switch (viewId) {
case R.id.search_history_item_container:
saveSearchHistory(searchHistoryTerm, searchHistoryMetadata, searchHistorySymbol);
if (StringUtils.isEmpty(searchHistoryMetadata)) {
runSearch(searchHistoryTerm);
} else {
displayQuotes(searchHistoryTerm, searchHistoryMetadata, searchHistorySymbol);
}
break;
case R.id.search_item_button:
mSearchView.setText(searchHistoryTerm);
mSearchView.requestFocus();
mSearchView.setSelection(searchHistoryTerm.length());
break;
default:
}
}
@Override
public void onAutoSuggestItemClicked(final View view) {
switch (view.getId()) {
case R.id.search_autosuggest_item_container:
final String searchAutoSuggestTitle = ((TextView)view.findViewById(R.id.search_autosuggest_item_name)).getText().toString();
final String searchAutoSuggestSubTitle = (String) ((TextView)view.findViewById(R.id.search_autosuggest_item_subtitle)).getText();
final String searchAutoSuggestSymbol = ((TextView)view.findViewById(R.id.search_autosuggest_item_symbol)).getText().toString();
saveSearchHistory(searchAutoSuggestTitle, searchAutoSuggestSubTitle, searchAutoSuggestSymbol);
displayQuotes(searchAutoSuggestTitle, searchAutoSuggestSubTitle, searchAutoSuggestSymbol);
break;
default:
break;
}
}
private void displayQuotes(final String searchAutoSuggestTitle, final String searchAutoSuggestSubTitle, final String searchAutoSuggestSymbol) {
killLastSearchAutoSuggestTask();
QuotesFragmentWebView.newInstanceForSearch(getFragmentManager(), searchAutoSuggestSymbol, null);
hideSearchView();
}
private void showKeyboard() {
final MainActivity activity = (MainActivity) getActivity();
if (activity == null) {
return;
}
final EditText searchText = (EditText) activity.findViewById(R.id.search_text);
KeyboardUtils.showKeyboard(searchText, activity);
}
};
}
回答webviewfragment:
public class AnswersWebViewFragment extends Fragment implements OnClickListener , MainActivity.BackPressListener<Fragment> {
public static String OUTPUT_ENVIRONMENT = "https://.mobile13.cp.com/msf1.0/fwd/answers/answers/service/v1/?q="+[SEARCHTERM]+"%20revenue&ui.theme=novadark&uuid=PADACT-002&userAgent=iphone";
private static AnswersWebViewFragment __newInstance(final AnswersWebViewFragment fragment, final FragmentManager manager,
final String searchTerm, final String symbolType, int containerViewId, final int inAnimation, final int outAnimation, final int popInAnimation, final int popOutAnimation) {
final Bundle bundle = new Bundle();
bundle.putString(SEARCH_TERM, searchTerm);
bundle.putString(AnswersWebViewFragment.SYMBOL_TYPE, symbolType);
bundle.putInt(AnswersWebViewFragment.CONTAINER_ID, containerViewId);
fragment.setArguments(bundle);
FragmentInfo fragmentInfo = new FragmentInfo(TransactionMethods.ADD, containerViewId);
fragmentInfo.setAnimation(inAnimation, outAnimation);
fragmentInfo.setPopAnimation(popInAnimation, popOutAnimation);
fragmentInfo.setFragmentTag(TAG_ANSWERS_FRAGMENT_WEBVIEW);
fragmentInfo.setActionBarTitle(EikonApplication.getAppResources().getString(R.string.nav_option_quotes));
FragmentStackManager.getInstance().transitionFragment(manager, fragment, fragmentInfo);
return fragment;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSearchTerm = getArguments().getString(SEARCH_TERM);
}
如何修复 answersWebviewfragment 以检索搜索词?PS:一旦您的答案被批准为正确,回答此问题的人将获得大约 100 分,而不仅仅是 50 分