Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.90 KB | None | 0 0
  1. public class EventActivity extends AppCompatActivity {
  2.  
  3. private String jsonResponseEventName;
  4. private String jsonResponseEventStartDate;
  5. private String jsonResponseEventEndDate;
  6. private String jsonResponseEventTime;
  7. private String jsonResponseEventLocation;
  8. private String jsonResponseEventOwner;
  9. private String jsonResponseEventDes;
  10. private String jsonResponseEventPic;
  11. //--------------------------------
  12. private ProgressDialog pDialog;
  13. //-------------------------
  14. private MyDatabase db;
  15. //----------------------------
  16.  
  17. private String urlJsonArray="http://kazeroon.mosbate16.com/event.php";
  18. private static String TAG = ListActivity.class.getSimpleName();
  19. static List<String> eventNameItems = new ArrayList<String>();
  20. static List<String> eventStartDateItems = new ArrayList<String>();
  21. static List<String> eventEndDateItems = new ArrayList<String >();
  22. static List<String> eventTimeItems = new ArrayList<String>();
  23. static List<String> eventLocationItems = new ArrayList<String>();
  24. static List<String> eventOwnerItems = new ArrayList<String>();
  25. static List<String> eventDesItems = new ArrayList<String>();
  26. static List<String> eventPicOItems = new ArrayList<String>();
  27.  
  28. @Override
  29. protected void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.activity_event);
  32. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  33. setSupportActionBar(toolbar);
  34.  
  35. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  36. fab.setOnClickListener(new View.OnClickListener() {
  37. @Override
  38. public void onClick(View view) {
  39. Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  40. .setAction("Action", null).show();
  41.  
  42. }
  43. });
  44. //------------
  45.  
  46. db=new MyDatabase(this);
  47. pDialog = new ProgressDialog(EventActivity.this);
  48. pDialog.setMessage("Please wait...");
  49. pDialog.setCancelable(false);
  50.  
  51.  
  52. new GetData().execute();
  53. }
  54.  
  55. @Override
  56. public boolean onCreateOptionsMenu(Menu menu) {
  57. // Inflate the menu; this adds items to the action bar if it is present.
  58. getMenuInflater().inflate(R.menu.menu_event, menu);
  59. return true;
  60. }
  61.  
  62. @Override
  63. public boolean onOptionsItemSelected(MenuItem item) {
  64. // Handle action bar item clicks here. The action bar will
  65. // automatically handle clicks on the Home/Up button, so long
  66. // as you specify a parent activity in AndroidManifest.xml.
  67. int id = item.getItemId();
  68.  
  69. //noinspection SimplifiableIfStatement
  70. if (id == R.id.action_send_event){
  71. Intent sendEventActivity =new Intent(EventActivity.this,SendEventActivity.class);
  72. startActivity(sendEventActivity);
  73. return true;
  74. }
  75. if (id == R.id.action_exit) {
  76. finish();
  77. return true;
  78. }
  79.  
  80. return super.onOptionsItemSelected(item);
  81. }
  82. public void setaddapter(){
  83. //tarif class adapter recyclview
  84. RecyclerView recyclerViewlist=(RecyclerView) findViewById(R.id.eventList);
  85. recyclerViewlist.setAdapter(new RVAdapterEvent(this, eventNameItems, eventStartDateItems, eventEndDateItems,
  86. eventTimeItems, eventLocationItems, eventOwnerItems, eventDesItems, eventPicOItems));
  87. //tarif layout maneger baraye tarif noe nemayesh
  88. recyclerViewlist.setLayoutManager(new LinearLayoutManager(this));
  89. }
  90.  
  91. private class GetData extends AsyncTask<String,Integer,String >{
  92.  
  93.  
  94. @Override
  95. protected void onPreExecute() {
  96. super.onPreExecute();
  97.  
  98. showpDialog();
  99.  
  100. }
  101.  
  102. @Override
  103. protected String doInBackground(String ... params) {
  104. JsonArrayRequest req = new JsonArrayRequest(urlJsonArray,
  105. new Response.Listener<JSONArray>() {
  106. @Override
  107. public void onResponse(JSONArray response) {
  108. Log.d(TAG, response.toString());
  109.  
  110. try {
  111. // Parsing json array response
  112. // loop through each json object
  113. eventNameItems.clear();
  114. eventStartDateItems.clear();
  115. eventEndDateItems.clear();
  116. eventTimeItems.clear();
  117. eventLocationItems.clear();
  118. eventOwnerItems.clear();
  119. eventDesItems.clear();
  120. eventPicOItems.clear();
  121. db.Querry("DELETE FROM tbl_event");
  122.  
  123. for (int i = 0; i < response.length(); i++) {
  124.  
  125. JSONObject person = (JSONObject) response.get(i);
  126.  
  127. String name = person.getString("name");
  128. String startdate = person.getString("startdate");
  129. String enddate = person.getString("enddate");
  130. String time = person.getString("time");
  131. String location = person.getString("location");
  132. String owner = person.getString("owner");
  133. String des = person.getString("des");
  134. String pic = person.getString("pic");
  135.  
  136.  
  137.  
  138. db.Querry("INSERT INTO tbl_event ( name , startdate , enddate , time , location , owner , des , pic ) " +
  139. "VALUES ('"+name+"','"+startdate+"','"+enddate+"','"+time+"','"+location+"','"+owner+"','"+des+"','"+pic+"' )");
  140.  
  141.  
  142. // Toast.makeText(ListActivity.this,name,Toast.LENGTH_SHORT).show();
  143. jsonResponseEventName = name;
  144. jsonResponseEventStartDate = startdate;
  145. jsonResponseEventEndDate = enddate;
  146. jsonResponseEventTime= time;
  147. jsonResponseEventLocation=location;
  148. jsonResponseEventOwner = owner;
  149. jsonResponseEventDes = des;
  150. jsonResponseEventPic = pic;
  151.  
  152. eventNameItems.add(jsonResponseEventName);
  153. eventStartDateItems.add(jsonResponseEventStartDate);
  154. eventEndDateItems.add(jsonResponseEventEndDate);
  155. eventTimeItems.add(jsonResponseEventTime);
  156. eventLocationItems.add(jsonResponseEventLocation);
  157. eventOwnerItems.add(jsonResponseEventOwner);
  158. eventDesItems.add(jsonResponseEventDes);
  159. eventPicOItems.add(jsonResponseEventPic);
  160.  
  161.  
  162.  
  163. }
  164.  
  165.  
  166.  
  167.  
  168. } catch (JSONException e) {
  169. e.printStackTrace();
  170. Toast.makeText(getApplicationContext(),
  171. "Error: " + e.getMessage(),
  172. Toast.LENGTH_LONG).show();
  173. Toast.makeText(getApplicationContext(),
  174. "ارتباط با سرور برقرار نشد،آخرین اطلاعات دریافتی نمایش داده می شود",
  175. Toast.LENGTH_LONG).show();
  176. //---------------------------------------------------------------------
  177.  
  178. }
  179. }
  180. }, new Response.ErrorListener() {
  181. @Override
  182. public void onErrorResponse(VolleyError error) {
  183. VolleyLog.d(TAG, "Error: " + error.getMessage());
  184. Toast.makeText(getApplicationContext(),
  185. error.getMessage(), Toast.LENGTH_SHORT).show();
  186. Toast.makeText(getApplicationContext(),
  187. "ارتباط با سرور برقرار نشد،آخرین اطلاعات دریافتی نمایش داده می شود",
  188. Toast.LENGTH_LONG).show();
  189.  
  190.  
  191. }
  192. });
  193.  
  194. // Adding request to request queue
  195. AppController.getInstance().addToRequestQueue(req);
  196. return null;
  197.  
  198.  
  199. }
  200.  
  201. @Override
  202. protected void onPostExecute(String s) {
  203. super.onPostExecute(s);
  204.  
  205. hidepDialog();
  206.  
  207. runOnUiThread(new Runnable() {
  208. @Override
  209. public void run() {
  210. setaddapter();
  211. }
  212. });
  213.  
  214. }
  215. }
  216. private void showpDialog() {
  217. if (!pDialog.isShowing())
  218. pDialog.show();
  219. }
  220.  
  221. private void hidepDialog() {
  222. if (pDialog.isShowing())
  223. pDialog.dismiss();
  224. }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement