0

我有一个小应用程序,我在其中使用eclipse创建了三个可滑动的选项卡。其中一个是通用片段,而第二个和第三个是ListFragments,每个都从Cursor获取数据。如何使用标签运行活动中片段的方法?

我对它进行了实验并希望删除在托管活动的onCreate中创建的数据库的内容。我的问题是,当我删除数据时,第二个选项卡,其中一个ListFragments,没有更新,而第三个选项卡是。

当第二次得到更新时,当我将手机置于睡眠状态时,我认为第二个选项卡并未移入其生命周期,因此它不会调用onResume。

两个ListFragments使用相同的代码更新:

@Override 
    public void onResume(){ 
     super.onResume(); 
     updateList(); 
     Log.i(TAG, "onResume"); 
    } 

public void updateList(){ 
     dbCursor.requery(); 
     dbAdapter.notifyDataSetChanged(); 
     Log.i(TAG, "updateList"); 
    } 

所以,我试图用onTabSelected方法来做到这一点,但我总是得到我想要避免使用标签NullPointerException异常,但我不明白它工作。

@Override 
    public void onTabSelected(ActionBar.Tab tab, 
      FragmentTransaction fragmentTransaction) { 
     // When the given tab is selected, switch to the corresponding page in 
     // the ViewPager. 
     mViewPager.setCurrentItem(tab.getPosition()); 
     Log.e(TAG, String.valueOf(tab.getPosition())); 
     switch(tab.getPosition()) { 
     case 0: 
      MenueFragment menu = new MenueFragment(); 
//   fragmentTransaction.add(menu, "1");//Eclipse wants to change Type of menu from MenueFragment to Fragment 
//   menu = (MenueFragment) getFragmentManager().findFragmentByTag("1"); //My latest, not working approach... 
      menu.updateList(); //In this Fragment updateList only does an entry into LogCat for testing. 
      break; 
     case 1: //These two following cases show how I tried it before. 
      GefilterteListe fragment = new GefilterteListe(); 
      fragment.updateList(); 
      break; 
     case 2: 
      GesamteListe fragment2 = new GesamteListe(); 
      fragment2.updateList(); 
      break; 
     } 
     Log.i(TAG, "onTabSelected"); 
    } 

的Eclipse总是想改变我的类型的片段到另一个片段级和希望android.app.fragment,而我想用support.v4 ...

我如何让它工作,这样两个ListFragments刷新?
林全新片段和我知道光标级的折旧,但我并没有足够的时间来了解装载机...:/

编辑:这是我的工作不ListFragment:

import android.content.Context; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.support.v4.app.ListFragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class GefilterteListe extends ListFragment{ 

    public static final String TAG = GefilterteListe.class.getSimpleName(); 

    private DatenbankHandler dbHandler; 
    private Cursor dbCursor; 
    private LehrerDatenbankAdapter dbAdapter; 
    private Context mContext; 

    public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
     Log.i(TAG, "GefilterteListe onCreate"); 
     View returnView = inflater.inflate(R.layout.plan, container, false); 
     return returnView; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     dbHandler = new DatenbankHandler(getActivity()); 
     dbCursor = dbHandler.queryFilter(); 
     dbAdapter = new LehrerDatenbankAdapter(getActivity(), dbCursor); 
     setListAdapter(dbAdapter); 
     Log.i(TAG, "onCreate"); 
    } 

    @Override 
    public void onResume(){ 
     super.onResume(); 
     updateList(); 
     Log.i(TAG, "onResume"); 
    } 

    /*@Override 
    public void onPause(){ 
     super.onPause(); 
     updateList(); 
     Log.i(TAG, "onPause"); 
    }*/ 

    public void updateList(){ 
     dbCursor.requery(); 
     dbAdapter.notifyDataSetChanged(); 
     Log.i(TAG, "updateList"); 
    } 

} 

除了名称和另一个使用queryAll()而不是queryFilter()之外,它与工作中的几乎相同。

我的另一个片段:

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.TextView; 

public class MenueFragment extends Fragment { 


