最是人间留不住,朱颜辞镜花辞树。这篇文章主要讲述Android - 将搜索传递给片段相关的知识,希望能为你提供帮助。
所以我有一个带有标签(4)的应用程序,每个标签都有自己的布局和类文件。我还在操作栏中有一个searchview小部件。
我试图做的是允许用户搜索某些内容,将其作为字符串获取然后将其发送到片段,以便我可以使用该字符串并在将其用于查询后更新UI。
目前我有它,以便用户输入文本,一个类处理意图等等,然后我在片段类中获取我需要它的变量。
但是,当我在搜索视图中输入内容并按Enter键时,会弹出一个新窗口,但屏幕为空白。没有标签。没有搜索查看。
当然我做错了什么。有没有人有我可以提出的建议或改变?
main activity.java
public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Home", "Most Recent", "Followed", "All Items" };
public String test = "test";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs)
{
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {@Override
public void onPageSelected(int position)
{
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{}@Override
public void onPageScrollStateChanged(int arg0)
{}
});
}@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}@Override
public void onTabReselected(Tab tab, FragmentTransaction ft)
{}@Override
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft)
{}}
search results activity.Java
public class SearchResultsActivity extends Activity
{
public String query = "";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
handleIntent(getIntent());
}@Override
protected void onNewIntent(Intent intent)
{
setIntent(intent);
handleIntent(intent);
}private void handleIntent(Intent intent)
{
if(Intent.ACTION_SEARCH.equals(intent.getAction()))
query = intent.getStringExtra(SearchManager.QUERY);
}
}
company fragment.Java
public class companyFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.company, container, false);
String temp = new SearchResultsActivity().query;
TextView tv = (TextView) rootView.findViewById(R.id.textView1);
tv.setText(temp);
return rootView;
}}
答案我回过头来实现了一个基本的搜索示例。希望这会让你朝着正确的方向前进。
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import android.content.Context;
import android.content.Intent;
import android.database.AbstractCursor;
import android.database.Cursor;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.widget.SearchView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class SearchActivity extends FragmentActivity {
public static Intent createIntent(Context context) {
return new Intent(context, SearchActivity.class);
}@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search, menu);
return super.onCreateOptionsMenu(menu) | true;
}@Override
public boolean onPrepareOptionsMenu(Menu menu) {
final SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSuggestionsAdapter(new SearchSuggestionsAdapter(this));
searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionClick(int position) {
Toast.makeText(SearchActivity.this, "Position: " + position, Toast.LENGTH_SHORT).show();
searchView.clearFocus();
return true;
}@Override
public boolean onSuggestionSelect(int position) {
return false;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(SearchActivity.this, query, Toast.LENGTH_SHORT).show();
searchView.clearFocus();
return true;
}@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onPrepareOptionsMenu(menu) | true;
}public static class SearchSuggestionsAdapter extends SimpleCursorAdapter {
private static final String[] mFields = {"_id", "result"};
private static final String[] mVisible = {"result"};
private static final int[] mViewIds = {android.R.id.text1};
public SearchSuggestionsAdapter(Context context) {
super(context, android.R.layout.simple_list_item_1, null, mVisible, mViewIds, 0);
}@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
return new SuggestionsCursor(constraint);
}private static class SuggestionsCursor extends AbstractCursor {
private ArrayList<
String>
mResults;
public SuggestionsCursor(CharSequence constraint) {
final int count = 100;
mResults = new ArrayList<
String>
(count);
for (int i = 0;
i <
count;
i++) {
mResults.add("Result " + (i + 1));
}
if (!TextUtils.isEmpty(constraint)) {
String constraintString = constraint.toString().toLowerCase(Locale.ROOT);
Iterator<
String>
iter = mResults.iterator();
while (iter.hasNext()) {
if (!iter.next().toLowerCase(Locale.ROOT).startsWith(constraintString)) {
iter.remove();
}
}
}
}@Override
public int getCount() {
return mResults.size();
}@Override
public String[] getColumnNames() {
return mFields;
}@Override
public long getLong(int column) {
if (column == 0) {
return mPos;
}
throw new UnsupportedOperationException("unimplemented");
}@Override
public String getString(int column) {
if (column == 1) {
return mResults.get(mPos);
}
throw new UnsupportedOperationException("unimplemented");
}@Override
public short getShort(int column) {
throw new UnsupportedOperationException("unimplemented");
}@Override
public int getInt(int column) {
throw new UnsupportedOperationException("unimplemented");
}@Override
public float getFloat(int column) {
throw new UnsupportedOperationException("unimplemented");
}@Override
public double getDouble(int column) {
throw new UnsupportedOperationException("unimplemented");
}@Override
public boolean isNull(int column) {
return false;
}
}
}
}
【Android - 将搜索传递给片段】https://gist.github.com/slightfoot/5514856
推荐阅读
- Okta教程(appBaseUrl丢失)
- 在空对象引用Error上获取'getActivity()。getApplication()'
- 我正在尝试在android studio上创建标签片段
- Android 9 Pie崩溃(com.google.android.gms ... ClassNotFoundException)
- 管理帐户mvc app c#[关闭]
- XP开机蓝屏提示stop 0x000000c2出错如何修好
- XP系统蓝屏代码0x1000000a如何修好?
- XP系统蓝屏代码0x000000c5的处理办法
- XP系统蓝屏提示出错代码0x000000ED怎样办?