Home > OS >  Constructor is undefined in Fragment
Constructor is undefined in Fragment

Time:10-24

My Code

@Override
  public void onBindViewHolder(ViewHolder _holder, final int _position) {
   View _view = _holder.itemView;
   
   final LinearLayout linear1 = _view.findViewById(R.id.linear1);
   final LinearLayout linear2 = _view.findViewById(R.id.linear2);
   final LinearLayout linear4 = _view.findViewById(R.id.linear4);
   final androidx.cardview.widget.CardView cardview2 = _view.findViewById(R.id.cardview2);
   final LinearLayout linear3 = _view.findViewById(R.id.linear3);
   final LinearLayout linear7 = _view.findViewById(R.id.linear7);
   final ImageView imageview1 = _view.findViewById(R.id.imageview1);
   final TextView textview1 = _view.findViewById(R.id.textview1);
   final TextView textview2 = _view.findViewById(R.id.textview2);
   final LinearLayout linear5 = _view.findViewById(R.id.linear5);
   final TextView textview3 = _view.findViewById(R.id.textview3);
   final TextView textview4 = _view.findViewById(R.id.textview4);
   final LinearLayout linear10 = _view.findViewById(R.id.linear10);
   final ImageView imageview3 = _view.findViewById(R.id.imageview3);
   
 
   duration = Double.parseDouble(videos.get((int)_position).get("videoDuration").toString());
   videoDuration = stringForTime((int)duration);
   textview1.setText(videoDuration);
   path = videos.get((int)_position).get("videoPath").toString();
   //using cardview programmatically inside recyclerview .
   androidx.cardview.widget.CardView cardview1 = new androidx.cardview.widget.CardView(FolderListFragmentActivity.this);
   cardview1.setCardElevation(0);
   cardview1.setRadius(10);
   ViewGroup imageParent = ((ViewGroup)imageview1.getParent()); imageParent.removeView(imageview1);
   cardview1.addView(imageview1);
   imageParent.addView(cardview1);
   textview2.setText(Uri.parse(path).getLastPathSegment());
   size = Double.parseDouble(videos.get((int)_position).get("videoSize").toString());
   humanReadableSize = bytesIntoHumanReadable((long)size);
   textview3.setText(humanReadableSize);
   com.bumptech.glide.Glide.with(getContext().getApplicationContext())
   .load(path)
   .into(imageview1);
   textview4.setText(videos.get((int)_position).get("formattedDate").toString());
  }

And Error is

androidx.cardview.widget.CardView cardview1 = new androidx.cardview.widget.CardView(FolderListFragmentActivity.this);

The constructor CardView(FolderListFragmentActivity) is undefined

CodePudding user response:

Are you in a fragment, activity, or neither?

If you're in an activity, the code should work.

If you're in a fragment, try:

androidx.cardview.widget.CardView cardview1 = new androidx.cardview.widget.CardView(FolderListFragmentActivity.this.getContext());

If you're in neither....

  • Related