    public static final String TAG = MenueFragment.class.getSimpleName(); 
    TextView text; 
    Button button; 
    DatenbankHandler dbHandler; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     Log.i(TAG, "MenueFragment onCreate"); 
     View returnView = inflater.inflate(R.layout.fragment_main_dummy, container, false); 
     text = (TextView) returnView.findViewById(R.id.section_label); 
     button = (Button) returnView.findViewById(R.id.button1); 

     button.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       text.setText("Datenbank gelöscht."); 
       dbHandler = new DatenbankHandler(getActivity()); 
       dbHandler.redoTbl(); 
      } 

     }); 

     return returnView; 
    } 

    public void updateList(){ 
     Log.i(TAG, "updateList nonsense"); 
    } 
} 

活动大多是相同的,比如Eclipse创建它。我只添加了我的碎片。
EDIT2:这里是我的托管服务活动:

public class HostActivity extends FragmentActivity implements ActionBar.TabListener { 

    /** 
    * The {@link android.support.v4.view.PagerAdapter} that will provide 
    * fragments for each of the sections. We use a 
    * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which 
    * will keep every loaded fragment in memory. If this becomes too memory 
    * intensive, it may be best to switch to a 
    * {@link android.support.v4.app.FragmentStatePagerAdapter}. 
    */ 
    SectionsPagerAdapter mSectionsPagerAdapter; 

    /** 
    * The {@link ViewPager} that will host the section contents. 
    */ 
    ViewPager mViewPager; 

    private static final String TAG = HostActivity.class.getSimpleName(); 
    private DatenbankHandler dbHandler; 

    private MenueFragment menueFragment; 
    private GefilterteListe gefilterteListe; 
    private GesamteListe gesamteListe; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
//  passwort("okidoki"); //Passwort Dialog 
     setContentView(R.layout.activity_main); 

     menueFragment = new MenueFragment(); 
     gefilterteListe = new GefilterteListe(); 
     gesamteListe = new GesamteListe(); 

     dbHandler = new DatenbankHandler(this); 
     dbHandler.redoTbl(); 
     dbHandler.insert("T", "E", "S", "T", "This is a ", "Test."); 
     dbHandler.insert("E", "E", "S", "T", "This is a ", "Test."); 
     dbHandler.insert("S", "E", "S", "T", "This is a ", "Test."); 
     dbHandler.insert("T ", "E", "S", "T", "This is a", "Test."); 

