Advertisement
Guest User

mainactivity again

a guest
Jan 25th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.60 KB | None | 0 0
  1. package com.example.mysqlexampleproject;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.res.Resources;
  8. import android.os.AsyncTask;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.widget.Adapter;
  12. import android.widget.ArrayAdapter;
  13. import android.widget.Button;
  14. import android.widget.ListView;
  15. import android.widget.TextView;
  16.  
  17. import org.json.JSONArray;
  18.  
  19. import java.sql.Array;
  20. import java.sql.Connection;
  21. import java.sql.DriverManager;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.sql.Statement;
  25. import java.util.ArrayList;
  26. import java.util.HashMap;
  27. import java.util.LinkedHashMap;
  28. import java.util.Map;
  29.  
  30. public class MainActivity extends AppCompatActivity {
  31.  
  32. //googleBtn.setOnClickListener(new View.OnClickListener() {
  33. //
  34. // @Override
  35. // public void onClick(View v) {
  36. // //String google = "https://www.google.com";
  37. // //Uri webaddress = Uri.parse(google);
  38.  
  39. //Intent gotoGoogle = new Intent(Intent.ACTION_VIEW, webaddress);
  40. //if (gotoGoogle.resolveActivity(getPackageManager()) != null) {
  41. // startActivity(gotoGoogle);
  42. //}
  43. // }
  44. // });
  45.  
  46. //}
  47.  
  48. ArrayAdapter ArrayAdapter;
  49. // Construct the data source
  50. ArrayList<user> arrayOfUsers = new ArrayList<user>();
  51. // Create the adapter to convert the array to views
  52. //UsersAdapter adapter = new UsersAdapter(this, arrayOfUsers);
  53. UsersAdapter adapter;
  54. // Attach the adapter to a ListView
  55. ListView listView = (ListView) findViewById(R.id.myListView);
  56. //listView.setAdapter(ArrayAdapter);
  57. Context thisContext;
  58. ListView myListView;
  59. TextView progressTextView;
  60. Integer resource;
  61. //Map<Integer, String> usersMap = new LinkedHashMap<Integer, String>(); //A Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value.
  62. //String[][] userList;
  63.  
  64. @Override
  65. protected void onCreate(Bundle savedInstanceState) {
  66. super.onCreate(savedInstanceState);
  67. setContentView(R.layout.activity_main);
  68.  
  69. //Attempts to launch an activity within our own app
  70. //Button secondActivityBtn = (Button)findViewById(R.id.loginBtn);
  71. //secondActivityBtn.setOnClickListener(new View.OnClickListener() {
  72.  
  73. // @Override
  74. // public void onClick(View v_) {
  75. // Intent startIntent = new Intent(getApplicationContext(), loginScreen.class);
  76. // //show how to pass info to another activity
  77. // startIntent.putExtra("com.example.mysqlexampleproject.NAME", "TEST");
  78. // startActivity(startIntent);
  79. // }
  80. //});
  81.  
  82. //Attempt to launch an activity outside our app
  83. //Button googleBtn = (Button)findViewById(R.id.signUpBtn);
  84.  
  85. adapter = new UsersAdapter(this, arrayOfUsers);
  86. Resources res = getResources();
  87. myListView = (ListView) findViewById(R.id.myListView);
  88. progressTextView = (TextView) findViewById(R.id.progressTextView);
  89. thisContext = this;
  90. listView.setAdapter(adapter);
  91.  
  92. progressTextView.setText("");
  93. Button btn = (Button) findViewById(R.id.getDataButton);
  94. btn.setOnClickListener(new View.OnClickListener() {
  95. @Override
  96. public void onClick(View v) {
  97. GetData retrieveData = new GetData();
  98. retrieveData.execute("");
  99. }
  100. });
  101. }
  102.  
  103. private class GetData extends AsyncTask<String,String,String> {
  104.  
  105. Integer flag = 0;
  106. //Integer user = 0;
  107. String msg = "";
  108. //JDBC driver name and database URL
  109. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  110. // Example: 10.20.30.40:3306
  111. static final String DB_URL = "jdbc:mysql://" + DBStrings.DATABASE_URL + "/" + DBStrings.DATABASE_NAME;
  112.  
  113. @Override
  114. protected void onPreExecute() {
  115. progressTextView.setText("Connecting to database...");
  116. }
  117.  
  118. @Override
  119. protected String doInBackground(String... params) {
  120.  
  121. Connection conn = null;
  122. Statement stmt = null;
  123.  
  124. try {
  125.  
  126. flag = 1;
  127. Class.forName(JDBC_DRIVER);
  128. conn = DriverManager.getConnection(DB_URL, DBStrings.USERNAME, DBStrings.PASSWORD);
  129. //conn = DriverManager.getConnection(DB_URL);
  130.  
  131. flag = 2;
  132. stmt = conn.createStatement();
  133. String sql = "SELECT * FROM users";
  134. ResultSet rs = stmt.executeQuery(sql);
  135.  
  136. //lets hope this works
  137. ArrayList<user> users = new ArrayList<>();
  138.  
  139. /*
  140. // Add item to adapter
  141. User newUser = new User("Nathan", "San Diego");
  142. adapter.add(newUser);
  143. // Or even append an entire new collection
  144. // Fetching some data, data has now returned
  145. // If data was JSON, convert to ArrayList of User objects.
  146. JSONArray jsonArray = ...;
  147. ArrayList<User> newUsers = User.fromJson(jsonArray)
  148. adapter.addAll(newUsers);
  149. */
  150.  
  151.  
  152. flag = 3;
  153. while (rs.next()) {
  154. Integer userID = rs.getInt("userID");
  155. String firstName = rs.getString("firstName");
  156. String surname = rs.getString("surname");
  157. String username = rs.getString("username");
  158. user newUser = new user(username, userID);
  159. newUser.user_id = userID;
  160. newUser.username = username;
  161. //UsersAdapter.add(newUser);
  162. adapter.add(newUser);
  163. //Double price = rs.getDouble("price");
  164. //userList[user][0] = firstName;
  165. //userList[user][1] = surname;
  166. //userList[user][2] = username;
  167. //usersMap.put(userID, firstName);
  168. //usersMap.put(name, price);
  169. //user += 1;
  170. flag = 4;
  171. }
  172.  
  173. msg = "Process complete.";
  174.  
  175. rs.close();
  176. stmt.close();
  177. conn.close();
  178.  
  179.  
  180. //milbs recommends you create static array list of users in user class (or new class)
  181. //ArrayList<user> users = new ArrayList<>();
  182. //user newUser = new user();
  183. //newUser.user_id = 5;
  184. //newUser.username = "TJM4";
  185. //users.add(newUser);
  186.  
  187. //users.contains(newUser);
  188.  
  189.  
  190. //milbs recommends you dont use these
  191. //HashMap<String, user> newHashmap = new HashMap<>();
  192. //newHashmap.put("new user string", newUser);
  193.  
  194. //user userfromhashmap = newHashmap.get("new user string");
  195.  
  196.  
  197.  
  198. } catch (SQLException connError) {
  199. msg = "An exception was thrown for JDBC.\nFlag: " + flag.toString();
  200. connError.printStackTrace();
  201. } catch (ClassNotFoundException e) {
  202. msg = "A class not found exception was thrown.";
  203. e.printStackTrace();
  204. } finally {
  205.  
  206. try {
  207. if (stmt != null) {
  208. stmt.close();
  209. }
  210. } catch (SQLException e) {
  211. e.printStackTrace();
  212. }
  213.  
  214. try {
  215. if (conn != null) {
  216. conn.close();
  217. }
  218. } catch (SQLException e) {
  219. e.printStackTrace();
  220. }
  221.  
  222. }
  223.  
  224. return null;
  225. }
  226.  
  227. @Override
  228. protected void onPostExecute(String msg) {
  229.  
  230. progressTextView.setText(this.msg);
  231.  
  232. //if (userList.length() > 0) {
  233. // itemAdapter = new ItemAdapter(thisContext, userList);
  234. //myListView.setAdapter(itemAdapter);
  235. //}
  236.  
  237. //if (usersMap.size() > 0) {
  238. if (arrayOfUsers.size() > 0) {
  239.  
  240. //itemAdapter = new ItemAdapter(thisContext, usersMap);
  241. ArrayAdapter = new ArrayAdapter(thisContext, resource);
  242. myListView.setAdapter(ArrayAdapter);
  243. }
  244. }
  245. }
  246.  
  247. } //END OF MAINACTIVITY
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement