2

I developed an android application, as you see in this picture, enter image description here

I used sherlock action bar for searching, the only problem is that when the user search something in web tab if he/she wants to see the image result, he should type and search again in image tab, is there any way to pass the edittext parameter to other tabs and do searching when user changed the tabs?

MainActivity:

public class MainActivity extends SherlockFragmentActivity implements
    TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private com.actionbarsherlock.app.ActionBar actionBar;
private String[] tabs = { "Sound", "Image", "Web" };

@Override
protected void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getSupportActionBar();
    actionBar.setDisplayShowTitleEnabled(false);  
    actionBar.setDisplayShowHomeEnabled(false);
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
    actionBar.setHomeButtonEnabled(false);
    viewPager.setAdapter(mAdapter);
    viewPager.setOffscreenPageLimit(3);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    actionBar.addTab(actionBar.newTab().setText(tabs[0])
            .setTabListener(this), false);
    actionBar.addTab(actionBar.newTab().setText(tabs[1])
            .setTabListener(this), false);
    actionBar.addTab(actionBar.newTab().setText(tabs[2])
            .setTabListener(this), true);

    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

@Override
public void onTabSelected(com.actionbarsherlock.app.ActionBar.Tab tab,
        android.support.v4.app.FragmentTransaction ft) {
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(com.actionbarsherlock.app.ActionBar.Tab tab,
        android.support.v4.app.FragmentTransaction ft) {
}

@Override
public void onTabReselected(com.actionbarsherlock.app.ActionBar.Tab tab,
        android.support.v4.app.FragmentTransaction ft) {
}
}

Adapter:

public class TabsPagerAdapter extends FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0:
        return new Sound();
    case 1:
        return new Image();
    case 2:
        return new Search();
    }

    return null;
}

@Override
public int getCount() {
    return 3;
}

}

Sound Frgment:

public class Sound extends Fragment {

private String URL_ADDRESS, Servlet;
private ListView listview;
private ListViewAdapter adapter;
private List<Spanned> numberlist = new ArrayList<Spanned>();
private List<Ava2> avaList = new ArrayList<Ava2>();
private Boolean isInternetPresent = false;
private ConnectionDetector detector;
private View rootView;
private RelativeLayout loading, layout;
private int counter = 10;
private EditText editText;
private String a = "http://***";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.ava, container, false);
    ImageButton voiceSearch = (ImageButton) rootView
            .findViewById(R.id.imageButton2);
    ImageButton buttonSearch = (ImageButton) rootView
            .findViewById(R.id.imageButton1);
    listview = (ListView) rootView.findViewById(R.id.listview);
    loading = (RelativeLayout) rootView.findViewById(R.id.loading);
    layout = (RelativeLayout) rootView.findViewById(R.id.layout);
    editText = (EditText) rootView.findViewById(R.id.editText1);
    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                search();
                return true;
            }
            return false;
        }

    });
    buttonSearch.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            search();
        }
    });

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view,
                int position, long l) {
                try {
                    Intent i = new Intent(getActivity(), Play.class);

                    startActivity(i);
                } catch (Exception e) {
                }
            }
        }
    });

    return rootView;
}
user3427977
  • 115
  • 1
  • 1
  • 8

2 Answers2

1

First,you could write a interface:

public interface OnProfileDataPass {
      public String getCurrentSearchword();
      public void setCurrentSearchword(String searchword);
}

Then in your fragments:

public class WebFragment extends Fragment {
    private OnProfileDataPass dataPasser;

    @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        dataPasser = (OnProfileDataPass) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnProfileDataPass");
    }
    searchword = dataPasser.getCurrentSearchword();
    }
}

In your activity:

  public class MainActivity implements OnProfileDataPass {
     String currentSearchword;

 @Override
    public String getCurrentSearchword() {
        return currentSearchword;
    }

 @Override
    public String setCurrentSearchword(String searchword) {
        this.currentSearchword = searchword;
    }


}

That's my solution.When you change the tap,you could listen the proper event and set the word with getActivity.setCurrentSearchword(String newword),then you could get the newword in the fragment's dataPasser variable's method getCurrentSearchword() to get the search word. I have test it in my app. Wish that helps you a little.

penkzhou
  • 1,200
  • 13
  • 30
0

you should save the value of the editText in your activity and then pass it as an argument when you call your fragment, using the same technique as the one described here: http://developer.android.com/reference/android/app/Fragment.html

k3v1n4ud3
  • 2,904
  • 1
  • 15
  • 19