     // Set up the action bar. 
     final ActionBar actionBar = getActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the app. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(
       getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

     // When swiping between different sections, select the corresponding 
     // tab. We can also use ActionBar.Tab#select() to do this if we have 
     // a reference to the Tab. 
     mViewPager 
       .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
        @Override 
        public void onPageSelected(int position) { 
         actionBar.setSelectedNavigationItem(position); 
        } 
       }); 

     // For each of the sections in the app, add a tab to the action bar. 
     for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { 
      // Create a tab with text corresponding to the page title defined by 
      // the adapter. Also specify this Activity object, which implements 
      // the TabListener interface, as the callback (listener) for when 
      // this tab is selected. 
      actionBar.addTab(actionBar.newTab() 
        .setText(mSectionsPagerAdapter.getPageTitle(i)) 
        .setTabListener(this)); 
     } 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.host, menu); 
     return true; 
    } 

    @Override 
    public void onTabSelected(ActionBar.Tab tab, 
      FragmentTransaction fragmentTransaction) { 
     // When the given tab is selected, switch to the corresponding page in 
     // the ViewPager. 
     mViewPager.setCurrentItem(tab.getPosition()); 
     /*Log.e(TAG, String.valueOf(tab.getPosition())); 
     switch(tab.getPosition()) { 
     case 0: 
      MenueFragment menu = new MenueFragment(); 
//   fragmentTransaction.add(menu, "1");//Eclipse wants to change Type of menu from MenueFragment to Fragment 
//   menu = (MenueFragment) getFragmentManager().findFragmentByTag("1"); //My latest, not working approach... 
      menu.updateList(); 
      break; 
     case 1: 
      GefilterteListe fragment = new GefilterteListe(); 
      fragment.updateList(); 
      break; 
     case 2: 
      GesamteListe fragment2 = new GesamteListe(); 
      fragment2.updateList(); 
      break; 
     }*/ 
//  updateAll(); 
     Log.i(TAG, "onTabSelected"); 
    } 

    @Override 
    public void onTabUnselected(ActionBar.Tab tab, 
      FragmentTransaction fragmentTransaction) { 
    } 

    @Override 
    public void onTabReselected(ActionBar.Tab tab, 
      FragmentTransaction fragmentTransaction) { 
    } 

    /** 
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
    * one of the sections/tabs/pages. 
    */ 
    public class SectionsPagerAdapter extends FragmentStatePagerAdapter { 

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

     @Override 
     public Fragment getItem(int position) { 
      Fragment frag = new Fragment(); 
      // getItem is called to instantiate the fragment for the given page. 
      // Return a DummySectionFragment (defined as a static inner class 
      // below) with the page number as its lone argument. 
      switch(position) { 
      case 0: 
       MenueFragment menu = new MenueFragment(); 
       frag = menu; 
       break; 
      case 1: 
       Fragment fragment = new GefilterteListe(); 
       frag = fragment; 
       break; 
      case 2: 
       Fragment fragment2 = new GesamteListe(); 
       frag = fragment2; 
       break; 
      } 
      return frag; 
     } 

     @Override 
     public int getCount() { 
      // Show 3 total pages. 
      return 3; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      Locale l = Locale.getDefault(); 
      switch (position) { 
      case 0: 
       return getString(R.string.title_section1).toUpperCase(l); 
      case 1: 
       return getString(R.string.title_section2).toUpperCase(l); 
      case 2: 
       return getString(R.string.title_section3).toUpperCase(l); 
      } 
      return null; 
     } 
    } 

    public void updateAll() {  
     menueFragment.updateList(); 
     gefilterteListe.updateList(); 
     gesamteListe.updateList(); 
    } 
} 
+0

你可以在这里放一些片段吗? – squixy 2013-05-05 15:39:43

+0

我添加了代码。 – 2013-05-05 16:26:40

回答

0

尝试类似的东西:

interface DBchangesListener { 
    public void updateList(); 
} 

那么你的片段:

public class MenueFragment extends Fragment implements DBchangesListener {...} 
public class GefilterteListe extends Fragment implements DBchangesListener {...} 
public class GesamteListe extends Fragment implements DBchangesListener {...} 

在活动提出:

private MenueFragment menueFragment; 
private GefilterteListe gefilterteListe; 
private DBchangesListener gesamteListe; 

,然后在onCreate()

menueFragment = new MenueFragment(); 
gefilterteListe = new GefilterteListe(); 
gesamteListe = new GesamteListe(); 

添加您的片段像你现在添加它。
下一页您的活动创造简单的方法:

public void updateAll() { 
    menueFragment.updateList(); 
    gefilterteListe.updateList(); 
    gesamteListe.updateList(); 
} 

在你的DB变化刚刚结束调用updateAll()更新所有片段。

+0

谢谢,但可悲的是我不明白它的工作原理。我在你的例子中创建了一个新的接口(之前从未使用过),并在我的片段中实现它。 你确定我应该这样做:'private DBchangesListener gesamteListe;'? GesamteListe也是我的片段之一。无论如何,我都尝试过。这个onCreate事情对我来说很清楚。 我把updateAll()一次放在onTabSelected中,另一次放在onTabUnselected中。两者都不起作用。 Waht我做错了吗?另外,我添加了我的托管活动,让您更好地了解它。 – 2013-05-05 22:11:28

+0

此外:我将updateAll()放入MenueFragment中,因为数据库在那里被删除,并且也将它放在提到的onTab(Un)Selected中。 – 2013-05-05 22:27:55

+0

我不知道为什么你使用片段而不是活动。也许尝试将您的课程改为活动而不是片段 – squixy 2013-05-06 07:38:39