Type-Casting a java object according to Android SDK Level -


I fill out a list of programs with checkbox property objects. For a new Android SDK, there is a method set icon () Which had not been implemented earlier.

That's why I extended the checkbox launcher class and implemented the set icon () similarly. My new class is called IconCheckBoxPreference, which has problems with KitKat [New SDK].

I have to do something

  object CBP; If (android.os.build.VERSION.SDK_INT & gt; = android.os.build.VERSION_CODES.HONEYCOMB) {cbp = new checkbox reference (this); Casted_cbp = CastAccordingToClass (cbp, checkBoxPreference.class); //to do! } And {cbp = new iconcackbox presentation (this, zero); Casted_cbp = CastAccordingToClass (cbp, IconCheckBoxPreference.class); //to do! } Casted_cbp.setTitle ("My Title"); Casted_cbp.setIcon (.getResources () getDrawable (R.drawable.bla));   

Continue your code using the "codeed_cbp" variable without any further SDK statuses. What types of "Casted_cbp" can you do? Is there a way to do this? What is the best practice in such situations?

What you want is not possible.

You have to set in that icon, when the compiler still knows the exact concrete square.

For casting, use the checkbox prefix as the common superclass object type, so you can still type setTitle () independently from the platform version You can call.

This code will look like this:

  checkboxespeed cbp; If (android.os.build.VERSION.SDK_INT & gt; = android.os.build.VERSION_CODES.HONEYCOMB) {checkbox presentation pref = new checkbox presentation (this); Pref.setIcon (.getResources () getDrawable (R.drawable.bla)); Cbp = pref; } Other {IconCheckBoxPreference pref = New iconcackboxpress (this, blank); Pref.setIcon (.getResources () getDrawable (R.drawable.bla)); Cbp = pref; } Cbp.setTitle ("My Title");    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -