单击Cardview后如何显示其他活动中的详细信息

时间:2020-02-04 05:44:17

标签: java android

我遇到问题,如何根据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);
                }
            });

        }
    }

点击后,我想在其他活动中显示textViewDatetextViewTitle

这是我的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);


    }

}

我觉得我已经接近答案了,但是我仍然被困住了。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

您可以使用意图将数据从一个活动发送到另一个活动。

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

现在您所需要做的就是添加具有意图的数据,例如

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


    }

附加:-,您应始终检查是否已收到来自意图的数据,并确保textViewDate, textViewTitle不为空。

您还可以在检索if(getIntent().hasExtra("date"))之类的活动中的数据之前进行检查,如果有数据,则为true。

答案 1 :(得分:0)

我认为,除了将职位仅传递给详细活动之外,您还应尝试在意图中传递日期和标题(而不是仅传递位置),然后让它们都进行详细活动以显示它,或者您可以使公告对象可打包,然后以意图的方式将其传递,这样您就可以获取所有详细信息。您可以将标签设置为itemView 使用itemView.getTag(),然后使用{{1}}

获取对象

答案 2 :(得分:0)

我建议您建立一个界面

public interface DeviceClickedInterface {
void DeviceSelectedInterface(int position);
}

,然后在您的适配器中将其传递给Class

private DeviceClickedInterface deviceClickedInterface;

public DevicesAdapter( DeviceClickedInterface deviceClickedInterface, ... the rest of params.) {
    //other params. init.
    this.deviceClickedInterface = deviceClickedInterface;
}

使AnnouncementViewHolder也包含itemView作为子元素,并在onBindViewHolder内的ClickListener上添加itemView

        holder.itemView.setOnClickListener(v -> deviceClickedInterface.DeviceSelectedInterface(position));

最后使DetailActivity实现自己的DeviceClickedInterface,它将覆盖Activity中的DeviceSelectedInterface方法,该方法将在您每次点击CardView时返回位置/ p>