简体   繁体   中英

Send object from RecyclerView to new activity onclick

I'd like to be able to send an object from parsed JSON data displayed on a RecyclerView to a new activity on click. Below is the relevant code:

I've tried a few solutions but I don't get any results. I've been getting this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference at athena.sentineljs.com.athena.RVNewsAdapter$NewsViewHolder.onClick(RVNewsAdapter.java:55)

RVNewsAdapter:

public class RVNewsAdapter extends RecyclerView.Adapter<RVNewsAdapter.NewsViewHolder> {

    public static final String KEY_LINK = "link";

    public static class NewsViewHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener {
        private static final String TAG = "Hello";
        CardView cv;
        TextView date;
        TextView link;
        TextView title;
        TextView today;
        List<News> news;


        public NewsViewHolder(View itemView) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.cv);
            date = (TextView) itemView.findViewById(R.id.date);
            link = (TextView) itemView.findViewById(R.id.link);
            title = (TextView) itemView.findViewById(R.id.heading);
            today = (TextView) itemView.findViewById(R.id.today);


            itemView.setOnClickListener(this);
        }


        @Override
        public void onClick(View view) {
            Context context = view.getContext();

            Intent intent;

            intent = new Intent(context, Details.class);
            intent.putExtra("LINK", news.toString());
            context.startActivity(intent);


        }


    }

    List<News> news;

    RVNewsAdapter(List<News> news) {
        this.news = news;
    }


    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public RVNewsAdapter.NewsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.itemnewsfeed, viewGroup, false);
        NewsViewHolder nvh = new NewsViewHolder(v);
        return nvh;
    }

    @Override
    public void onBindViewHolder(NewsViewHolder newsViewHolder, int i) {
        newsViewHolder.date.setText(news.get(i).date);
        newsViewHolder.link.setText(news.get(i).link);
        newsViewHolder.title.setText(news.get(i).title);
        newsViewHolder.today.setText(news.get(i).today);
    }

    @Override
    public int getItemCount() {
        if (news != null) {
            return news.size();
        }
        return 0;
    }
}

Details activity:

public class Details extends AppCompatActivity {

    private static final String TAG_LINK = "link";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Intent in = getIntent();

        // Get JSON values from previous intent
        String link = in.getStringExtra("link");

        // Displaying all values on the screen
        TextView lblLink = (TextView) findViewById(R.id.link);

        lblLink.setText(link);
        Log.d("Details", "Data not shown");

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

You should send the news object like this

@Override
public void onClick(View view) {
    Context context = view.getContext();

    Intent intent = new Intent(context, Details.class);

    int clickPosition = getAdapterPosition();  // get position of clicked item

    News newObject = news.get(clickPosition);   // get clicked new object from news(news is an ArrayList) 
    intent.putExtra("LINK", newObject);

    context.startActivity(intent);
}

Also for pass the object via Intent correctly please see this answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM