Created
May 17, 2013 11:29
-
-
Save DominicFinn/5598510 to your computer and use it in GitHub Desktop.
Just a little example of extracting some of the ghoulishness of doing find view by ID and then casting to a specific type. I prefer this because then the type is already cast and you don't have to provide the generic type in the argument like GetViewById<T> because it's inferred.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.test1; | |
import android.app.Activity; | |
public class BaseActivity extends Activity { | |
protected <T> T GetViewById(int id) { | |
return (T)findViewById(id); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.test1; | |
import android.os.Bundle; | |
import android.app.Activity; | |
import android.view.Menu; | |
import android.widget.TextView; | |
public class MainActivity extends BaseActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
// Example of using the new tidy way of getting view | |
TextView name = GetViewById(R.id.lblImportant); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment