The package The trap.Simplified coding.androidmysqlsync;
Import Android.Clearly;
Import Android.App.Progressressialog;
Import Android.Content.broadcast receiver;
Import Android.Content.Context;
Import Android.Content.Intent;
Import Android.Content.Intent filter;
Import Android.Content.in the evening.package manager;
Import Android.The database.Cursor;
Import Android.The trap.Valency;
Import Android.Support.v4.Content.Context.com;
Import Android.Support.v7.App.AppCompatActivity;
Import Android.The.Bundle;
Import Android.Opinion.Opinion;
Import Android.Widget.switch;
Import Android.Widget.Change in writing;
Import Android.Widget.list View;
Import Android.Widget.Toast;
Import With.Android.Volley.Authorfailure;
Import With.Android.Volley.Request;
Import With.Android.Volley.request;
Import With.Android.Volley.Feedback;
Import With.Android.Volley.VolleyError;
Import With.Android.Volley.Toolbox.Stringering;
Import With.Android.Volley.Toolbox.Volley;
Import org.Jasan.JSONException;
Import org.Jasan.JSONObject;
Import Java.Useful.array list;
Import Java.Useful.hash map;
Import Java.Useful.The list;
Import Java.Useful.Map;
public class Main activity Is spread AppCompatActivity come into force Opinion.OnClickListener {{
/ *
* This is the URL for our webservice
* Make sure you are using IP instead of localhost
* If you are using localhost this will not work
* * /
public Fixed The last String URL_SAVE_NAME = = “http://192.168.1.107/SqliteSync/saveName.php”;
// database helper object
Private Database helper db;
// view items
Private switch buttonSave;
Private Change in writing editTextName;
Private list View List viewname;
// List to store all names
Private The list Name;
// 1 means data is synced and 0 means data is not synced
public Fixed The last int NAME_SYNCED_WITH_SERVER = Have 1;
public Fixed The last int NAME_NOT_SYNCED_WITH_SERVER = 0;
// Whether a broadcast data is synced to know the weather
public Fixed The last String DATA_SAVED_BROADCAST = = “net.simplifiedcoding.datasaved”;
// Broadcaster receiver to know sync status
Private broadcast receiver broadcast receiver;
// Adoptobiz for list view
Private NameAdapter nameAdapter;
@Override
Protected Zero Go ahead()Bundle saveInstanceState) {{
Super.Go ahead()saveInstanceState);
setContentView()R.Layout.activity_main);
// initialize ideas and objects
db = = New Database helper()this);
Name = New array list());
buttonSave = ()switch) findViewById()R.Id.buttonSave);
editTextName = = ()Change in writing) findViewById()R.Id.editTextName);
List viewname = = ()list View) findViewById()R.Id.List viewname);
// add the click listener to the button
buttonSave.setOnClickListener()this);
// Calling the method to load all stored names
Load name());
// broadcast receiver to update sync status
broadcast receiver = = New broadcast receiver()) {{
@Override
public Zero onReceive()Context The reference, Intent is intended) {{
// reload names
Load name());
}
};
// registering the broadcast receiver to update the sync status
Registrar()broadcast receiver, New Intent filter()DATA_SAVED_BROADCAST));
}
/ *
* This method will be
* Load name from database
* With update sync status
* * /
Private Zero Load name()) {{
Name.Clear());
Cursor Cursor = = db.getNames());
If ()Cursor.MoveToFirst())) {{
do {{
Name Name = = New Name()
Cursor.Get up()Cursor.getColumnIndex()Database helper.column name)),
Cursor.Tinted()Cursor.getColumnIndex()Database helper.COLUMN_STATUS))
);
Name.Add up()Name);
} whereas ()Cursor.MoveToNext()));
}
nameAdapter = = New NameAdapter()this, R.Layout.Name, Name);
List viewname.setAdapter()nameAdapter);
}
/ *
* This method will simply refresh the list
* * /
Private Zero refresh list()) {{
nameAdapter.Information());
}
/ *
* Server is being named by this method
* * /
Private Zero saveNameToServer()) {{
The last Progressressialog PragatiDialogue = = New Progressressialog()this);
PragatiDialogue.Setmessage()“Saving Name …”);
PragatiDialogue.Display());
The last String Name = = editTextName.Gettext()).Wire()).Trim());
Stringering Rigid = = New Stringering()Request.way.Post, URL_SAVE_NAME,
New Feedback.Listner()) {{
@Override
public Zero Treason()String Feedback) {{
PragatiDialogue.reject());
try {{
JSONObject obj = = New JSONObject()Feedback);
If ()!obj.GetBoolean()“Error”)) {{
// if there is any success
// storing the name in sqlite to sync the position
saveNameToLocalStorage()Name, NAME_SYNCED_WITH_SERVER);
} other {{
// if there is some error
// Name the cyclite with the status as uncommented
saveNameToLocalStorage()Name, NAME_NOT_SYNCED_WITH_SERVER);
}
} Hold ()JSONException I) {{
I.Printstacktress());
}
}
},
New Feedback.Error maker()) {{
@Override
public Zero onErrorResponse()VolleyError Error) {{
PragatiDialogue.reject());
// Storing the name in storelight to make the condition error-free
saveNameToLocalStorage()Name, NAME_NOT_SYNCED_WITH_SERVER);
}
}) {{
@Override
Protected Map<String, String> getParams()) Throws up Authorfailure {{
Map<String, String> Para = = New hash map());
The ultimate.Branch()“Name”, Name);
The return Para;
}
};
Volleysington.get Instance()this).addToRequestQueue()Rigid);
}
// name saving for local storage
Private Zero saveNameToLocalStorage()String Name, int Event) {{
editTextName.Settext()“”);
db.addName()Name, Event);
Name N = = New Name()Name, Event);
Name.Add up()N);
refresh list());
}
@Override
public Zero on click()Opinion Opinion) {{
saveNameToServer());
}
}
Source link