Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – Create an instance by a string name input

Posted on November 14, 2022

Solution 1 :

I suggest you to create a method and make filterList.getSelectedItem().toString() into a variable. Then make use of reflection in creating a class

something like

String item = filterList.getSelectedItem().toString();

apply(createInstance(item))
public Object createInstance(String item){
  Class classDefinition = Class.forName(item);
  return classDefinition.newInstance();
}

But you have to add further validations there if the item exists or not.
But the idea is like that. Take advantage of reflection instead of using new keyword

Problem :

I am working on CS50 Android track Fiftygram app. My code is working but I don’t like to see copy pasted codes.

Currently checking dropdown selected item name and using if/else if to call with representing instance of a Transformation. How can I directly call apply function with the string without using all these if and else ifs.

If I can find a way, I can fix some of the wording. For example, I can get rid of Transformation end of the strings and add it myself before calling the function.

public void applyFilter(View view) {
    if (filterList.getSelectedItem().toString() != null) {
        if (filterList.getSelectedItem().toString().equals("ToonFilterTransformation")) {
            apply(new ToonFilterTransformation());
        }
        else if (filterList.getSelectedItem().toString().equals("SepiaFilterTransformation")) {
            apply(new SepiaFilterTransformation());
        }
        else if (filterList.getSelectedItem().toString().equals("ContrastFilterTransformation")) {
            apply(new ContrastFilterTransformation());
        }
        else if (filterList.getSelectedItem().toString().equals("InvertFilterTransformation")) {
            apply(new InvertFilterTransformation());
        }
        else if (filterList.getSelectedItem().toString().equals("PixelationFilterTransformation")) {
            apply(new PixelationFilterTransformation());
        }
        else if (filterList.getSelectedItem().toString().equals("SketchFilterTransformation")) {
            apply(new SketchFilterTransformation());
        }
        else if (filterList.getSelectedItem().toString().equals("SwirlFilterTransformation")) {
            apply(new SwirlFilterTransformation());
        }
        else if (filterList.getSelectedItem().toString().equals("KuwaharaFilterTransformation")) {
            apply(new KuwaharaFilterTransformation());
        }
        else if (filterList.getSelectedItem().toString().equals("VignetteFilterTransformation")) {
            apply(new VignetteFilterTransformation());
        }
    }
}

public void apply(Transformation<Bitmap> filter) {
    if (original != null) {
        Glide
                .with(this)
                .load(original)
                .apply(RequestOptions.bitmapTransform(filter))
                .into(imageView);
    }
}

Comments

Comment posted by enszrlu

I dont know but it does not work. It requires casting but casting also does not work.

Comment posted by Jayson Gonzaga

could you paste ur answer

READ  [ANSWERED] java - ListView.getItemAtPosition always return 0
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress