How do I pass on the search string from one class to another?
I have a search View which is defined as follows in a class basesearch.java:
protected EditText mSearchView;
(It refers to the basic textbox search view).
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);
}
}
Note: mSearchView.getText().toString().toLowerCase() is getting the typed
in search value. Now, I want to use this value
"mSearchView.getText().toString().toLowerCase()" and pass it on to another
class which is being called "AWebViewFragment" in the first if clause. I
am defining the AwebViewfragment as follwos:
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";
However it throws an error at mSearchView,unless I set some dummy value
defining it. I want it to be able to get information from previous class
for the typed in search term. I wonder what I am doing wrong. I want the
url to display with the dynamically typed search term. Is it possible to
do the same somehow? Feel free to modify the code and walk me thru a new
change, I will try any suggestions for new code or improvements. Any help
greatly appreciated. Thanks! Justin Also, I am already using the bundles
in 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;
}
And OUTPUT_ENVIRONMENT is defined outside the method for bundle, how do i
retrieve the search term passed on from the previous class? and oncreate I
am using :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSearchTerm = getArguments().getString(SEARCH_TERM);
}
ENTIRE CODE BELOW:
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:
public class SearchHistoryAutoSuggestFragment extends
AbsSearchAutoSuggestFragment implements 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);
}
};
}
Answerswebviewfragment:
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);
}
How do I fix the answersWebviewfragment to retrieve the searchterm??
No comments:
Post a Comment