我有一个小应用程序,我在其中使用 eclipse 创建了三个可滑动的标签。其中一个是公共片段,而第二个和第三个是 ListFragments,它们每个都从 Cursor 获取数据。
我对它进行了实验,并想删除数据库的内容,该内容是在托管活动的 onCreate 中创建的。我的问题是,当我删除数据时,第二个选项卡(ListFragments 之一)没有更新,而第三个选项卡是。
随着第二个标签的更新,当我让手机进入睡眠状态并重新唤醒它时,我认为,第二个标签并没有进入它的生命周期,所以它不会调用 onResume。
两个 ListFragment 都使用相同的代码进行更新:
@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 总是想将我的 Fragments 的类型更改为另一个 Fragment 类,并且在我想使用 support.v4 时想要 android.app.fragment ...
如何让它工作,以便刷新两个 ListFragments?
我是 Fragments 的新手,我知道 Cursor-class 已被贬值,但我还没有时间了解 Loaders...:/
编辑:这是我不工作的 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();
}
}