我怎样才能在这个例子中访问对象" selectedBox"从片段?我需要在主要活动中测试连接和片段。
当我在main和片段中创建它。
MainActivity.java
public class MainActivity extends FragmentActivity implements ActionBar.TabListener{
private ViewPager viewPager;
private ActionBar actionBar;
private MainTabAdapter mAdapter;
private String[] tabs = { "Live" };
private box selectedBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new MainTabAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_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) {
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
selectedBox = new box(this);
}
public void onResume(){
super.onResume();
if(selectedBox.getId() != -1){
checkReachable(selectedBox.getIpPort());
}else {
Intent intent = new Intent(this, SettingsActivity.class);
this.startActivity(intent);
finish();
}
}
private void checkReachable(String ipPort) {
Log.d("XML Request", "CHECK BOX");
String e2About = "http://" + ipPort + "/web/about";
getXml.DownloadCompleteListener dcl = new getXml.DownloadCompleteListener() {
@Override
public void onDownloadComplete(String result) {
if (!result.contains("<e2enigmaversion>")) {
Toast.makeText(getApplicationContext(), "Active box not reachable.", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
startActivity(intent);
}else{
viewPager.setAdapter(mAdapter);
}
}
};
getXml downloader = new getXml(dcl);
downloader.execute(e2About);
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.action_settings:
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
MainTabAdapter
public class MainTabAdapter extends FragmentPagerAdapter {
public MainTabAdapter(FragmentManager fm){
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new MainFragmentLive();
}
return null;
}
@Override
public int getCount() {
return 1;
}
}
MainFragmentLive.java
public class MainFragmentLive extends ListFragment {
private box selectedBox;
private List<String> bouquetListString;
private ArrayAdapter<String> adapter;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main_live, container, false);
Log.d("Position", "Main Live Set Box");
selectedBox = new box(getActivity());
bouquetListString = selectedBox.getBouquets().get(2);
if(bouquetListString.size() < 1){
getBouquetBox();
}
adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, bouquetListString);
setListAdapter(adapter);
ImageButton reloadBouquet = (ImageButton) view.findViewById(R.id.reloadBouquet);
reloadBouquet.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
getBouquetBox();
}
});
setHasOptionsMenu(true);
return view;
}
public void onListItemClick(ListView l, View v, int position, long id) {
SharedPreferences savedData = getActivity().getSharedPreferences("box",0);
SharedPreferences.Editor editor = savedData.edit();
editor.putInt("boId", position);
editor.apply();
Intent gotoChannels = new Intent(getActivity(), ChannelListActivity.class);
startActivity(gotoChannels);
}
public void getBouquetBox(){
getXml.DownloadCompleteListener dcl = new getXml.DownloadCompleteListener() {
@Override
public void onDownloadComplete(String result) {
selectedBox.delBouquets();
bouquetListString.clear();
String [] tags = {"e2servicereference", "e2servicename"};
List<List<String>> bouquetsList = parseXml.parseXmlByTag(result, tags);
selectedBox.addBouquets(bouquetsList);
bouquetListString.addAll(selectedBox.getBouquets().get(2));
adapter.notifyDataSetChanged();
}
};
Log.d("XML Request", "GET BOUQUET");
getXml downloader = new getXml(dcl);
downloader.execute("http://" + selectedBox.getIpPort() + "/web/getservices");
}
}
activity_main.xml中
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
fragment_main_live.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:text="@string/selectBouquet"
style="@style/header1"/>
<ImageButton
android:layout_width="0dip"
android:layout_height="wrap_content"
android:id="@+id/reloadBouquet"
android:src="@drawable/ic_action_refresh"
android:contentDescription="@string/search"
android:layout_weight=".20"
android:layout_gravity="bottom"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/list" />
</LinearLayout>
答案 0 :(得分:0)
您需要从片段中创建活动的界面。类似的东西:
public class MainActivity extends FragmentActivity implements ActionBar.TabListener,MainFragment.getSelectedBox {
private ViewPager viewPager;
private ActionBar actionBar;
private MainTabAdapter mAdapter;
private String[] tabs = { "Live" };
private box selectedBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
....
}
//called from the MainFragment
@Override
public void AccessSelectedBox {
this.box = <do something with box>
}
}
片段类
public class MainFragment extends ListFragment {
//interface to the MainActivity activity class
private getSelectedBox listener;
public interface getSelectedBox {
public void AccessSelectedBox();
}
}
以下是一篇很好的文章:http://www.vogella.com/articles/AndroidFragments/article.html。