Advertisement
Guest User

Untitled

a guest
Aug 20th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.87 KB | None | 0 0
  1. public class BindTenGPS extends Activity {
  2. private MenuItem menuItem;
  3. DbaAdapter db;
  4. private ProgressDialog progressDialog;
  5. ListView listView ;
  6. EditText inputSearch;
  7. private BindTenDeviceBaseAdapter adapter;
  8. List<RowTenDevice> rowTenDevice;
  9. ArrayList<String> DeviceName = new ArrayList<String>();
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.list1_main);
  15. inputSearch = (EditText)findViewById(R.id.inputSearch);
  16. db=new DbaAdapter(getApplicationContext());
  17.  
  18.  
  19. new LoadTrackNumbers().execute();
  20.  
  21. }
  22.  
  23. private class LoadTrackNumbers extends AsyncTask<Void, Integer, Void> {
  24. int myProgress;
  25. @Override
  26. protected void onPreExecute()
  27. {
  28. //Create a new progress dialog
  29. progressDialog = new ProgressDialog(BindTenGPS.this);
  30. //Set the progress dialog to display a horizontal progress bar
  31. progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  32. //Set the dialog title to 'Loading...'
  33. progressDialog.setTitle("Loading...");
  34. //Set the dialog message to 'Loading application View, please wait...'
  35. progressDialog.setMessage("Loading application View, please wait...");
  36. //This dialog can't be canceled by pressing the back key
  37. progressDialog.setCancelable(true);
  38. //This dialog isn't indeterminate
  39. progressDialog.setIndeterminate(false);
  40. //The maximum number of items is 100
  41. progressDialog.setMax(100);
  42. //Set the current progress to zero
  43. progressDialog.setProgress(0);
  44. //Display the progress dialog
  45. progressDialog.show();
  46.  
  47. }
  48. @Override
  49. protected Void doInBackground(Void... params) {
  50. try
  51. {
  52. //Get the current thread's token
  53. synchronized (this)
  54. {
  55. DeviceName.clear();
  56. publishProgress(10);
  57. this.wait(500);
  58. Connection conn = null;
  59. try {
  60. Log.w("Error connection","shyam1");
  61. String driver = "net.sourceforge.jtds.jdbc.Driver";
  62. Log.w("Error connection","shyam2");
  63. Log.i("Android"," MySQL Connect Example0.");
  64. Class.forName(driver).newInstance();
  65. Log.w("Error connection","shyam3");
  66. String connString = "jdbc:jtds:sqlserver://ip address;instance=SQLEXPRESS;DatabaseName=AAUMConnect;";
  67. String username = "username";
  68. String password = "password";
  69. Log.w("Error connection","shyam4");
  70. publishProgress(20);
  71. this.wait(500);
  72. publishProgress(30);
  73. this.wait(500);
  74. publishProgress(40);
  75. this.wait(500);
  76. publishProgress(50);
  77. this.wait(500);
  78. conn = DriverManager.getConnection(connString,username,password);
  79. publishProgress(80);
  80. Log.w("Error connection","shyam5");
  81. Statement stmt = conn.createStatement();
  82. ResultSet reset;
  83. db.open();
  84. int superuser = db.GetISSuperUser();
  85. db.close();
  86. db.open();
  87. int clientid = db.GetClientID();
  88. db.close();
  89. if(superuser == 0)
  90. {
  91. db.open();
  92. String branch = db.GetBranch();
  93. db.close();
  94. reset = stmt.executeQuery("SELECT dbo.fn_AddEveryNthItem(LEFT(AssignVehicleToTrackTable, LEN(AssignVehicleToTrackTable) - 1), ',','@', 1) AS DeviceName FROM (SELECT DeviceName + ',' FROM AssignVehicleToTrack where ClientID = " + clientid + " and BranchName = '" + branch.trim() + "' and VirtualNo = 'GPS' order by DeviceName FOR XML PATH ('')) T1 (AssignVehicleToTrackTable)");
  95. }
  96. else
  97. {
  98. reset = stmt.executeQuery("SELECT dbo.fn_AddEveryNthItem(LEFT(AssignVehicleToTrackTable, LEN(AssignVehicleToTrackTable) - 1), ',','@', 1) AS DeviceName FROM (SELECT DeviceName + ',' FROM AssignVehicleToTrack where ClientID = " + clientid + " and VirtualNo = 'GPS' order by DeviceName FOR XML PATH ('')) T1 (AssignVehicleToTrackTable)");
  99. }
  100.  
  101. //Print the data to the console
  102. Log.w("Error connection","shyam6");
  103. int i1 = 0, c = 0;
  104. try
  105. {
  106. while(reset.next()){
  107. publishProgress(c);
  108. String[] tokens = new String[100];
  109. tokens = reset.getString(1).split("@");
  110. for(int i=0;i<tokens.length;i++)
  111. {
  112. DeviceName.add(tokens[i].substring(1));
  113. }
  114. i1++;
  115. c+=10;
  116. }
  117. }
  118. catch(Exception ex)
  119. {
  120. runOnUiThread(new Runnable() {
  121. @Override
  122. public void run() {
  123. Toast.makeText(getApplicationContext(), "GPS Device not available", Toast.LENGTH_LONG).show();
  124. }
  125. });
  126. }
  127.  
  128. reset.close();
  129. conn.close();
  130. rowTenDevice = new ArrayList<RowTenDevice>();
  131. for (int j = 0; j < DeviceName.size(); j++) {
  132. RowTenDevice item = new RowTenDevice(DeviceName.get(j));
  133. Log.w("Data:","D"+j+" : "+DeviceName.get(j));
  134. rowTenDevice.add(item);
  135. }
  136. runOnUiThread(new Runnable() {
  137. @Override
  138. public void run() {
  139.  
  140. listView = (ListView) findViewById(R.id.list);
  141. Log.w("Data:","E1");
  142. adapter = new BindTenDeviceBaseAdapter(BindTenGPS.this, rowTenDevice);
  143. Log.w("Data:","E2");
  144. listView.setAdapter(adapter);
  145. Log.w("Data:","E3");
  146.  
  147. inputSearch.addTextChangedListener(new TextWatcher() {
  148.  
  149.  
  150. public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
  151. // When user changed the Text
  152. BindTenGPS.this.adapter.getFilter().filter(cs);
  153. }
  154.  
  155.  
  156. public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
  157. int arg3) {
  158. // TODO Auto-generated method stub
  159.  
  160. }
  161.  
  162. @Override
  163. public void afterTextChanged(Editable arg0) {
  164. // TODO Auto-generated method stub
  165. }
  166. });
  167. listView.setOnItemClickListener(new OnItemClickListener() {
  168.  
  169. @Override
  170. public void onItemClick(
  171. AdapterView<?> arg0, View arg1,
  172. int position, long arg3) {
  173. // TODO Auto-generated method stub
  174. Intent intent = new Intent(BindTenGPS.this, ShowGPS.class);
  175. intent.putExtra("DeviceList", rowTenDevice.get(position).getDeviceName());
  176.  
  177. startActivity(intent);
  178. }
  179.  
  180. });
  181. Log.w("Data:","E4");
  182.  
  183.  
  184. }
  185. });
  186.  
  187. } catch (Exception e)
  188. {
  189. Log.w("Error connection","shyam" + e.getMessage());
  190. e.printStackTrace();
  191. }
  192. this.wait(500);
  193. publishProgress(100);
  194. }
  195. }
  196. catch (Exception e)
  197. {
  198. e.printStackTrace();
  199. }
  200. return null;
  201. }
  202. @Override
  203. protected void onProgressUpdate(Integer... values)
  204. {
  205. //set the current progress of the progress dialog
  206. progressDialog.setProgress(values[0]);
  207. }
  208. //after executing the code in the thread
  209. @Override
  210. protected void onPostExecute(Void result)
  211. {
  212. //close the progress dialog
  213. progressDialog.dismiss();
  214. //initialize the View
  215. //setContentView(R.layout.list1_main);
  216. }
  217. }
  218.  
  219. }
  220.  
  221. public class BindTenDeviceBaseAdapter extends BaseAdapter implements Filterable {
  222. Context context;
  223. List<RowTenDevice> rowTenDevice;
  224. List<RowTenDevice> arrayList; //temporary list to store filtered values.
  225. LayoutInflater inflater;
  226.  
  227. public BindTenDeviceBaseAdapter(Context context, List<RowTenDevice> items) {
  228. this.context = context;
  229. this.rowTenDevice = items;
  230. this.arrayList = items;
  231. inflater = LayoutInflater.from(context);
  232.  
  233. }
  234.  
  235. private class ViewHolder {
  236.  
  237. TextView txtTenDevice;
  238. }
  239.  
  240.  
  241. @Override
  242. public int getCount() {
  243.  
  244. return arrayList.size();
  245. }
  246.  
  247. @Override
  248. public Object getItem(int position) {
  249.  
  250. return arrayList.get(position);
  251. }
  252.  
  253. @Override
  254. public long getItemId(int position) {
  255. // TODO Auto-generated method stub
  256. return arrayList.indexOf(getItem(position));
  257.  
  258. }
  259.  
  260. @Override
  261. public View getView(int position, View convertView, ViewGroup parent) {
  262.  
  263. ViewHolder holder = null;
  264.  
  265. if (convertView == null) {
  266. convertView = inflater.inflate(R.layout.list_tendevice, null);
  267. holder = new ViewHolder();
  268. holder.txtTenDevice = (TextView) convertView.findViewById(R.id.tvTenDevice);
  269. convertView.setTag(holder);
  270. }
  271. else {
  272. holder = (ViewHolder) convertView.getTag();
  273. }
  274.  
  275. RowTenDevice rowItem = (RowTenDevice) getItem(position);
  276.  
  277. holder.txtTenDevice.setText(rowItem.getDeviceName());
  278. return convertView;
  279. }
  280.  
  281. @Override
  282. public Filter getFilter() {
  283. // TODO Auto-generated method stub
  284. Filter filter = new Filter() {
  285.  
  286. @Override
  287. protected void publishResults(CharSequence constraint, FilterResults results) {
  288. // TODO Auto-generated method stub
  289. arrayList = (List<RowTenDevice>) results.values; // has the filtered values
  290. notifyDataSetChanged();
  291. }
  292.  
  293. @Override
  294. protected FilterResults performFiltering(CharSequence constraint) {
  295. // TODO Auto-generated method stub
  296. FilterResults results = new FilterResults();
  297. List<RowTenDevice> FilteredArrList = new ArrayList<RowTenDevice>();
  298.  
  299. if (rowTenDevice == null) {
  300. rowTenDevice = new ArrayList<RowTenDevice>(arrayList); // saves the original data in rowTenDevice
  301. }
  302. if (constraint == null || constraint.length() == 0) {
  303.  
  304. // set the Original result to return
  305. results.count = rowTenDevice.size();
  306. results.values = rowTenDevice;
  307. } else {
  308. constraint = constraint.toString().toLowerCase();
  309. for (int i = 0; i < rowTenDevice.size(); i++) {
  310. RowTenDevice data = rowTenDevice.get(i);
  311. if(data.getDeviceName().toLowerCase().contains(constraint.toString())) {
  312. FilteredArrList.add(data);
  313. }
  314. }
  315. // set the Filtered result to return
  316. results.count = FilteredArrList.size();
  317. results.values = FilteredArrList;
  318. }
  319. return results;
  320. }
  321.  
  322. };
  323. return filter;
  324.  
  325. }
  326. }
  327.  
  328. public class RowTenDevice {
  329. private String devicename;
  330. public RowTenDevice(String devicename) {
  331. this.devicename = devicename;
  332. }
  333. public String getDeviceName() {
  334. return devicename;
  335. }
  336. public void setDeviceName(String devicename) {
  337. this.devicename = devicename;
  338. }
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement