Hide Tab from Android ActionBar - android

I used the application-setup thing to make a tabs layout. It uses the action bar and a ViewPager in a layout to display a number of tabs. I currently have two tabs.
However I want to add a third tab, that will be hidden unless a special button in one of the tab-fragments is pushed. On button push I thought the application could slide over to the hidden tab and make it visible. Sliding back would make it hidden again.
Is this even possible in Android as it is? :)
How would I implement this with my current form of tab-management? I found some questions like this about people using the TabActivity or TabHosts in the layout. Would it be easier with one of the two implementations?
If you need any source code just ask. Thanks in advance.

Yeah, what you're asking can be done.
In a nutshell:
In your onClickListener for the Button you've designated to make the hidden tab visible, you should call ActionBar.addTab.
Adding the new Fragment
Depending on the layout that contains your Fragments, you could call FragmentTransaction.hide and FragmentTransaction.show, but otherwise I would assume you're adding the Fragment dynamically and therefore using a FragmentPagerAdapter, in which case add your new Fragment to your List.
Links
ActionBar - addTab
FragmentTransaction - hide, show
You should also read up on the Adding Fragments docs.
Here's a very basic example:
ViewPager's Adapter
public class PagerAdapter extends FragmentPagerAdapter {
/**
* The list of {#link Fragment}s used in the adapter
*/
private final List<Fragment> mFragments = new ArrayList<Fragment>();
/**
* Constructor for <code>PagerAdapter</code>
*
* #param fm The {#link FragmentManager} to use.
*/
public PagerAdapter(FragmentManager fm) {
super(fm);
}
/**
* Adds a new {#link Fragment} to the adapter
*
* #param fragment The new {#link Fragment} to add to the list
*/
public void addFragment(Fragment fragment) {
mFragments.add(fragment);
notifyDataSetChanged();
}
/**
* {#inheritDoc}
*/
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
/**
* {#inheritDoc}
*/
#Override
public int getCount() {
return mFragments.size();
}
}
Dummy Fragment
public static final class DummyFragment extends Fragment implements View.OnClickListener {
/**
* Empty constructor as per the {#link Fragment} docs
*/
public DummyFragment() {
}
/**
* #param color The color to make the root view
* #return A new instance of {#link DummyFragment}
*/
public static DummyFragment getInstance(int color) {
final Bundle bundle = new Bundle();
bundle.putInt("color", color);
final DummyFragment fragment = new DummyFragment();
fragment.setArguments(bundle);
return fragment;
}
/**
* {#inheritDoc}
*/
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
rootView.setBackgroundColor(getArguments().getInt("color"));
return rootView;
}
/**
* {#inheritDoc}
*/
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// This should be your Button
view.setOnClickListener(this);
}
/**
* {#inheritDoc}
*/
#Override
public void onClick(View v) {
// This adds the new tab
((MainActivity) getActivity()).addTab(2, Color.BLUE);
}
}
Called to add each Fragment
/**
* Used to add a new {#link Fragment} to {#link ViewPager}'s adapter and
* adds a new {#link Tab} to the {#link ActionBar}.
*
* #param pageTitle The title of the tab
* #param color The background color of the {#link Fragment}
*/
public void addTab(int pageTitle, int color) {
mPagerAdapter.addFragment(DummyFragment.getInstance(color));
mActionBar.addTab(mActionBar.newTab()
.setText("" + pageTitle)
.setTabListener(this));
}

Related

Android update data fragment

It will be like 3 hours I try to update data from a activity to a fragment. I check all pages but it didn't work...
I have to made a BookManager. I have create some class to do it.
I have a main activity with two fragment, a first empty for the moment and a second with the summary of my list of book (most price of book, average...). When I want to add a book, I use a new activity and take data back after add the book (its work).
and I want to refresh the fragment after add the book.
Code of my main activity :
public class MainActivity extends AppCompatActivity {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link 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}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
private SimpleBookManager bookManager;
public final static int ADD_BOOK_REQUEST = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bookManager = new SimpleBookManager();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.button_add_menu) {
Intent intent = new Intent(MainActivity.this, AddBookActivity.class);
startActivityForResult(intent, ADD_BOOK_REQUEST);
return true;
}
return super.onOptionsItemSelected(item);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ADD_BOOK_REQUEST) {
// On vérifie aussi que l'opération s'est bien déroulée
if (resultCode == RESULT_OK) {
// On affiche le bouton qui a été choisi
String[] valueBook = data.getStringArrayExtra("VALUE");
Book book = this.bookManager.createBook();
book.setTitle(valueBook[0]);
book.setAuthor(valueBook[1]);
book.setCourse(valueBook[2]);
book.setIsbm(valueBook[3]);
book.setPrice((int) Integer.parseInt(valueBook[4]));
}
}
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position) {
case 0:
return PlaceholderFragment.newInstance(position + 1);
case 1:
Fragment frag = SummaryFragment.newInstance(bookManager);
return frag;
}
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 2 total pages.
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "COLLECTION";
case 1:
return "SUMMARY";
}
return null;
}
}
In the method onActivityResult, I add the book and I want here to refresh or eventuelly when I'm back to the summary fragment. I tried with a FragmentTransaction, but the commit didn't work, because I don't have a tag to get my fragment (I don't know how it works to get one)... And the we don't have instance of the fragment because we use a static method to create it.
Code of my fragment :
public class SummaryFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String BOOKMANAGER = "book";
// TODO: Rename and change types of parameters
private String[] info;
private OnFragmentInteractionListener mListener;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #return A new instance of fragment SummaryFragment.
*/
// TODO: Rename and change types and number of parameters
public static SummaryFragment newInstance(SimpleBookManager bookManager) {
String[] info = new String[5];
info[0] = String.valueOf(bookManager.count());
info[1] = String.valueOf(bookManager.getTotalCost());
info[2] = String.valueOf(bookManager.getMaxPrice());
info[3] = String.valueOf(bookManager.getMinPrice());
info[4] = String.valueOf(bookManager.getMeanPrice());
SummaryFragment fragment = new SummaryFragment();
Bundle args = new Bundle();
args.putStringArray(BOOKMANAGER, info);
fragment.setArguments(args);
return fragment;
}
public SummaryFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
this.info = getArguments().getStringArray(BOOKMANAGER);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView;
TextView textView;
rootView = inflater.inflate(R.layout.fragment_summary, container, false);
textView = (TextView) rootView.findViewById(R.id.nbbook_value);
textView.setText(this.info[0]);
textView = (TextView) rootView.findViewById(R.id.totalcost_value);
textView.setText(this.info[1] + " SEK");
textView = (TextView) rootView.findViewById(R.id.mostprice_value);
textView.setText(this.info[2] + " SEK");
textView = (TextView) rootView.findViewById(R.id.leastprice_value);
textView.setText(this.info[3] + " SEK");
textView = (TextView) rootView.findViewById(R.id.averageprice_value);
textView.setText(this.info[4] + " SEK");
return rootView;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
Activity a;
if (context instanceof Activity){
a=(Activity) context;
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}
}
You can get the Fragment instance by calling FragmentPagerAdapter's getItem method.
SummaryFragment frag = (SummaryFragment) mSectionsPagerAdapter.getItem(1);
And then call a method in the Fragment instance to refresh display, make sure to check null before calling method on the Fragment.
if (frag != null) {
frag.refreshBook(book);
}

ANDROID: getActionBar() produces a null pointer exception with a tab / fragment activity

