为什么我的对象'信息传递给第二个活动标签?

时间:2018-05-08 20:50:18

标签: java android

我无法在ArrayList bookList中获取图书对象,以填充第二项活动中的字段。我能够硬编码填充第二个活动的文本。在将此信息传递给onClickListener时,我无法弄清楚我做错了什么?

public class UsedBooksFragment extends Fragment {

    public static final String TAG = "Details_Fragment";
    static final int REQUEST_BOOK_DETAILS = 1;

    List<Book> bookList = new ArrayList<Book>();

    Book book0 = new Book(1, "1984", "George Orwell",
            "1984 is a dystopian novel depicting an oligarchical, collectivist society.",
            "$6.41", R.drawable.orwell);

    Book book1 = new Book(2, "Hyperspace", "Michio Kaku",
            "A scientific odyssey through parallel universes, time warps, and the 10th dimension",
            "$7.74", R.drawable.hyperspace);

    Book book2 = new Book(3, "Johnny Got His Gun", "Dalton Trumbo",
            "An Army soldier is hit by an artillery shell in World War I and suffers injuries that have all but erased his humanity.",
            "$5.99", R.drawable.johnny);

    Book book3 = new Book(4, "Me Talk Pretty One Day", "David Sedaris",
            "A collection of hilarious autobiographical stories, including David's move to Paris and his attempt at learning French.",
            "$6.79", R.drawable.me_talk);

    Book book4 = new Book(5, "Red Notice", "Bill Browder", "A true story of high finance, murder, and one man\'s fight for justice.",
            "$8.00", R.drawable.red_notice);

    public UsedBooksFragment() {
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        View.OnClickListener callback = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(v.getId() == R.id.imageButton_book0)
                {
                    showDetails(0);
                }
                else if(v.getId() == R.id.imageButton_book1)
                {
                    showDetails(1);
                }
                else if(v.getId() == R.id.imageButton_book2)
                {
                    showDetails(2);
                }
                else if(v.getId() == R.id.imageButton_book3)
                {
                    showDetails(3);
                }
                else if(v.getId() == R.id.imageButton_book4)
                {
                    showDetails(4);
                }
            }
        };

        ImageButton btn0 = (ImageButton) getActivity().findViewById(R.id.imageButton_book0);
        btn0.setOnClickListener(callback);

        ImageButton bt1 = (ImageButton) getActivity().findViewById(R.id.imageButton_book1);
        bt1.setOnClickListener(callback);
    }

    private void showDetails(int index) {
        Intent intent = new Intent(getActivity(), Details.class);
        intent.putExtra(DetailsFragment.TITLE, bookList.get(index).getTitle());
        intent.putExtra(DetailsFragment.AUTHOR, bookList.get(index).getAuthor());
        intent.putExtra(DetailsFragment.PRICE, bookList.get(index).getPrice());
        intent.putExtra(DetailsFragment.DESCRIPTION, bookList.get(index).getDescription());
        intent.putExtra(DetailsFragment.IMAGE, bookList.get(index).getImage());
        startActivityForResult(intent, REQUEST_BOOK_DETAILS);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        bookList.add(book0);
        bookList.add(book1);
        bookList.add(book2);
        bookList.add(book3);
        bookList.add(book4);

        return inflater.inflate(R.layout.fragment_used_books, container, false);

    }
}

0 个答案:

没有答案