Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Combinatorial Testing In Cloud Computing

Authors: Wei-Tek Tsai ,Guanqiu Qi

1st Edition

9811044805, 978-9811044809

More Books

Students also viewed these Programming questions

Question

How do we do subnetting in IPv6?Explain with a suitable example.

Answered: 1 week ago

Question

Explain the guideline for job description.

Answered: 1 week ago

Question

What is job description ? State the uses of job description.

Answered: 1 week ago

Question

What are the objectives of job evaluation ?

Answered: 1 week ago

Question

Write a note on job design.

Answered: 1 week ago