Question
public class MainActivity extends AppCompatActivity implements BookListFragment.BookSelectedInterface { FragmentManager fm; boolean twoPane; BookDetailsFragment bookDetailsFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); twoPane = findViewById(R.id.container2)
public class MainActivity extends AppCompatActivity implements BookListFragment.BookSelectedInterface {
FragmentManager fm;
boolean twoPane;
BookDetailsFragment bookDetailsFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
twoPane = findViewById(R.id.container2) != null;
fm = getSupportFragmentManager();
fm.beginTransaction()
.replace(R.id.container1, BookListFragment.newInstance(getTestBooks()))
.commit();
/*
If we have two containers available, load a single instance
of BookDetailsFragment to display all selected books
*/
if (twoPane) {
bookDetailsFragment = new BookDetailsFragment();
fm.beginTransaction()
.replace(R.id.container2, bookDetailsFragment)
.commit();
}
}
/*
Generate an arbitrary list of "books" for testing
*/
private ArrayList> getTestBooks() {,>
ArrayList> books = new ArrayList>();,>,>
HashMap book;,>
for (int i = 0; i
book = new HashMap();,>
book.put("title", "Book" + i);
book.put("author", "Author" + i);
books.add(book);
}
return books;
}
@Override
public void bookSelected(int index) {
if (twoPane)
/*
Display selected book using previously attached fragment
*/
bookDetailsFragment.displayBook(getTestBooks().get(index));
else {
/*
Display book using new fragment
*/
fm.beginTransaction()
.replace(R.id.container1, BookDetailsFragment.newInstance(getTestBooks().get(index)))
// Transaction is reversible
.addToBackStack(null)
.commit();
}
}
}
public class BookListFragment extends Fragment {
private static final String BOOK_LIST_KEY = "booklist";
private ArrayList books;
private BookSelectedInterface parentActivity;
public BookListFragment() {}
static BookListFragment newInstance(ArrayList> books) {,>
BookListFragment fragment = new BookListFragment();
Bundle args = new Bundle();
/*
A HashMap implements the Serializable interface
therefore we can place a HashMap inside a bundle
by using that put() method.
*/
args.putSerializable(BOOK_LIST_KEY, books);
fragment.setArguments(args);
return fragment;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
/*
This fragment needs to communicate with its parent activity
so we verify that the activity implemented our known interface
*/
if (context instanceof BookSelectedInterface) {
parentActivity = (BookSelectedInterface) context;
} else {
throw new RuntimeException("Please implement the required interface(s)");
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
books = (ArrayList) getArguments().getSerializable(BOOK_LIST_KEY);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ListView listView = (ListView) inflater.inflate(R.layout.fragment_book_list, container, false);
listView.setAdapter(new BooksAdapter(getContext(), books));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView?> parent, View view, int position, long id) {
parentActivity.bookSelected(position);
}
});
return listView;
}
/*
Interface for communicating with attached activity
*/
interface BookSelectedInterface {
void bookSelected(int index);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started