简体   繁体   English

如何使用arrayAdapter中的intent进入第三个活动?

[英]How do I use intent from arrayAdapter to go into a third activity?

I am almost done with this project, and the code is janky, but I am trying to pass data through the arrayadapter to the third activity, but even without data passing the intent wont take it to the third activity. 我几乎完成了这个项目,代码很简陋,但我试图通过arrayadapter传递数据到第三个活动,但即使没有数据传递意图也不会把它带到第三个活动。

I've been looking through stackoverflow, I've changed the context. 我一直在浏览stackoverflow,我已经改变了上下文。 I've tried adding flags and such. 我试过添加标志等。

I have three main activities, one adapter and two custom classes. 我有三个主要活动,一个适配器和两个自定义类。

Also, the list view uses a custom adapter to a different card view depending on data entered, if that matters. 此外,如果重要,列表视图会根据输入的数据将自定义适配器用于其他卡片视图。

public class BookAdaptor extends ArrayAdapter {
    int mLayoutID;
    List<BookList> dataset;
    Context mContext;

    public BookAdaptor(Context context, int resource, List<BookList> objects) {
        super(context, resource, objects);
        mContext = context;
        dataset = objects;
        mLayoutID = resource;
    }

    public class pickedAuthor implements Serializable {
        private String genre;
        private String bookTitle;
        private String synopsis;
        private String bookAuthor;
        private String bookPublisher;
        private String bookImage;
        private int currentPlace;

        public pickedAuthor(String genre, String bookTitle, String synopsis, String bookAuthor, String bookPublisher, String bookImage, int currentPlace) {
            this.genre = genre;
            this.bookTitle = bookTitle;
            this.synopsis = synopsis;
            this.bookAuthor = bookAuthor;
            this.bookPublisher = bookPublisher;
            this.bookImage = bookImage;
            this.currentPlace = currentPlace;
        }

        public int getCurrentPlace() {
            return currentPlace;
        }

        public String getCategoryImage() {
            return genre;
        }

        public String getBookImage() {
            return bookImage;
        }
        public String getBookTitle() {
            return bookTitle;
        }

        public String getBookSynopsis() {
            return synopsis;
        }

        public String getBookAuthor() {
            return bookAuthor;
        }

        public String getBookPublisher() {
            return bookPublisher;
        }

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View currentListViewItem = convertView;

        // Check if the existing view is being reused, otherwise inflate the view
        if (currentListViewItem == null) {
            currentListViewItem = LayoutInflater.from(getContext()).inflate(mLayoutID, parent, false);
        }
        //Get the Number object for the current position
        final BookList currentNumber = dataset.get(position);

        //Set the attributed of list_view_number_item views
        ImageView iconImageView = (ImageView) currentListViewItem.findViewById(R.id.image_view_book_icon);
        int i = mContext.getResources().getIdentifier(
                currentNumber.getBookImage(), "drawable",
                mContext.getPackageName());

        //Setting the icon
        iconImageView.setImageResource(i);

        TextView titleNameTextView = (TextView) currentListViewItem.findViewById(R.id.text_view_book_title);
        titleNameTextView.setText(currentNumber.getBookTitle());

        TextView authorNameTextView = (TextView) currentListViewItem.findViewById(R.id.text_view_author_name);
        authorNameTextView.setText(currentNumber.getBookAuthor());

        TextView publisherNameTextView = (TextView) currentListViewItem.findViewById(R.id.text_view_publisher_name);
        publisherNameTextView.setText(currentNumber.getBookPublisher());


        CardView button = (CardView) currentListViewItem.findViewById(R.id.card_view_list_item);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                int currentDigit;

                intent = new Intent(mContext.getApplicationContext(), DetailsActivity.class);

                String currentGenre,currentTitle, currentBookImage, currentCategoryImage, currentSynopsis, currentAuthor, currentPublisher;
                currentDigit = 1;
                currentGenre = currentNumber.getCategoryImage();
                currentTitle = currentNumber.getBookImage().toString();
                currentBookImage = currentNumber.getBookImage();
                currentSynopsis = currentNumber.getBookSynopsis();
                currentAuthor = currentNumber.getBookAuthor();
                currentPublisher = currentNumber.getBookPublisher();

                pickedAuthor author;
                author = new pickedAuthor(currentGenre, currentTitle, currentSynopsis, currentAuthor, currentPublisher, currentBookImage, currentDigit);


                intent.putExtra("Author", author);
                mContext.getApplicationContext().startActivity(intent);
            }
        });

        return currentListViewItem;
    }
}

        package com.example.bookstore;

        import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.TextView;

    public class DetailsActivity extends AppCompatActivity {
        public BookAdaptor.pickedAuthor author;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_details);

            Intent intent = getIntent();
            author = (BookAdaptor.pickedAuthor) intent.getSerializableExtra("Author Name");

            TextView titleNameTextView = (TextView) findViewById(R.id.text_view_book_title);
            titleNameTextView.setText(author.getBookTitle());

            TextView authorNameTextView = (TextView) findViewById(R.id.details_text_view_author_name);
            authorNameTextView.setText(author.getBookAuthor());

            TextView publisherNameTextView = (TextView) findViewById(R.id.details_text_view_publisher_name);
            publisherNameTextView.setText(author.getBookPublisher());

            TextView synopsisTextView = (TextView) findViewById(R.id.text_view_synopsis);
        synopsisTextView.setText(author.getBookSynopsis());

    }

    }

Change onclick like this 像这样改变onclick

Intent intent = new Intent(v.getContext, ThirdActivity.class);
DataClass data = new DataClass(param1, param2);
intent.putExtra("param", data);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
v.getContext().startActivity(intent);

If it did not work, try changing the Dataclass to implement parcelable and sent parcelable object using intent. 如果它不起作用,请尝试更改Dataclass以使用intent实现parcelable和sent parcelable对象。

Note : Always prefer parcelable over serializable (Parcelable is perfomant) if you have no specific advantage using serializables. 注意:如果您没有使用可序列化的特定优势,则始终优先选择parcelable over serializable(Parcelable是perfomant)。

    intent.putExtra("Author", new Gson().toJson(author));

    Author author= new Gson().fromJson(intent.getStringExtra("Author"), Author.class);

try like this. 试试这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我该如何参加第三项活动? - How do I go to a third activity? 如何设置一个意图,以便我可以将数据发送到第二个活动,然后从那里发送到第三个活动? - How to set an intent so that I can send a data to the second activity and from there to the third activity? 如何将意图从活动传递给扩展LinearLayout的类 - How do I pass an intent from an activity to a class extending LinearLayout 如何从另一个活动调用 ArrayAdapter 以使用 strings.xml 数组从 ArrayAdapter 中删除项目? - How can I call an ArrayAdapter from another activity to delete item from ArrayAdapter with strings.xml array? 如何根据第一活动数据(单击单选按钮)从第二活动转到第三活动或第四活动? - How to go from second activity to third activity or fourth activity based on first activity data (Radio button clicked)? 如何获得使用意图打开的活动的参考? - How do I get the reference of an activity I opened using intent? 如何从ArrayAdapter类更新ListView? - How do I update a ListView from an ArrayAdapter class? 如何使用AlertDialog进行活动? - How to intent activity use AlertDialog? 如何将相机意图设置为主要活动 - How do I set the Camera Intent as the Main Activity 如何将意图数据设置为当前活动editText? - How do I set intent data to current activity editText?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM