简体   繁体   English

单击 Cardview 时如何在另一个活动中显示详细信息

[英]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.我想在单击时向我的其他活动显示textViewDatetextViewTitle

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附加ClickListeneritemView

        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.

相关问题 在Firebase RecyclerView Cardview中单击项目时如何打开详细信息活动 - How to open a details activity when an item is clicked in Firebase RecyclerView Cardview 使用Cardview详细信息启动另一个活动 - Launch another activity with cardview details 当我单击图像按钮时,如何将 ImageButton 放入会导致另一个活动的片段中 - How can I put ImageButton in Fragment that will lead to another activity when I clicked the Image Button 在 android 工作室中单击时,如何使单选按钮在另一个活动中显示文本? - How to make radio buttons display a text in another activity when clicked in android studio? 选中后如何更改cardview的颜色?单击 - how to change the colour of a cardview when selected?clicked 如何在另一个活动中显示数据库数据并显示登录详细信息? - How can i show the database data and show the login details in another Activity? 我如何获取我单击的Cardview的信息以将其发送到下一个活动 - How can i get the info of the cardview i click on to send it to the next activity 在应用程序中单击后退按钮时,如何定义活动? - How can I define the activity when the back-button is clicked in my App? 在 Android java 中单击导航项时如何启动 n 个新活动 - How i can start n new activity when a Navigation item is clicked in Android java 将RecyclerView CardView单击项目数据传递给活动 - Passing RecyclerView CardView Clicked Item Data To Activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM