Android 如何将搜索字符串从一个类传递到另一个类?

Android 如何将搜索字符串从一个类传递到另一个类?,android,Android,我有一个搜索视图,在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().to

我有一个搜索视图,在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;
    }
    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);
}
而OUTPUT_环境是在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);
        }
    }

}
}

回答:查看碎片:

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;
    }
    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以检索搜索词??
注:回答这个问题的人将得到大约100分的奖励,而不仅仅是50分,一旦你的答案被批准为正确的

如果你的另一个类是fragment,你可以使用setArgumentbundle方法将字符串传递给另一个fragment。要接收这些字符串,可以使用getArgument方法。


在mSearchView.getText有值之前,您不是定义了输出\u环境吗?BaseSearch.java是一个显示SearchFragment的活动吗?或者SearchFragment扩展了BaseSearch?OUTPUT_环境位于不同的类AebFragment中,我已经为searchview类导入了SearchFragment类。但是,我确信这不是正确的方法,即使我在输出环境之前定义searchview,它也不能帮助我从前面的searchfragment类中获取动态值,如何传递msearchview或searchterm中包含的值,以便在AWebviewfragment的url中使用它class@drees这些都是片段,seatchfragment确实扩展了basesearch.javaBase类不应该持有视图,将mSearchView移动到另一个片段,然后将输入的字符串作为变量保存在basesearch.java中。然后在SearchFragment中,调用BaseSearch.getEnteredText并使用它,而不是调用mSearchView.getText。如果你仍然有问题,我会发布一个完整的答案。请检查我编辑的问题,我已经在使用AWEBVIEWFRAGMENT类中的包了