I want to update the ListView in a fragment when the dialog is closed. Until now, the update only works when the application is restarted, and I really don't know why.
Here are the classes:
This is a fragment with a ListView on it that I want to update when the dialog is closed.
public class RegisterListFragment extends Fragment { public static final String TAG = "RegisterListFragment"; RegisterListAdapter adapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { setRetainInstance(true);
And this is the Adapter that I populate the List:
public class RegisterListAdapter extends BaseAdapter { View view = null; Database db = new Database(mContext); data=db.getAlltitles(); if(convertView==null) { view = (View) inflater.inflate(R.layout.act__select_listview, null); TextView title = (TextView) view.findViewById(R.id.title); TextView artist = (TextView) view.findViewById(R.id.artist); TextView duration = (TextView) view.findViewById(R.id.duration); ImageView thumb_image = (ImageView) view.findViewById(R.id.list_image); title.setText(data.get(position));
And this is the Basic that holds everything together:
When the positv button of the dialog is pressed, the new data that I want is added to the database
public class MainActivity extends SherlockFragmentActivity implements OnRegisterCreatedListener { RegisterListFragment registerListFragment; HelpFragment helpFragment; boolean helpVisible = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Grund-Layout für die Activity festlegen setContentView(R.layout.act_select_main); addFragments(); supportInvalidateOptionsMenu(); setLargeInLandscape(); } // Was passiert wenn der Menüknopf gedrückt wird // Inflate the menu; this adds items to the action bar if it is present. @Override public boolean onCreateOptionsMenu(Menu menu) { // Zeigt nur die die nicht in Actionbar sind, kann aber eingestellt // werden im XML-File if (helpVisible) { getSupportMenuInflater().inflate( R.menu.act_select_menu_help_visible, menu); } else { getSupportMenuInflater().inflate( R.menu.act_select_menu_registerlist_visible, menu); } return true; } // Was passiert wenn ein Menüpunkt ausgewählt wird @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { // Das wenn der Knopf das Plus war case R.id.menu_help: changeFragment(); // this.supportInvalidateOptionsMenu(); break; case android.R.id.home: changeFragment(); // this.supportInvalidateOptionsMenu(); break; case R.id.menu_add_register: FragmentManager fm = getSupportFragmentManager(); NewRegisterFragment dialog = new NewRegisterFragment(); dialog.setRetainInstance(true); dialog.show(fm, "fragment_name"); break; // Listenelement hinzufügen und dem Adapter sagen es hat sich was // geändert, mach neu } return true; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub switch (keyCode) { case KeyEvent.KEYCODE_BACK: if (helpVisible = true) { // changeFragment(); helpVisible = false; getSupportActionBar().setDisplayHomeAsUpEnabled(false); supportInvalidateOptionsMenu(); } break; } return super.onKeyDown(keyCode, event); } private void addFragments() { // Zuerst versuchen Fragmente aus dem Layout zu laden registerListFragment = (RegisterListFragment) getSupportFragmentManager() .findFragmentById(R.id.registerListFragment); helpFragment = (HelpFragment) getSupportFragmentManager() .findFragmentById(R.id.HelpFragment); // Wenn die Fragmente nicht im Layout sind, füge sie dynamisch hinzu if (registerListFragment == null && helpFragment == null) { FragmentTransaction ft = getSupportFragmentManager() .beginTransaction(); registerListFragment = new RegisterListFragment(); helpFragment = new HelpFragment(); ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, android.R.anim.fade_in, android.R.anim.fade_out); ft.add(R.id.Fragmentframe, registerListFragment); ft.add(R.id.Fragmentframe, helpFragment); ft.show(registerListFragment); ft.hide(helpFragment); ft.commit(); // OnclickListener für Fragmentwechseln } } private void changeFragment() { // Fragmente austauschen FragmentManager fm = getSupportFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); if (registerListFragment.isVisible()) { ft.hide(registerListFragment); ft.show(helpFragment); helpVisible = true; supportInvalidateOptionsMenu(); getSupportActionBar().setDisplayHomeAsUpEnabled(true); if (getSupportFragmentManager().findFragmentByTag( "RegisterListFragment") == null) { ft.addToBackStack("RegisterListFragment"); } } else { ft.hide(helpFragment); helpVisible = false; supportInvalidateOptionsMenu(); getSupportActionBar().setDisplayHomeAsUpEnabled(false); fm.popBackStack(); } ft.commit(); } private void setLargeInLandscape() { if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); findViewById(R.id.menu_help).setVisibility(View.GONE); } if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); findViewById(R.id.menu_help).setVisibility(View.GONE); } } @Override public void OnRegisterCreated() { // TODO Auto-generated method stub registerListFragment.refreshData(); }
to see the “behind” dialog box in the list. I tried so much, but I really did not understand. I hope you help me.
Thanks:)