Basically I'm making a tab activity that has fragments of other activities as the tabs. However when it runs, I am recieving a null pointer exception at the line:
final ActionBar actionBar = getActionBar();
Here is the whole activity. It is taken from the google sample.
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_main);
// Set up the action bar. The navigation mode is set to NAVIGATION_MODE_TABS, which will
// cause the ActionBar to render a set of tabs. Note that these tabs are *not* rendered
// by the ViewPager; additional logic is lower in this file to synchronize the ViewPager
// state with the tab state. (See mViewPager.setOnPageChangeListener() and onTabSelected().)
// BEGIN_INCLUDE (set_navigation_mode)
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// END_INCLUDE (set_navigation_mode)
// BEGIN_INCLUDE (setup_view_pager)
// 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);
// END_INCLUDE (setup_view_pager)
// 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.
// BEGIN_INCLUDE (page_change_listener)
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// END_INCLUDE (page_change_listener)
// BEGIN_INCLUDE (add_tabs)
// 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));
}
// END_INCLUDE (add_tabs)
}
/**
* Update {#link ViewPager} after a tab has been selected in the ActionBar.
*
* #param tab Tab that was selected.
* #param fragmentTransaction A {#link android.app.FragmentTransaction} for queuing fragment operations to
* execute once this method returns. This FragmentTransaction does
* not support being added to the back stack.
*/
// BEGIN_INCLUDE (on_tab_selected)
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, tell the ViewPager to switch to the corresponding page.
mViewPager.setCurrentItem(tab.getPosition());
}
// END_INCLUDE (on_tab_selected)
/**
* Unused. Required for {#link android.app.ActionBar.TabListener}.
*/
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
/**
* Unused. Required for {#link android.app.ActionBar.TabListener}.
*/
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
// BEGIN_INCLUDE (fragment_pager_adapter)
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages. This provides the data for the {#link ViewPager}.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
// END_INCLUDE (fragment_pager_adapter)
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
// BEGIN_INCLUDE (fragment_pager_adapter_getitem)
/**
* Get fragment corresponding to a specific position. This will be used to populate the
* contents of the {#link ViewPager}.
*
* #param position Position to fetch fragment for.
* #return Fragment for specified position.
*/
#Override
public Fragment getItem(int position) {
// 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:
return new SearchPlayerFragment();
case 1:
return new SearchPlayerFragment();
case 2:
return new SearchPlayerFragment();
default:
return new SearchPlayerFragment();
}
}
// END_INCLUDE (fragment_pager_adapter_getitem)
// BEGIN_INCLUDE (fragment_pager_adapter_getcount)
/**
* Get number of pages the {#link ViewPager} should render.
*
* #return Number of fragments to be rendered as pages.
*/
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
// END_INCLUDE (fragment_pager_adapter_getcount)
// BEGIN_INCLUDE (fragment_pager_adapter_getpagetitle)
/**
* Get title for each of the pages. This will be displayed on each of the tabs.
*
* #param position Page to fetch title for.
* #return Title for specified page.
*/
#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;
}
// END_INCLUDE (fragment_pager_adapter_getpagetitle)
}
}
getActionBar() returns null because you are extending FragmentActivity.
Extend AppCompactActivity instead.
Try calling the Support Action Bar instead
getSupportActionBar();
I suggest you inherit from an ActionBarActivity instead of a FragmentActivity

How can I send my Database and other Objects from MainActivity to my Fragment?

I've just started working with Android and I find this so hard to understand. What I have is three different fragments and I'm using a NavigationDrawerFragment to go between the fragments. I want to display information from my database in these different fragmets in ListViews.
This is the code in my MainActivity that creates the Database and how the app goes between fragments.
...
private NavigationDrawerFragment mNavigationDrawerFragment;
private DatabaseHandler dbHandler;
private CollectionEmployee allEmployees;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up database.
dbHandler = new DatabaseHandler(this);
allEmployees = new CollectionEmployee(dbHandler.getAllContacts(), dbHandler);
allEmployees.addEmployee(new Employee(10,"Kim D","07","da#gmail.com","ki3"));
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment myFragment;
switch(position){
case 0: myFragment = new FragmentEmoployees();
break;
case 1: myFragment = new FragmentProjects();
break;
case 2: myFragment = new FragmentDomains();
break;
default: myFragment = new FragmentEmoployees();
break;
}
fragmentManager.beginTransaction()
.replace(R.id.container, myFragment)
.commit();
}
...
This is my class FragmentEmployee where I want to display all the employees, from the Database in my MainActivity, in a ListView.
public class FragmentEmoployees extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
private ListView listViewEmployees;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment FragmentEmoployees.
*/
// TODO: Rename and change types and number of parameters
public static FragmentEmoployees newInstance(String param1, String param2) {
FragmentEmoployees fragment = new FragmentEmoployees();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_employee, container, false);
listViewEmployees = (ListView) view.findViewById(R.id.listViewEmployees);
//Set up listview so it displays all employees from database.. but how?...
return view;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}
The information of the employees are in a Collection-class so I find it relevant to just send variable "allEmployees" to my EmployeeFragment. But the only help I can find is how to send simple Strings by using Bundle and getArguments(). Could I make my class variables in MainActivity static or is that a bad idea? I'm trying to have loose coupling.
Thanks alot in advance.
You can use Intent or Bundle to pass information from one activity to other Fragments.
Example:
Simple example for Intent and Bundle

