I am currently working on an emergency app that dials service short codes or numbers when a user clicks on a card.
So far, i have been able to achieve the dialing feature but i want to start a new activity from one of the cards instead of dialing a number.
I tried using an intent to start the new activity from the OnClick Method but my app crashes while other cards works perfectly well
Below is the error message generated by the IDE and the CardviewDataAdapter.java code.
CardViewDataAdapter.java Code:
public class CardViewDataAdapter extends RecyclerView.Adapter<CardViewDataAdapter.ViewHolder> {
private List<ItemObject> itemList;
public CardViewDataAdapter(Context context, List<ItemObject> itemList) {
this.itemList = itemList;
}
// Create new views (invoked by the layout manager)
@Override
public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.cardview_row, null);
//Todo: set the view's size, margins, paddings and layout parameters
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
// - get data from your itemsData at this position
// - replace the contents of the view with that itemsData
viewHolder.image_view.setImageResource(itemList.get(position).getPhoto());
viewHolder.image_name.setText(itemList.get(position).getName());
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return this.itemList.size();
}
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
// inner class to hold a reference to each item of RecyclerView
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// each data item is a string and an image in this case
public ImageView image_view;
public TextView image_name;
public Context context;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
this.image_view = (ImageView) itemLayoutView.findViewById(R.id.image_view);
this.image_name = (TextView) itemLayoutView.findViewById(R.id.image_name);
// Attach a click listener to the entire row view
itemLayoutView.setOnClickListener(this);
itemLayoutView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(v.getContext(), "OnLongClick Version :" + getAdapterPosition(),
Toast.LENGTH_SHORT).show();
return true;
}
});
}
// Handles the row being being clicked
@Override
public void onClick(View view) {
if (getAdapterPosition() == 0) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:123"));
view.getContext().startActivity(callIntent);
} else if (getAdapterPosition() == 1) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:1234"));
view.getContext().startActivity(callIntent);
} else if (getAdapterPosition() == 2) {
//intent = new Intent(mContext, TutorialActivity.class);
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:12345"));
view.getContext().startActivity(callIntent);
} else if (getAdapterPosition() == 3) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:123456"));
view.getContext().startActivity(callIntent);
}else if (getAdapterPosition() == 4){
view.getContext().startActivity(new Intent(view.getContext(),safetyActivity.class));
}
else {
view.getContext().startActivity(new Intent(view.getContext(),safetyActivity.class));
}
}
}
}