Reputation: 294
I am having a problem, how can I display the value of the cardView
based on its position? 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.
Here is my 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.
Upvotes: 1
Views: 129
Reputation: 16
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()
Upvotes: 0
Reputation: 1374
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
.
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
@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.
you can also check before retrieving data in activity like if(getIntent().hasExtra("date"))
if you have data then it will be true.
Upvotes: 1
Reputation: 739
I suggest you make an interface
public interface DeviceClickedInterface {
void DeviceSelectedInterface(int position);
}
and in your Adapter pass it to the 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
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
Upvotes: 0