I am using cardview
for displaying items in gridview. I want to open a new activity but as per my code its shows the toast about the item position. I am not getting where i am making mistake so please help me. Help would be appreciate, thanks in advance.
DataModel.class
public class DataModel {
public String text;
public int drawable;
public String color;
public DataModel(String t, int d, String c )
{
text=t;
drawable=d;
color=c;
}
}
RecyclerViewAdapter.class
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
ArrayList<DataModel> mValues;
Context mContext;
protected ItemListener mListener;
public RecyclerViewAdapter(Context context, ArrayList<DataModel> values, ItemListener itemListener) {
mValues = values;
mContext = context;
mListener=itemListener;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView textView;
public ImageView imageView;
public RelativeLayout relativeLayout;
DataModel item;
public ViewHolder(View v) {
super(v);
v.setOnClickListener(this);
textView = (TextView) v.findViewById(R.id.textView);
imageView = (ImageView) v.findViewById(R.id.imageView);
relativeLayout = (RelativeLayout) v.findViewById(R.id.relativeLayout);
}
public void setData(DataModel item) {
this.item = item;
textView.setText(item.text);
imageView.setImageResource(item.drawable);
relativeLayout.setBackgroundColor(Color.parseColor(item.color));
}
@Override
public void onClick(View view) {
if (mListener != null) {
mListener.onItemClick(item);
}
}
}
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.recycler_view_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder Vholder, int position) {
Vholder.setData(mValues.get(position));
}
@Override
public int getItemCount() {
return mValues.size();
}
public interface ItemListener {
void onItemClick(DataModel item);
}
}
MainActivity class
public class MainActivity extends AppCompatActivity implements RecyclerViewAdapter.ItemListener {
RecyclerView recyclerView;
ArrayList<DataModel> arrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
arrayList = new ArrayList<>();
arrayList.add(new DataModel("Item 1", R.drawable.downtoearth, "#FFFFFF"));
arrayList.add(new DataModel("Item 2", R.drawable.beer, "#3E51B1"));
arrayList.add(new DataModel("Item 3", R.drawable.ferrari, "#673BB7"));
arrayList.add(new DataModel("Item 4", R.drawable.jetpack_joyride, "#4BAA50"));
arrayList.add(new DataModel("Item 5", R.drawable.three_d, "#F94336"));
arrayList.add(new DataModel("Item 6", R.drawable.terraria, "#0A9B88"));
RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, arrayList, this);
recyclerView.setAdapter(adapter);
/**
AutoFitGridLayoutManager that auto fits the cells by the column width defined.
**/
AutoFitGridLayoutManager layoutManager = new AutoFitGridLayoutManager(this, 300);
recyclerView.setLayoutManager(layoutManager);
/**
Simple GridLayoutManager that spans two columns
**/
GridLayoutManager manager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(manager);
}
@Override
public void onItemClick(DataModel item) {
//here toast is
Toast.makeText(getApplicationContext(), item.text + " is clicked",
Toast.LENGTH_SHORT).show();
}
}
after edit i am getting error in the intent.putExtra command
@Override public void onItemClick(DataModel item) {
Intent intent = new Intent(MainActivity.this, Yojana.class);
intent.putExtra("dataitem", item);
startActivity(intent);
}
Yojana.class
public class Yojana extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yojana);
getIntent().getParcelableExtra("dataitem");
}
logcaterror :
Error:(56, 15) error: no suitable method found for
putExtra(String,DataModel)
method Intent.putExtra(String,boolean) is not applicable
(argument mismatch; DataModel cannot be converted to boolean)
method Intent.putExtra(String,byte) is not applicable
(argument mismatch; DataModel cannot be converted to byte)
method Intent.putExtra(String,char) is not applicable
(argument mismatch; DataModel cannot be converted to char)
method Intent.putExtra(String,short) is not applicable
(argument mismatch; DataModel cannot be converted to short)
method Intent.putExtra(String,int) is not applicable
(argument mismatch; DataModel cannot be converted to int)
method Intent.putExtra(String,long) is not applicable
(argument mismatch; DataModel cannot be converted to long)
method Intent.putExtra(String,float) is not applicable
(argument mismatch; DataModel cannot be converted to float)
method Intent.putExtra(String,double) is not applicable
(argument mismatch; DataModel cannot be converted to double)
method Intent.putExtra(String,String) is not applicable
(argument mismatch; DataModel cannot be converted to String)
method Intent.putExtra(String,CharSequence) is not applicable
(argument mismatch; DataModel cannot be converted to CharSequence)
method Intent.putExtra(String,Parcelable) is not applicable
(argument mismatch; DataModel cannot be converted to Parcelable)
method Intent.putExtra(String,Parcelable[]) is not applicable
(argument mismatch; DataModel cannot be converted to Parcelable[])
method Intent.putExtra(String,Serializable) is not applicable
(argument mismatch; DataModel cannot be converted to Serializable)
method Intent.putExtra(String,boolean[]) is not applicable
(argument mismatch; DataModel cannot be converted to boolean[])
method Intent.putExtra(String,double[]) is not applicable
(argument mismatch; DataModel cannot be converted to double[])
method Intent.putExtra(String,String[]) is not applicable
(argument mismatch; DataModel cannot be converted to String[])
method Intent.putExtra(String,CharSequence[]) is not applicable
(argument mismatch; DataModel cannot be converted to CharSequence[])
method Intent.putExtra(String,Bundle) is not applicable
(argument mismatch; DataModel cannot be converted to Bundle)
this opens a new activity .but how to open different activities by clicking on different cardviews ?
@Override public void onItemClick(DataModel item) {
Intent intent = new Intent(MainActivity.this, Yojana.class);
intent.putExtra("dataitem", String.valueOf(item));
startActivity(intent);
}
Yojana.class
public class Yojana extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yojana);
getIntent().getExtras().getString("dataitem");
}