将对象从RecyclerView发送到新活动on

时间:2016-06-10 10:37:17

标签: java android android-recyclerview

我希望能够将RecyclerView上显示的已解析JSON数据中的对象发送到点击的新活动。以下是相关代码:

我尝试了一些解决方案,但我没有得到任何结果。我一直在收到这个错误:

  

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String java.lang.Object.toString()'                                                                                   在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;
    }
}

详情活动:

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);
    }
}

1 个答案:

答案 0 :(得分:0)

您应该像这样发送news object

@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);
}

也可以通过Intent正确传递对象,请参阅this answer