gpt4 book ai didi

Android - 将搜索传递给 fragment

转载 作者:行者123 更新时间:2023-11-29 00:22:49 25 4
gpt4 key购买 nike

所以我有一个应用程序,其中包含选项卡 (4),每个选项卡都有自己的布局和类文件。我在操作栏中也有一个搜索 View 小部件。

我试图做的是允许用户搜索某些内容,将其作为字符串获取,然后将其发送到 fragment ,这样我就可以使用该字符串并在使用它进行查询后更新 UI。

目前我有它,以便用户输入文本,一个类处理 Intent 等等,然后我在 fragment 类中获取我需要它的变量。

但是,当我在搜索 View 中输入内容并按下回车键时,会弹出一个新窗口,但屏幕是空白的。没有标签。没有搜索 View 。

肯定是我做错了什么。有没有人有我可以做的建议或更改?

主 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)
{

}

}

SearchResultsActivity.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);
}
}

companyFragment.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;
}
}
}
}

https://gist.github.com/slightfoot/5514856

关于Android - 将搜索传递给 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21962064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com