java - Generics type as a parameter -


The following code was being used to update the value of some fields in that database

  Public Zero Update Field (String Registration ID) {ContentValues ​​objValues; Try {objDatabase = this.getWritableDatabase (); ObjValues ​​= new content value (); ObjValues.put (COLUMN_REGISTRATION_ID, RegistrationId); ObjDatabase.update (CUSTOMER_USERS_TABLE_NAME, objValues, null, null); ObjDatabase.close (); } Hold (exception error exemption) {log d. ("Error", "" + exception to error); }}  

Then I decided to use a general approach and therefore the code written above such as

  public & lt; T & gt; Zero update (string table name, string columnname, T value) {ContentValues ​​objValues; Try {objDatabase = this.getWritableDatabase (); ObjValues ​​= new content value (); ObjValues.put (columnName, value); /// Here is an error because 'value' objDatabase.update (tagname, objValues, null, null); ObjDatabase.close (); } Hold (exception error exemption) {log d. ("Error", "" + exception to error); }}  

is the error because ContentValues ​​ is the last and I have to store it to create my new class Can not expand the normal variable type to be optimized so that I can get rid of the error with the same code?


The second edition of the same question (if the above has been told that the thing is low to understand)

I have a last predefined class which is the form of method and value Take the parameters in.


Enter image details here


However, I want to implement a generic functionality and decide on a timely basis whether the value should be of type

You can write that dispatch for you:

  class content value adapter {Private content values; Public content value adapter (content value value) {this.values ​​= values; } Enter the public zero (string columnname, object value) {if (value of string value) {values.put (columnName, (string) value); } Else if (...) {...}} Get the value of public contentcentant value () {this.values ​​return; } / * ContentValueExplain all other methods for example * /}  

Now you can use this class instead of the original class, and keep your code clean. .


Comments