gpt4 book ai didi

java - 单击 Cardview 时如何显示另一个 Activity 的详细信息

转载 作者:行者123 更新时间:2023-11-30 05:18:04 25 4
gpt4 key购买 nike

我遇到问题,如何根据 cardView 的位置显示其值?我感觉我好像缺少了一些东西。

这是我的代码

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

}
}

我想在点击时向我的其他 Activity 显示 textViewDatetextViewTitle

这是我的detailActivity

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


}

}

我觉得我已经接近我的答案,但我仍然陷入困境。任何帮助将不胜感激。

最佳答案

您可以使用 Intent 将数据从一个 Activity 发送到另一个 Activity 。

在您的 itemView.setOnClickListener() 中,您已经定义了打开 DetailActivity 的 Intent。

现在您需要做的就是按 Intent 添加数据,例如

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

在您的 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);


}

额外:-您应该始终检查是否已收到来自 Intent 的数据,并确保 textViewDate、textViewTitle 不为空。

您还可以在检索 Activity 中的数据之前进行检查,例如 if(getIntent().hasExtra("date")) 如果您有数据,那么它就是 true。

关于java - 单击 Cardview 时如何显示另一个 Activity 的详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60051275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com