Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. package com.example.laboratorinis;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.content.Intent;
  6. import android.os.AsyncTask;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.widget.AdapterView;
  10. import android.widget.ListView;
  11. import android.widget.SimpleAdapter;
  12. import android.view.View;
  13. import android.view.View.OnClickListener;
  14. import android.widget.Button;
  15. import java.lang.*;
  16.  
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import java.io.*;
  21. import android.widget.EditText;
  22. import android.widget.TextView;
  23.  
  24.  
  25.  
  26.  
  27. public class MainActivity extends Activity implements AdapterView.OnItemClickListener {
  28. private static final String TAG = "MainActivity";
  29. ArrayList<HashMap<String, String>> UzrasaiDataList;
  30.  
  31. private EditText trinamas;
  32. Button RequestButton; // button which on clicking, sends the request
  33. TextView DisplayText; // a text field to display the request response
  34. TextView DataField; // a text field where the data to be sent is entered
  35.  
  36. Button ASCButton; // button which on clicking, sends the request
  37. Button DESCButton; // button which on clicking, sends the request
  38.  
  39.  
  40. @Override
  41. protected void onCreate(Bundle savedInstanceState) {
  42. super.onCreate(savedInstanceState);
  43. setContentView(R.layout.activity_main);
  44. System.setProperty("http.keepAlive","false");
  45. gautiUzrasus();
  46. initcontrols();
  47.  
  48. rikiavimas();
  49. }
  50.  
  51. private void initcontrols() {
  52. RequestButton = (Button) findViewById(R.id.trinti);
  53. DataField = (TextView) findViewById(R.id.kat_trin);
  54.  
  55. RequestButton.setOnClickListener(new OnClickListener() {
  56. public void onClick(View v) {
  57. String name = DataField.getText().toString();
  58. try {
  59. new trintiUzrasusTask().execute(name, null, null);
  60. } catch (Exception ex) {
  61. Log.e(TAG, ex.toString());
  62. }
  63.  
  64. RequestButton.setVisibility(View.VISIBLE);
  65. }
  66. });
  67. }
  68.  
  69. private void rikiavimas() {
  70. ASCButton = (Button) findViewById(R.id.ascending);
  71.  
  72. ASCButton.setOnClickListener(new OnClickListener() {
  73. public void onClick(View v) {
  74. try {
  75. new gautiUzrasusTask().execute("ASC", null, null);
  76. } catch (Exception ex) {
  77. Log.e(TAG, ex.toString());
  78. }
  79.  
  80. ASCButton.setVisibility(View.VISIBLE);
  81. }
  82. });
  83.  
  84. DESCButton = (Button) findViewById(R.id.descending);
  85.  
  86. DESCButton.setOnClickListener(new OnClickListener() {
  87. public void onClick(View v) {
  88. try {
  89. new gautiUzrasusTask().execute("DESC", null, null);
  90. } catch (Exception ex) {
  91. Log.e(TAG, ex.toString());
  92. }
  93.  
  94. DESCButton.setVisibility(View.VISIBLE);
  95. }
  96. });
  97. }
  98. private void gautiUzrasus() {
  99. new gautiUzrasusTask().execute("ASC", null, null);
  100. }
  101.  
  102. private void rodytiUzrasus(List<Uzrasas> uzrasai) {
  103. UzrasaiDataList = new ArrayList<HashMap<String, String>>();
  104.  
  105. for (int i = 0; i < uzrasai.size(); i++) {
  106. Uzrasas u = uzrasai.get(i);
  107.  
  108. HashMap<String, String> UzrasasDataMap = new HashMap<String, String>();
  109.  
  110. UzrasasDataMap.put("id", String.valueOf(u.ID));
  111. UzrasasDataMap.put("pavadinimas", u.Pavadinimas);
  112. UzrasasDataMap.put("kategorija", u.Kategorija);
  113.  
  114. UzrasaiDataList.add(UzrasasDataMap);
  115. }
  116.  
  117. ListView mlv = (ListView) findViewById(R.id.uzrasaiListView);
  118.  
  119. SimpleAdapter SimpleMiestaiAdapter = new SimpleAdapter(this, UzrasaiDataList, R.layout.uzrasai_list_row,
  120. new String[]{"pavadinimas", "kategorija"},
  121. new int[]{R.id.pavadinimasTextView, R.id.kategorijaTextView});
  122.  
  123. mlv.setAdapter(SimpleMiestaiAdapter);
  124. mlv.setOnItemClickListener(this);
  125. }
  126.  
  127. @Override
  128. public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
  129.  
  130. }
  131.  
  132. private class gautiUzrasusTask extends AsyncTask<String, Void, List<Uzrasas>> {
  133. ProgressDialog actionProgressDialog = new ProgressDialog(MainActivity.this);
  134.  
  135. @Override
  136. protected void onPreExecute() {
  137. actionProgressDialog.setMessage("Gaunami užrašai...");
  138. actionProgressDialog.show();
  139. actionProgressDialog.setCancelable(true);
  140. super.onPreExecute();
  141. }
  142.  
  143. protected List<Uzrasas> doInBackground(String... str_param) {
  144. String RestURL = str_param[0];
  145. List<Uzrasas> uzrasai = new ArrayList<Uzrasas>();
  146. try {
  147. uzrasai = DataAPI.gautiUzrasus(RestURL);
  148. } catch (Exception ex) {
  149. Log.e(TAG, ex.toString());
  150. }
  151.  
  152. return uzrasai;
  153.  
  154.  
  155. }
  156.  
  157. protected void onProgressUpdate(Void... progress) {
  158. }
  159.  
  160. protected void onPostExecute(List<Uzrasas> result) {
  161. actionProgressDialog.cancel();
  162. rodytiUzrasus(result);
  163.  
  164. }
  165.  
  166. }
  167. private class trintiUzrasusTask extends AsyncTask<String, Void, List<Uzrasas>> {
  168. ProgressDialog actionProgressDialog = new ProgressDialog(MainActivity.this);
  169.  
  170. @Override
  171. protected void onPreExecute() {
  172. actionProgressDialog.setMessage("Trinami užrašai...");
  173. actionProgressDialog.show();
  174. actionProgressDialog.setCancelable(true);
  175. super.onPreExecute();
  176. }
  177.  
  178. protected List<Uzrasas> doInBackground(String... str_param) {
  179. String RestURL = str_param[0];
  180. List<Uzrasas> uzrasai = new ArrayList<Uzrasas>();
  181. try {
  182. WebAPI.sendGET(RestURL);
  183. uzrasai = DataAPI.gautiUzrasus("http://10.0.2.2:3000/android");
  184. } catch (Exception ex) {
  185. Log.e(TAG, ex.toString());
  186. }
  187. return uzrasai;
  188. }
  189.  
  190. protected void onProgressUpdate(Void... progress) {
  191. }
  192.  
  193. protected void onPostExecute(List<Uzrasas> result) {
  194. actionProgressDialog.cancel();
  195. rodytiUzrasus(result);
  196.  
  197. }
  198.  
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement