目前,我的应用程序有两个(滑动)选项卡形式的碎片。 在一个片段上,用户可以向数据库添加信息。 在另一个片段上,ListView显示DB表的内容。 如果用户添加了一个新的DB-Entry并刷到第二个选项卡,则ListView不会更新,因为我在onCreateView()方法中完成所有加载。
我已经尝试过使用onResume()方法并从我的FragmentActivity(在onPageSelected()方法中)调用Fragment的“update方法”。 onResume()永远不会被调用。 我如果从我的“更新方法”(在我的片段中)调用getActivity(),它总是返回null,但我需要我的数据库连接的活动对象。
My Fragment看起来像这样:
public class ListFragment extends Fragment implements AdapterView.OnItemLongClickListener, OnRefreshListener{
private ListView listView;
private SpeedsArrayAdapter adapter = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View table = inflater.inflate(R.layout.fragment_list, container, false);
listView = (ListView)table.findViewById(R.id.listViewSpeeds);
MySQLiteHelper mSQLHelper = new MySQLiteHelper(getActivity());
List <MichealSpeedBean> mSpeeds = mSQLHelper.getAllSpeedEntries();
adapter = new SpeedsArrayAdapter(getActivity(),
R.layout.list_item, mSpeeds);
listView.setAdapter(adapter);
listView.setOnItemLongClickListener(this);
return table;
}
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
MySQLiteHelper mSQLHelper = new MySQLiteHelper(getActivity());
MichealSpeedBean michealSpeedBean = adapter.getItem(position);
mSQLHelper.deleteSpeedById(michealSpeedBean.getObjectId());
adapter.remove(michealSpeedBean);
Toast.makeText(getActivity(),
"Entry deleted " + position, Toast.LENGTH_LONG)
.show();
return false;
}
@Override
public void onRefresh() {
//getActivity Returns null here, if called form FragmentyActivity
}
我的Fragment Activity看起来像这样:
public class MainActivity extends FragmentActivity {
ViewPager viewPager;
TabPagerAdapter tabAdapter;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabAdapter = new TabPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager)findViewById(R.id.pager);
viewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position);
if (position == 1) {
//Here i tried to update my list from outside of my fragment
((OnRefreshListener)tabAdapter.getItem(position)).onRefresh();
}
}
}
);
viewPager.setAdapter(tabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
@Override
public void onTabReselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
}};
//Add New Tab
actionBar.addTab(actionBar.newTab().setText("General").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Speed entries").setTabListener(tabListener));
}
所以问题是我在哪里/如何更新我的表格内容? 提前谢谢!
答案 0 :(得分:1)
您可以使用以下代码:
public class ListFragment extends Fragment
@Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
if (getActivity() != null )
{
if (visible) {
// update your list, your fragment is visible now
}
}
}
答案 1 :(得分:0)
你可以用这个:
listview.invalidateViews();
享受!