[英]How can I display the details in another activity when Cardview is clicked
I am having a problem, how can I display the value of the cardView
based on its position?我遇到了问题,如何根据位置显示
cardView
的值? I feel like I am kinda missing something.我觉得我有点错过了什么。
here is my code这是我的代码
@Override
public void onBindViewHolder(AnnouncementViewHolder holder, int position) {
AnnouncementModel announcement = announcementList.get(position);
if (announcement.getImage()!=null && !announcement.getImage().isEmpty()){
Picasso.get()
.load(announcement.getImage())
// To fit image into imageView
.fit()
// To prevent fade animation
.noFade()
.into(holder.imageView);
}else{
//Do nothing
}
holder.textViewDate.setText(announcement.getDate());
holder.textViewTitle.setText(announcement.getTitle());
holder.textViewExcerpt.setText(announcement.getExcerpt());
}
@Override
public int getItemCount() {
return announcementList.size();
}
class AnnouncementViewHolder extends RecyclerView.ViewHolder {
TextView textViewTitle, textViewExcerpt,textViewDate;
ImageView imageView;
public AnnouncementViewHolder(View itemView) {
super(itemView);
textViewDate = itemView.findViewById(R.id.textAnnouncementDate);
textViewTitle = itemView.findViewById(R.id.textAnnouncementDesc);
textViewExcerpt = itemView.findViewById(R.id.textAnnouncementDesc);
imageView = itemView.findViewById(R.id.AnnouncementImageView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, DetailActivityAnnouncement.class);
intent.putExtra(DetailActivity.EXTRA_POSITION, getAdapterPosition());
context.startActivity(intent);
}
});
}
}
I want to display the textViewDate
and textViewTitle
to my other activity when click.我想在单击时向我的其他活动显示
textViewDate
和textViewTitle
。
Here is my detailActvity
这是我的
detailActvity
public class DetailActivity extends AppCompatActivity {
public static final String EXTRA_POSITION = "position";
AnnouncementModel model;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_announcement);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
int postion = getIntent().getIntExtra(EXTRA_POSITION, 0);
TextView announcementContent = (TextView) findViewById(R.id.textAnnouncementContext);
TextView announcementDate = (TextView) findViewById(R.id.textAnnouncementDate);
ImageView announcementImage = (ImageView) findViewById(R.id.AnnouncementImageView);
TextView announcementOtherDetails = (TextView) findViewById(R.id.textAnnouncementOtherDetails);
}
}
I feel like I am already close to my answer but I am still stuck.我觉得我已经接近我的答案了,但我仍然被困住了。 Any help would be really appreciated.
任何帮助将非常感激。
You can use intent to send data from one activity to another.您可以使用意图将数据从一个活动发送到另一个活动。
In your itemView.setOnClickListener()
you already defined the Intent to open your DetailActivity
.在您的
itemView.setOnClickListener()
您已经定义了打开DetailActivity
的 Intent 。
Now all you need to do is add data with intent like现在您需要做的就是添加带有意图的数据,例如
Context context = v.getContext();
Intent intent = new Intent(context, DetailActivityAnnouncement.class);
intent.putExtra(DetailActivity.EXTRA_POSITION, getAdapterPosition());
// putExtra can be used to send data with intent, here "date"
// and "title" is key that we will need later to access these values
intent.putExtra("date",announcementList.get(getAdapterPosition()).getDate());
intent.putExtra("title", announcementList.get(getAdapterPosition()).getTitle());
context.startActivity(intent);
In your DetailActivity
you can retrieve this information in your onCreate
method在您的
DetailActivity
您可以在onCreate
方法中检索此信息
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_announcement);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
int postion = getIntent().getIntExtra(EXTRA_POSITION, 0);
//you can retrieve data from intent like
String textViewDate = getIntent().getStringExtra("date");
String textViewTitle = getIntent().getStringExtra("title");
TextView announcementContent = (TextView) findViewById(R.id.textAnnouncementContext);
TextView announcementDate = (TextView) findViewById(R.id.textAnnouncementDate);
ImageView announcementImage = (ImageView) findViewById(R.id.AnnouncementImageView);
TextView announcementOtherDetails = (TextView) findViewById(R.id.textAnnouncementOtherDetails);
}
Extra:- you should always check that you have received data from intent and make sure textViewDate, textViewTitle
is not null.额外:-您应该始终检查您是否已从意图接收数据,并确保
textViewDate, textViewTitle
不为空。
you can also check before retrieving data in activity like if(getIntent().hasExtra("date"))
if you have data then it will be true.您还可以在检索活动中的数据之前进行检查,例如
if(getIntent().hasExtra("date"))
如果您有数据,那么它将为真。
I think instead of passing just the position to the detail activity you should try passing date and title in the intent(instead of just position) and then get both of them in detail activity to display it OR you can make your announcement object parcelable and then pass it in the intent itself that way you can get all details.我认为,而不是仅将位置传递给详细信息活动,您应该尝试在意图中传递日期和标题(而不仅仅是位置),然后将它们都放在详细活动中以显示它,或者您可以使您的公告对象可打包,然后将它传递给意图本身,这样您就可以获得所有详细信息。 you can set a tag to the itemView using
itemView.setTag()
and then get the object using itemView.getTag()
您可以使用标签设置为ItemView控件
itemView.setTag()
然后让使用对象itemView.getTag()
I suggest you make an interface我建议你做一个接口
public interface DeviceClickedInterface {
void DeviceSelectedInterface(int position);
}
and in your Adapter pass it to the Class并在您的 Adapter 中将其传递给 Class
private DeviceClickedInterface deviceClickedInterface;
public DevicesAdapter( DeviceClickedInterface deviceClickedInterface, ... the rest of params.) {
//other params. init.
this.deviceClickedInterface = deviceClickedInterface;
}
make the AnnouncementViewHolder
contains the itemView
as child too, and inside the onBindViewHolder
add ClickListener
on the itemView
使
AnnouncementViewHolder
包含itemView
为孩子过了,里面onBindViewHolder
附加ClickListener
在itemView
holder.itemView.setOnClickListener(v -> deviceClickedInterface.DeviceSelectedInterface(position));
at last make the DetailActivity
implements your own DeviceClickedInterface
and it will Override DeviceSelectedInterface
method in your Activity which will return the position each time you click on a CardView
最后使
DetailActivity
实现您自己的DeviceClickedInterface
它将覆盖您的 Activity 中的DeviceSelectedInterface
方法,该方法将在您每次单击CardView
时返回位置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.