简体   繁体   English

RecyclerView通过Intent Extras传递数据

[英]RecyclerView passing data via Intent extras

So I'm trying to pass some data via Intent extras to the second activity. 因此,我试图通过Intent Extras将一些数据传递给第二个活动。 This code worked fine with ListView, but now when I switched to RecyclerView it doesn't show any text, text area is blank. 该代码在ListView上可以正常工作,但是现在当我切换到RecyclerView时,它不显示任何文本,文本区域为空白。

Here's the code: (starting in onBindViewHolder()) 这是代码:(从onBindViewHolder()开始)

 holder.container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                passData();
            }
        });

    }

    private void passData() {
        Todo item = new Todo();
        Intent i = new Intent(c, Details.class);
        i.putExtra("nazivTodoa", item.getTitle());
        i.putExtra("datumTodoa", item.getRecordDate());
        i.putExtra("idTodoa", item.getItemId());
        c.startActivity(i);
    }

And this is how I get in in second activity: 这就是我参加第二个活动的方式:

  Bundle extras = getIntent().getExtras();
    String naslov = extras.getString("nazivTodoa");
    String datum = extras.getString("datumTodoa");

    textViewNazivTodoaDetails.setText(naslov);
    textViewDatumTodoaDetails.setText(datum);

What am I doing wrong? 我究竟做错了什么?

What you are doing wrong, is you are not getting the current object that is clicked on. 您做错了什么,是您没有获得当前单击的对象。 You do that by getting your object from the arraylist you use in your adapter. 您可以通过从适配器中使用的数组列表中获取对象来实现。 Do it like that: 那样做:

Arraylist<Item> yourlist = new Arraylist();

       @Override 
        public void onClick(View v) {
            // position = pass the current position of the object you want
            passData(int position);

        } 
    }); 

} 

private void passData(int position) { 
    Todo item = new Todo();
    Intent i = new Intent(c, Details.class);
    i.putExtra("nazivTodoa", yourlist.get(position).getTitle());
    i.putExtra("datumTodoa", yourlist.get(position).item.getRecordDate());
    i.putExtra("idTodoa",    yourlist.get(position).item.getItemId());
    c.startActivity(i);
} 

Like dymmeh said, you need to pass the item in the list to the new activity. 就像dymmeh所说的那样,您需要将列表中的项目传递给新活动。 You are currently passing a newly created empty object. 您当前正在传递一个新创建的空对象。

Instead of 代替

Todo item = new Todo();
Intent i = new Intent(c, Details.class);
i.putExtra("nazivTodoa", item.getTitle());
i.putExtra("datumTodoa", item.getRecordDate());
i.putExtra("idTodoa", item.getItemId());

You should have 你应该有

Todo item = listData.get(itemPosition);
Intent i = new Intent(c, Details.class);
i.putExtra("nazivTodoa", item.getTitle());
i.putExtra("datumTodoa", item.getRecordDate());
i.putExtra("idTodoa", item.getItemId());

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

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