LongClick to Delete List Item (Using Fragments in Master-Detail)

I've created a Master-Detail Flow application, and I'm using all of the default files that Eclipse/Android creates. I created a project using the template, and haven't modified anything in it, except to add the methods discussed below for deleting the item.
I need to implement an onItemLongClickListener so that when I longpress an item in the list, that item is deleted. I don't want an alert or a confirmation or to select multiple items, I just want the one I longclick to go away.
I've found several tutorials on the subject, but my issue is that they aren't using fragments, so I'm getting a bit confused about what methods go where. (This is the one I'm mostly working with: AndroidForBeginners)
If I'm understanding correctly, I should only be using the ItemListActivity and ItemListFragment. I understand that methods in the Fragment will be called in the Activity (such as the default onItemSelected that comes with the template). I understand that the removeItemFromList method (from the linked tutorial) removes the item from the array and notifies the adapter to update the list. My problem is, I don't know where the array and adapter are within the Master-Detail fragments and activities. There's an ArrayList in the DummyContent, so I thought maybe if I call removeItem in the fragment, then send it to the Activity, then call it from the DummyContent class it would work. But it doesn't, and I'm stuck.
Any advice would be much appreciated!
As requested, here is the code I'm currently using. As I said, just the default android template.
ItemListActivity.java
package Andrea.deletelistitem;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
/**
* An activity representing a list of Items. This activity
* has different presentations for handset and tablet-size devices. On
* handsets, the activity presents a list of items, which when touched,
* lead to a {#link ItemDetailActivity} representing
* item details. On tablets, the activity presents the list of items and
* item details side-by-side using two vertical panes.
* <p>
* The activity makes heavy use of fragments. The list of items is a
* {#link ItemListFragment} and the item details
* (if present) is a {#link ItemDetailFragment}.
* <p>
* This activity also implements the required
* {#link ItemListFragment.Callbacks} interface
* to listen for item selections.
*/
public class ItemListActivity extends FragmentActivity
implements ItemListFragment.Callbacks {
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
private boolean mTwoPane;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
if (findViewById(R.id.item_detail_container) != null) {
// The detail container view will be present only in the
// large-screen layouts (res/values-large and
// res/values-sw600dp). If this view is present, then the
// activity should be in two-pane mode.
mTwoPane = true;
// In two-pane mode, list items should be given the
// 'activated' state when touched.
((ItemListFragment) getSupportFragmentManager()
.findFragmentById(R.id.item_list))
.setActivateOnItemClick(true);
}
// TODO: If exposing deep links into your app, handle intents here.
}
/**
* Callback method from {#link ItemListFragment.Callbacks}
* indicating that the item with the given ID was selected.
*/
#Override
public void onItemSelected(String id) {
if (mTwoPane) {
// In two-pane mode, show the detail view in this activity by
// adding or replacing the detail fragment using a
// fragment transaction.
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, fragment)
.commit();
} else {
// In single-pane mode, simply start the detail activity
// for the selected item ID.
Intent detailIntent = new Intent(this, ItemDetailActivity.class);
detailIntent.putExtra(ItemDetailFragment.ARG_ITEM_ID, id);
startActivity(detailIntent);
}
}
}
ItemListFragment.java
package Andrea.deletelistitem;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import Andrea.deletelistitem.dummy.DummyContent;
/**
* A list fragment representing a list of Items. This fragment
* also supports tablet devices by allowing list items to be given an
* 'activated' state upon selection. This helps indicate which item is
* currently being viewed in a {#link ItemDetailFragment}.
* <p>
* Activities containing this fragment MUST implement the {#link Callbacks}
* interface.
*/
public class ItemListFragment extends ListFragment {
/**
* The serialization (saved instance state) Bundle key representing the
* activated item position. Only used on tablets.
*/
private static final String STATE_ACTIVATED_POSITION = "activated_position";
/**
* The fragment's current callback object, which is notified of list item
* clicks.
*/
private Callbacks mCallbacks = sDummyCallbacks;
/**
* The current activated item position. Only used on tablets.
*/
private int mActivatedPosition = ListView.INVALID_POSITION;
/**
* A callback interface that all activities containing this fragment must
* implement. This mechanism allows activities to be notified of item
* selections.
*/
public interface Callbacks {
/**
* Callback for when an item has been selected.
*/
public void onItemSelected(String id);
}
/**
* A dummy implementation of the {#link Callbacks} interface that does
* nothing. Used only when this fragment is not attached to an activity.
*/
private static Callbacks sDummyCallbacks = new Callbacks() {
#Override
public void onItemSelected(String id) {
}
};
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public ItemListFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO: replace with a real list adapter.
setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(
getActivity(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
DummyContent.ITEMS));
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Restore the previously serialized activated item position.
if (savedInstanceState != null
&& savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
}
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Activities containing this fragment must implement its callbacks.
if (!(activity instanceof Callbacks)) {
throw new IllegalStateException("Activity must implement fragment's callbacks.");
}
mCallbacks = (Callbacks) activity;
}
#Override
public void onDetach() {
super.onDetach();
// Reset the active callbacks interface to the dummy implementation.
mCallbacks = sDummyCallbacks;
}
#Override
public void onListItemClick(ListView listView, View view, int position, long id) {
super.onListItemClick(listView, view, position, id);
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mActivatedPosition != ListView.INVALID_POSITION) {
// Serialize and persist the activated item position.
outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
}
}
/**
* Turns on activate-on-click mode. When this mode is on, list items will be
* given the 'activated' state when touched.
*/
public void setActivateOnItemClick(boolean activateOnItemClick) {
// When setting CHOICE_MODE_SINGLE, ListView will automatically
// give items the 'activated' state when touched.
getListView().setChoiceMode(activateOnItemClick
? ListView.CHOICE_MODE_SINGLE
: ListView.CHOICE_MODE_NONE);
}
private void setActivatedPosition(int position) {
if (position == ListView.INVALID_POSITION) {
getListView().setItemChecked(mActivatedPosition, false);
} else {
getListView().setItemChecked(position, true);
}
mActivatedPosition = position;
}
}
DummyContent.java
package Andrea.deletelistitem.dummy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Helper class for providing sample content for user interfaces created by
* Android template wizards.
* <p>
* TODO: Replace all uses of this class before publishing your app.
*/
public class DummyContent {
/**
* An array of sample (dummy) items.
*/
public static List<DummyItem> ITEMS = new ArrayList<DummyItem>();
/**
* A map of sample (dummy) items, by ID.
*/
public static Map<String, DummyItem> ITEM_MAP = new HashMap<String, DummyItem>();
static {
// Add 3 sample items.
addItem(new DummyItem("1", "Item 1"));
addItem(new DummyItem("2", "Item 2"));
addItem(new DummyItem("3", "Item 3"));
}
private static void addItem(DummyItem item) {
ITEMS.add(item);
ITEM_MAP.put(item.id, item);
}
/**
* A dummy item representing a piece of content.
*/
public static class DummyItem {
public String id;
public String content;
public DummyItem(String id, String content) {
this.id = id;
this.content = content;
}
#Override
public String toString() {
return content;
}
}
}
This is the method from the tutorial:
protected void removeItemFromList(int position) {
final int deletePosition = position;
AlertDialog.Builder alert = new AlertDialog.Builder(
MainActivity.this);
alert.setTitle("Delete");
alert.setMessage("Do you want delete this item?");
alert.setPositiveButton("YES", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TOD O Auto-generated method stub
// main code on after clicking yes
arr.remove(deletePosition);
adapter.notifyDataSetChanged();
adapter.notifyDataSetInvalidated();
}
});
alert.setNegativeButton("CANCEL", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
alert.show();
}
This is the method with my modifications, to remove the confirmation alert:
protected void removeItemFromList(int position) {
arr.remove(position);
adapter.notifyDataSetChanged();
adapter.notifyDataSetInvalidated();
}
In this method, I think ITEMS from the DummyContent would replace the arr array, because it's an ArrayList. I'm not sure about the adapter, though.
There's a method on any Fragment called onActivityCreated You want to #Override that method.
Then, inside it, using getListView() you can attach an onLongItemClickListener().
Like so:
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//Here you implement your code. Basically delete an item from
// the underlying data structure of your adapter, and then...
setListAdapter(.....); //re-set the list adapter.
return false;
}
});

Passing message from Activity to Fragment

I seem to have a problem passing data gotten in an Activity to a Fragment. The data does not appear in the listfragment!
Here is my listfragmentactivity. I reinstantiate the fragment every time I add an item to the list aka bundle an item and use the constructor in the new fragment to do it.
package com.example.sample;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.Toast;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
/**
* An activity representing a list of Courses. This activity has different
* presentations for handset and tablet-size devices. On handsets, the activity
* presents a list of items, which when touched, lead to a
* {#link CourseDetailActivity} representing item details. On tablets, the
* activity presents the list of items and item details side-by-side using two
* vertical panes.
* <p>
* The activity makes heavy use of fragments. The list of items is a
* {#link CourseListFragment} and the item details (if present) is a
* {#link CourseDetailFragment}.
* <p>
* This activity also implements the required
* {#link CourseListFragment.Callbacks} interface to listen for item selections.
*/
public class CourseListActivity extends SherlockFragmentActivity implements
CourseListFragment.Callbacks {
CourseListFragment listFrag;
public static String courseName;
private static final int REQUEST_CODE = 10;
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
private boolean mTwoPane;
private boolean once = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course_list);
if (findViewById(R.id.course_detail_container) != null) {
// The detail container view will be present only in the
// large-screen layouts (res/values-large and
// res/values-sw600dp). If this view is present, then the
// activity should be in two-pane mode.
mTwoPane = true;
// In two-pane mode, list items should be given the
// 'activated' state when touched.
listFrag = ((CourseListFragment) getSupportFragmentManager().findFragmentById(
R.id.course_list));
listFrag.setActivateOnItemClick(true);
}
// TODO: If exposing deep links into your app, handle intents here.
}
/**
* Callback method from {#link CourseListFragment.Callbacks} indicating that
* the item with the given ID was selected.
*/
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getSupportMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
boolean bool;
switch (item.getItemId()) {
case R.id.add_course:
Intent intent = new Intent(this, CourseAddActivity.class);
startActivityForResult(intent, REQUEST_CODE);
bool = true;
default:
bool = super.onOptionsItemSelected(item);
}
return bool;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
if (data.hasExtra("courseName")) {
courseName = data.getExtras().getString("courseName");
Bundle args = new Bundle();
args.putString("courseKey", courseName);
listFrag = new CourseListFragment(args);
}
}
}
#Override
public void onItemSelected(String id) {
if (mTwoPane) {
// In two-pane mode, show the detail view in this activity by
// adding or replacing the detail fragment using a
// fragment transaction.
if (once) {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// initiating both tabs and set text to it.
ActionBar.Tab assignTab = actionBar.newTab().setText(
"Assignments");
ActionBar.Tab schedTab = actionBar.newTab().setText("Schedule");
ActionBar.Tab contactTab = actionBar.newTab()
.setText("Contact");
// Create three fragments to display content
Fragment assignFragment = new Assignments();
Fragment schedFragment = new Schedule();
Fragment contactFragment = new Contact();
assignTab.setTabListener(new MyTabsListener(assignFragment));
schedTab.setTabListener(new MyTabsListener(schedFragment));
contactTab.setTabListener(new MyTabsListener(contactFragment));
actionBar.addTab(assignTab);
actionBar.addTab(schedTab);
actionBar.addTab(contactTab);
once = false;
}
} else {
// In single-pane mode, simply start the detail activity
// for the selected item ID.
Intent detailIntent = new Intent(this, CourseDetailActivity.class);
startActivity(detailIntent);
}
}
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.course_detail_container, fragment);
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}
}
And here is my ListFragment, where I create two constructors, one which accepts arguments.
package com.example.sample;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockListFragment;
/**
* A list fragment representing a list of Courses. This fragment also supports
* tablet devices by allowing list items to be given an 'activated' state upon
* selection. This helps indicate which item is currently being viewed in a
* {#link CourseDetailFragment}.
* <p>
* Activities containing this fragment MUST implement the {#link Callbacks}
* interface.
*/
public class CourseListFragment extends SherlockListFragment {
private static String courseName;
ArrayList<String> courseItems;
ArrayAdapter<String> adapter;
/**
* The serialization (saved instance state) Bundle key representing the
* activated item position. Only used on tablets.
*/
private static final String STATE_ACTIVATED_POSITION = "activated_position";
/**
* The fragment's current callback object, which is notified of list item
* clicks.
*/
private Callbacks mCallbacks = sDummyCallbacks;
/**
* The current activated item position. Only used on tablets.
*/
private int mActivatedPosition = ListView.INVALID_POSITION;
/**
* A callback interface that all activities containing this fragment must
* implement. This mechanism allows activities to be notified of item
* selections.
*/
public interface Callbacks {
/**
* Callback for when an item has been selected.
*/
public void onItemSelected(String id);
}
/**
* A dummy implementation of the {#link Callbacks} interface that does
* nothing. Used only when this fragment is not attached to an activity.
*/
private static Callbacks sDummyCallbacks = new Callbacks() {
#Override
public void onItemSelected(String id) {
}
};
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public CourseListFragment() {
}
public CourseListFragment(Bundle args) {
courseName = args.get("courseKey").toString();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
courseItems = new ArrayList<String>();
adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, courseItems);
// TODO: replace with a real list adapter.
int layout = (Build.VERSION.SDK_INT >= 11) ? android.R.layout.simple_list_item_activated_1
: android.R.layout.simple_list_item_1;
setListAdapter(adapter);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Restore the previously serialized activated item position.
if (savedInstanceState != null
&& savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
setActivatedPosition(savedInstanceState
.getInt(STATE_ACTIVATED_POSITION));
courseItems.add(courseName);
adapter.notifyDataSetChanged();
}
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Activities containing this fragment must implement its callbacks.
if (!(activity instanceof Callbacks)) {
throw new IllegalStateException(
"Activity must implement fragment's callbacks.");
}
mCallbacks = (Callbacks) activity;
}
#Override
public void onDetach() {
super.onDetach();
// Reset the active callbacks interface to the dummy implementation.
mCallbacks = sDummyCallbacks;
}
#Override
public void onListItemClick(ListView listView, View view, int position,
long id) {
super.onListItemClick(listView, view, position, id);
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mActivatedPosition != ListView.INVALID_POSITION) {
// Serialize and persist the activated item position.
outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
}
}
/**
* Turns on activate-on-click mode. When this mode is on, list items will be
* given the 'activated' state when touched.
*/
public void setActivateOnItemClick(boolean activateOnItemClick) {
// When setting CHOICE_MODE_SINGLE, ListView will automatically
// give items the 'activated' state when touched.
getListView().setChoiceMode(
activateOnItemClick ? ListView.CHOICE_MODE_SINGLE
: ListView.CHOICE_MODE_NONE);
}
private void setActivatedPosition(int position) {
if (position == ListView.INVALID_POSITION) {
getListView().setItemChecked(mActivatedPosition, false);
} else {
getListView().setItemChecked(position, true);
}
mActivatedPosition = position;
}
}
Any help at all is much appreciated!
Thank You!
If you re-instantiate like this, class variables of CourseListFragment will be destroyed each time you call
listFrag = new CourseListFragment(args);
You should create a method in CourseListFragment to add a course name without destroying the current fragment:
public void addCourse(String courseName) {
courseItems.add(courseName);
adapter.notifyDataSetChanged();
}
And in your activity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
if (data.hasExtra("courseName")) {
courseName = data.getExtras().getString("courseName");
listFrag.addCourse(courseName);
}
}
}

Categories

Resources