Advertisement
Guest User

for loulis

a guest
Mar 27th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.24 KB | None | 0 0
  1. /*main2.java*/
  2. package user.example.com.roadworks;
  3.  
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.widget.TextView;
  7.  
  8. public class Main2Activity extends AppCompatActivity {
  9.  
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main2);
  14.  
  15. Bundle bundle = getIntent().getExtras();
  16. String t = bundle.getString("value");
  17. String sta = bundle.getString("start");
  18. String sto = bundle.getString("stop");
  19. String not = bundle.getString("note");
  20.  
  21.  
  22. TextView title = (TextView) findViewById(R.id.txtTitle);
  23. TextView start = (TextView) findViewById(R.id.txtStartTime);
  24. TextView stop = (TextView) findViewById(R.id.txtStopTime);
  25. TextView note = (TextView) findViewById(R.id.txtNotes);
  26.  
  27. title.setText(t);
  28. start.setText(sta);
  29. stop.setText(sto);
  30. note.setText(not);
  31.  
  32. }
  33. }
  34. /*main2.xml*/
  35. <?xml version="1.0" encoding="utf-8"?>
  36. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  37. xmlns:tools="http://schemas.android.com/tools"
  38. android:id="@+id/activity_main2"
  39. android:layout_width="match_parent"
  40. android:layout_height="match_parent"
  41. android:paddingBottom="@dimen/activity_vertical_margin"
  42. android:paddingLeft="@dimen/activity_horizontal_margin"
  43. android:paddingRight="@dimen/activity_horizontal_margin"
  44. android:paddingTop="@dimen/activity_vertical_margin"
  45. tools:context="user.example.com.roadworks.Main2Activity">
  46.  
  47. <TextView
  48. android:text="TextView"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:id="@+id/txtTitle"
  52. android:layout_alignParentTop="true"
  53. android:layout_alignParentStart="true" />
  54.  
  55. <TextView
  56. android:text="TextView"
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:layout_below="@+id/txtTitle"
  60. android:layout_alignParentStart="true"
  61. android:layout_marginTop="16dp"
  62. android:id="@+id/txtStartTime" />
  63.  
  64. <TextView
  65. android:text="TextView"
  66. android:layout_width="wrap_content"
  67. android:layout_height="wrap_content"
  68. android:id="@+id/txtStopTime"
  69. android:layout_marginTop="19dp"
  70. android:layout_below="@+id/txtStartTime"
  71. android:layout_alignParentStart="true" />
  72.  
  73. <TextView
  74. android:text="TextView"
  75. android:layout_width="wrap_content"
  76. android:layout_height="wrap_content"
  77. android:id="@+id/txtNotes"
  78. android:layout_marginTop="12dp"
  79. android:layout_below="@+id/txtStopTime"
  80. android:layout_alignParentStart="true" />
  81. </RelativeLayout>
  82. /*main1.java*/
  83. package user.example.com.roadworks;
  84.  
  85. import java.sql.Connection;
  86. import java.sql.DriverManager;
  87. import java.sql.ResultSet;
  88. import java.sql.SQLException;
  89. import java.sql.Statement;
  90. import java.util.ArrayList;
  91. import java.util.HashMap;
  92. import java.util.List;
  93. import java.util.Map;
  94.  
  95. import android.annotation.SuppressLint;
  96. import android.content.Intent;
  97. import android.graphics.Typeface;
  98. import android.os.Bundle;
  99. import android.os.StrictMode;
  100. import android.support.v7.app.ActionBarActivity;
  101. import android.util.Log;
  102. import android.view.View;
  103. import android.widget.AdapterView;
  104. import android.widget.AdapterView.OnItemClickListener;
  105. import android.widget.Button;
  106. import android.widget.ListView;
  107. import android.widget.SimpleAdapter;
  108. import android.widget.TextView;
  109. import android.widget.Toast;
  110. import android.support.v7.app.AppCompatActivity;
  111.  
  112.  
  113. public class MainActivity extends AppCompatActivity {
  114.  
  115. TextView lblheader;
  116. Typeface font;
  117. Button btn;
  118. ListView lstroadWorks;
  119. SimpleAdapter ADAhere;
  120.  
  121. /****************************Database Connection Variables*************************************/
  122.  
  123. ConnectionClass connectionclass;
  124. String usernameS;
  125. String datets;
  126. String call, db, un, passwords;
  127. Connection connect;
  128. ResultSet rs;
  129.  
  130. @SuppressLint("NewApi")
  131. private Connection CONN(String _user, String _pass, String _DB, String _server)
  132. {
  133. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  134. StrictMode.setThreadPolicy(policy);
  135. Connection conn = null;
  136. String ConnURL = null;
  137. try{
  138. Class.forName("net.sourceforge.jtds.jdbc.Driver");
  139. ConnURL = "jdbc:jtds:sqlserver://" + _server + ";"
  140. + "databaseName=" + _DB + ";user=" + _user + ";password=" + _pass + ";";
  141. conn = DriverManager.getConnection(ConnURL);
  142. }catch (SQLException se){
  143. Log.e("ERROR", se.getMessage());
  144. }catch (ClassNotFoundException e){
  145. Log.e("ERROR", e.getMessage());
  146. }catch (Exception e){
  147. Log.e("ERROR", e.getMessage());
  148. }
  149. return conn;
  150. }
  151.  
  152. @Override
  153. protected void onCreate(Bundle savedInstanceState) {
  154. super.onCreate(savedInstanceState);
  155. setContentView(R.layout.activity_main);
  156.  
  157. lblheader = (TextView) findViewById(R.id.lblheader);
  158. lstroadWorks = (ListView) findViewById(R.id.lstRoadWorks);
  159. btn = (Button) findViewById(R.id.btnview);
  160.  
  161. /***********************DATABASE CONNECTION VARIABLE***************************************/
  162. connectionclass = new ConnectionClass();
  163. call = connectionclass.getIp();
  164. un = connectionclass.getUn();
  165. passwords = connectionclass.getPassword();
  166. db = connectionclass.getDb();
  167. connect = CONN(un, passwords, db, call);
  168. btn.setOnClickListener(new View.OnClickListener(){
  169. @Override
  170. public void onClick(View v){
  171. //TODO Auto-generated method stub
  172. String querycmd = "select * from trafficNews2";
  173. try{
  174. Statement statement = connect.createStatement();
  175. rs = statement.executeQuery(querycmd);
  176. List<Map<String, String>> data = null;
  177. data = new ArrayList<Map<String, String>>();
  178.  
  179. while (rs.next())
  180. {
  181. Map<String, String> datanum = new HashMap<String, String>();
  182. datanum.put("A", rs.getString("streetName"));
  183. datanum.put("B", rs.getString("startTime"));
  184. datanum.put("C", rs.getString("stopTime"));
  185. datanum.put("D", rs.getString("notes"));
  186.  
  187.  
  188.  
  189. data.add(datanum);
  190. }
  191. String[] fromwhere = {"A", "B", "C", "D"};
  192.  
  193.  
  194. int[] viewswhere = {R.id.lblcountryname, R.id.lblStart, R.id.lblStop};
  195.  
  196. ADAhere = new SimpleAdapter(MainActivity.this, data, R.layout.listtemplate, fromwhere, viewswhere);
  197. lstroadWorks.setAdapter(ADAhere);
  198. }catch (SQLException e){
  199. Toast.makeText(MainActivity.this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
  200. }
  201. }
  202. });
  203.  
  204. lstroadWorks.setOnItemClickListener(new OnItemClickListener() {
  205. @Override
  206. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  207. //TODO Auto-generated method stub
  208. HashMap<String, Object> obj = (HashMap<String, Object>) ADAhere.getItem(position);
  209. String VechicleId = (String) obj.get("A");
  210. String startTime = (String) obj.get("B");
  211. String stoptTime = (String) obj.get("C");
  212. String notes = (String) obj.get("D");
  213. Toast.makeText(MainActivity.this, VechicleId, Toast.LENGTH_LONG).show();
  214. Intent in = new Intent(MainActivity.this,Main2Activity.class);
  215. in.putExtra("value",VechicleId);
  216. in.putExtra("start",startTime);
  217. in.putExtra("stop",stoptTime);
  218. in.putExtra("note",notes);
  219.  
  220. startActivity(in);
  221.  
  222. }
  223. });
  224. }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement