I am using one android project as a reference for my understanding,There i find this interface as:
public interface HttpCallback<T> {
void onSuccess(T var1);
void onHttpError(ResponseStatus var1);
}
I got reference to this interface in Main Activity which implements HttpCallback<UserDetails>.
My question is,am not able get what is T? Please suggest me what is T stands for in HttpCallback<T>.
//Main Activity Code:
public class MainActivity extends HttpCallback<UserDetails> {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
}
HttpCallback<UserDetails> userDetailCallback = new HttpCallback<UserDetails>() {
#Override
public void onSuccess(UserDetails userDetails) {
setProgressVisibility(R.id.button_find_user, View.INVISIBLE);
if(!"ok".equals(addressDetails.errorMsg)){
showErrorMessage("UserDetails Error", userDetails.errorMsg);
return;
}
}
#Override
public void onHttpError(ResponseStatus responseStatus) {
setProgressVisibility(R.id.button_find_user, View.INVISIBLE);
showErrorMessage("Internet Connection Problem", "Please check your Internet connection then try again.");
}
};
}
Thank you.
T is nothing but a Type....Its Generics..saying that it can accept any object of that Type...Read Generics in java for more details
Related
I want to write an app with about 20 different Activity. Inside each activity there is some parts that are similar in some other activities. I divided each page to smart parts and write a separate layout for them. Then in activities I use include tag for adding that part to activity.
I don't know what is the true way to implement this app.
Thanks for any suggestion.
I have made sample BaseActivity. Make abstract method you want to use and just override to your class. You can use all Base Activity public method in your class. abstract method will override to your class and simple Public method will be optional if you want to use
public abstract class BaseMainActivity extends AppCompatActivity implements View.OnClickListener{
private static ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initMethod();
setContentView(getLayout());
initUI();
initListeners();
}
public abstract void initMethod();
public abstract int getLayout();
public abstract void initUI();
public abstract void initListeners();
#Override
public void onClick(View view) {
}
public String getEditString(CustomEdittext edittext){
return edittext.getText().toString().trim();
}
public void getEditError(CustomEdittext edittext, String message){
edittext.setError(message);
}
public static void showProgress(AppCompatActivity activity){
try{
progressDialog = new ProgressDialog(activity);
progressDialog.setTitle("Please Wait");
progressDialog.show();
}catch (Exception ex){
ex.printStackTrace();
}
}
public static void hideProgress(){
try{
if(progressDialog!=null)
progressDialog.dismiss();
}catch (Exception ex){
ex.printStackTrace();
}
}
public void showToast(String message,Context context)
{
Toast.makeText(context,message,Toast.LENGTH_LONG).show();
}
}
Hi this is sample that we can extend BaseActivity to our class.
public class HomeActivity extends BaseMainActivity {
private Toolbar toolbar = null;
private Picasso picasso;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void initMethod() {
}
#Override
public int getLayout() {
return R.layout.activity_main;
}
#Override
public void initUI() {
initToolbar();
initDrawerLayout();
initNavigationView();
}
}
You can create base activity for similar methods and extend it in other activities.
And about layouts you can create similar layouts at first then include them to other layouts.
And for good structure i think use MVP.
I wrote a compound component and was adding a custom listener to react.
Inside the class for the compound component which uses an xml file.
public class VerticalCounterBlock extends LinearLayout {
public interface VerticalCounterBlockListener {
public void onCountChanged(int newCount);
}
private VerticalCounterBlockListener mVerticalCounterBlockListener = null;
public void setVerticalCounterBlockListener(VerticalCounterBlockListener listener){
mVerticalCounterBlockListener = listener;
}
// ... Other functions
}
I got my interface, I got the listener and I got the setter and I engage the listener like this in the button I have in the compound component. I can see that toast that is showing there when I test
addBtn = (Button)findViewById(R.id.btn_addcount);
addBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
count++;
counttv.setText(String.format("%1$d", count));
Toast.makeText(getContext(), "VCB", Toast.LENGTH_SHORT).show();
if(mVerticalCounterBlockListener != null) {
mVerticalCounterBlockListener.onCountChanged(count);
}
}
});
In my main activity
m20_vcb = (VerticalCounterBlock) findViewById(R.id.vcb_m20);
m20_vcb.setVerticalCounterBlockListener(new VerticalCounterBlock.VerticalCounterBlockListener() {
#Override
public void onCountChanged(int newCount) {
increasePreachCountTotal();
Toast.makeText(CounterActivity.this, String.format("%1$d", newCount), Toast.LENGTH_SHORT).show();
}
});
I do not see that toast nor does it engage the function call. What am I missing?
I can suggest you several improvement scope here mainly restructuring the current format.
Lets not keep the interface as a inner class. So here's your VerticalCounterBlockListener.java
public interface VerticalCounterBlockListener {
public void onCountChanged(int newCount);
}
Now implement this interface in your MainActivity
public class MainActivity implements VerticalCounterBlockListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
m20_vcb = (VerticalCounterBlock) findViewById(R.id.vcb_m20);
m20_vcb.setVerticalCounterBlockListener(this);
}
// ... Other methods
// Override the onCountChanged function.
#Override
public void onCountChanged(int newCount) {
increasePreachCountTotal();
Toast.makeText(CounterActivity.this, String.format("%1$d", newCount), Toast.LENGTH_SHORT).show();
}
}
You might consider removing the Toast from the addBtn click listener which might create exception.
addBtn = (Button)findViewById(R.id.btn_addcount);
addBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
count++;
counttv.setText(String.format("%1$d", count));
if(mVerticalCounterBlockListener != null) {
mVerticalCounterBlockListener.onCountChanged(count);
}
}
});
This was good there was something wrong with my system. i uninstaklled app and restarted computer and it worked as expected.
I have implemented an otto bus example. It works fine, but ONLY on the second time I visit the activity.
For example, when I load the app and hit the secret message button I am taken to the activity but the toast does not show. Then I hit the back button to return to the MainActivity and hit the show secret message button again and when I am taken to the secret message activity the toast is displayed. I realize it works the second time because I have created a leak by not unregistering the event.
Is there something I am missing about the logic?
MainActivity:
public class MainActivity extends AppCompatActivity {
Button buttonSecretMessage;
Intent intentToMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentToMessage = new Intent(MainActivity.this, SecretMessageActivity.class);
buttonSecretMessage = (Button) findViewById(R.id.buttonSecretMessage);
buttonSecretMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EventBus.getInstance().post(new MakeMySecretMessageEvent());
startActivity(intentToMessage);
}
});
}
}
Secret Message Activity:
public class SecretMessageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secret_message);
}
#Subscribe
public void getMySecretMessage(MakeMySecretMessageEvent event){
Toast.makeText(this, event.getMessage(), Toast.LENGTH_SHORT).show();
}
#Override
protected void onStart(){
super.onStart();
EventBus.getInstance().register(this);
}
#Override
protected void onStop() {
super.onStop();
//EventBus.getInstance().unregister(this);
}
}
MakeMySecretMessageEvent:
public class MakeMySecretMessageEvent {
public MakeMySecretMessageEvent() {
}
public String getMessage() {
String message = "YOU ARE AWESOME!";
return message;
}
}
EventBus:
public final class EventBus extends Bus{
private static final EventBus Bus = new EventBus();
public static Bus getInstance() {
return Bus;
}
private EventBus() {
}
}
You can send sticky event using EventBus library. It allows you to send events to component which is not created yet.
You`ll find more info here.
Here EventBus has applied in wrong scenario, when you can simply send data via intent or bundle. Which is more reliable in communication with one activity with another. You will never ever receive event on first click, as event fire is instant and your activity creation will take some time accordingly.
So try to use bundle or intent to setup communication b/w to activity one after another.
Thanks to contributors I now have a better understanding of the activity life cycle and how it fits in with event bus. That is you cannot send an event from the MainActivity to its children, but the other way around instead. Below reflects how to implement an otto event bus to pass a simple object from an activity back to the main activity. Hopefully someone else can find this useful :) And if this can be improved upon please comment. Thanks.
Main Activity:
public class MainActivity extends AppCompatActivity {
Button buttonSecretMessage;
Intent intentToMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventBus.getInstance().register(this);
intentToMessage = new Intent(MainActivity.this, SecretMessageActivity.class);
buttonSecretMessage = (Button) findViewById(R.id.buttonSecretMessage);
buttonSecretMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(intentToMessage);
}
});
}
public MakeMySecretMessageEvent event;
#Subscribe
public void getMySecretMessage(MakeMySecretMessageEvent event) {
Toast.makeText(this, event.getMessage(), Toast.LENGTH_SHORT).show();
}
protected void onStop() {
super.onStop();
if(event != null ){
EventBus.getInstance().unregister(this);
}
}
}
SecretMessageActivity (this is where the secret message is created)
public class SecretMessageActivity extends AppCompatActivity {
Button buttonClickToMeToSeeMessage;
Intent intentToMain;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secret_message);
intentToMain = new Intent(SecretMessageActivity.this, MainActivity.class);
buttonClickToMeToSeeMessage = (Button) findViewById(R.id.buttonClickToMeToSeeMessage);
buttonClickToMeToSeeMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MakeMySecretMessageEvent makeMySecretMessageEvent = new MakeMySecretMessageEvent();
EventBus.getInstance().post(makeMySecretMessageEvent);
startActivity(intentToMain);
}
});
}
}
MakeMySecretMessageEvent
public class MakeMySecretMessageEvent {
public MakeMySecretMessageEvent() {
}
public String getMessage() {
String message = "YOU ARE AWESOME!";
return message;
}
}
EventBus:
public final class EventBus extends Bus{
private static final EventBus Bus = new EventBus();
public static Bus getInstance() {
return Bus;
}
private EventBus() {
}
}
i am trying to create group chat android application and used Websocket server written in php. this server work fine on Web browser but when i try to use it in android application application disconnect as soon as it connects.
here Android code:
public class MainActivty extends Activity{
private WebSocketClient mWebSocketClient;
private ListView mMessageListView;
private ArrayAdapter<String> mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_layout);
mMessageListView=(ListView)findViewById(R.id.listView);
mAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
mMessageListView.setAdapter(mAdapter);
connectWebSocket();
}
private void connectWebSocket(){
URI uri;
try {
uri=new URI("ws://192.168.0.102:9000");
mWebSocketClient=new WebSocketClient(uri){
#Override
public void onOpen(ServerHandshake serverHandshake) {
}
#Override
public void onMessage(String s) {
}
#Override
public void onClose(int i, String s, boolean b) {
}
#Override
public void onError(Exception e) {
}
};
mWebSocketClient.connect();
} catch (URISyntaxException e) {
e.printStackTrace();
return;
}
}
private void Append_Message(String log){
mAdapter.add(log);
mAdapter.notifyDataSetChanged();
}
}
server console:
server console shows client activity
when i rewrite this code using https://github.com/pavelbucek/tyrus-client-android-test i think i could not satisfied full requirement of previous library that i used. but tutorial on this link have solved my problem.
I have problem with my App ,, and want to solve it but i could not access to solution please help me ,,,
// Main_Activity Class
public class MainActivity extends Activity {
Button Open_play_list;
AccessPlayList accessPL;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Open_play_list = (Button)findViewById(R.id.btnShA);
accessPL = new AccessPlayList();
Open_play_list.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Calling method tht contain code for open playlist
try {
accessPL.openPlaylist();
}`enter code here`
catch (Exception ex) {
Toast.makeText(getBaseContext(), ex.getMessage().toString(), Toast.LENGTH_LONG).show();
}
}
});
}
// AccessPlayList Class
public class AccessPlayList extends Activity {
Intent intentPL;
int REQUEST_CODE = 1;
int PLAYLIST_ID = 2;
public void openPlaylist()
{
intentPL = new Intent(Intent.ACTION_PICK);
intentPL.setComponent(new ComponentName("com.android.music","com.android.music.PlaylistBrowserActivity"));
intentPL.setType("MediaStore.Audio.Playlist.CONTENT_TYPE");
intentPL.setFlags(0x10000000);
intentPL.putExtra("oneShot",false);
intentPL.putExtra("PlayList",PLAYLIST_ID);
startActivityForResult(intentPL,REQUEST_CODE);
}
}
Since you did not post which line of your code is where the error is occurring, the only thing I can tell you is that you need to check if the object causing the problem is null.
Try some validation like this:
if(object != null){
//do something
}