Advertisement
Guest User

veternar

a guest
May 4th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.42 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  4. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  5. android:paddingRight="@dimen/activity_horizontal_margin"
  6. android:paddingTop="@dimen/activity_vertical_margin"
  7. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
  8.  
  9.  
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="Korisnicko ime"
  14. android:layout_centerHorizontal="true"
  15. android:textSize="20sp"
  16. android:id="@+id/text1"
  17. />
  18. <EditText
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:layout_below="@id/text1"
  22. android:id="@+id/edit1"
  23. />
  24. <TextView
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:id="@+id/text2"
  28. android:text="Lozinka"
  29. android:textSize="20sp"
  30. android:layout_below="@+id/edit1"
  31. android:layout_centerHorizontal="true"
  32. />
  33. <EditText
  34. android:layout_width="match_parent"
  35. android:layout_height="wrap_content"
  36. android:layout_below="@+id/text2"
  37. android:id="@+id/edit2"
  38. />
  39. <Button
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:layout_below="@+id/edit2"
  43. android:layout_centerHorizontal="true"
  44. android:text="Login"
  45. android:onClick="klik"/>
  46.  
  47.  
  48.  
  49.  
  50.  
  51. package com.example.veterinar;
  52.  
  53. import android.content.Intent;
  54. import android.database.Cursor;
  55. import android.database.sqlite.SQLiteDatabase;
  56. import android.support.v7.app.AppCompatActivity;
  57. import android.os.Bundle;
  58. import android.util.Log;
  59. import android.view.View;
  60. import android.widget.AdapterView;
  61. import android.widget.ArrayAdapter;
  62. import android.widget.EditText;
  63. import android.widget.ListView;
  64. import android.widget.TextView;
  65. import android.widget.Toast;
  66.  
  67. import java.util.ArrayList;
  68.  
  69. public class MainActivity extends AppCompatActivity {
  70.  
  71. ArrayList<String> listItems;
  72. ArrayAdapter<String> adapter;
  73. ListView listView;
  74. SQLiteDatabase myDB = null;
  75.  
  76. String T_ime="";
  77. String T_vlasnik="";
  78. String T_godina="";
  79. int _ID =0;
  80.  
  81. @Override
  82. protected void onCreate(Bundle savedInstanceState) {
  83. super.onCreate(savedInstanceState);
  84. setContentView(R.layout.activity_main);
  85.  
  86.  
  87. try {
  88. myDB = this.openOrCreateDatabase("korisnici", MODE_PRIVATE, null);
  89. myDB.execSQL("DROP TABLE IF EXISTS pacijenti");
  90. myDB.execSQL("CREATE TABLE IF NOT EXISTS pacijenti (id INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(20), password VARCHAR(20));");
  91. myDB.execSQL("INSERT INTO pacijenti (username,password) VALUES ('Keko','123');");
  92. } catch (Exception e) {
  93. Log.e("Error", "Error", e);
  94. } finally {
  95. if (myDB != null) myDB.close();
  96. }
  97.  
  98.  
  99. }
  100. public void dodaj(View V)
  101. {
  102. try {
  103. EditText edit1 = (EditText) findViewById(R.id.ime);
  104. EditText edit2 = (EditText) findViewById(R.id.vlasnik);
  105. EditText edit3 = (EditText) findViewById(R.id.god);
  106. myDB = this.openOrCreateDatabase("veterinar", MODE_PRIVATE, null);
  107. myDB.execSQL("INSERT INTO pacijenti (ime,vlasnik, god) VALUES ('"+edit1.getText()+"','"+edit2.getText()+"', "+Integer.parseInt(edit3.getText().toString())+");");
  108. } catch (Exception e) {
  109. Log.e("Error", "Error", e);
  110. } finally {
  111. if (myDB != null) myDB.close();
  112. }
  113. refreshList();
  114. }
  115.  
  116. public void brisi (View v){
  117. try {
  118. myDB = this.openOrCreateDatabase("veterinar", MODE_PRIVATE, null);
  119. myDB.execSQL("DELETE FROM pacijenti WHERE id="+_ID);
  120. } catch (Exception e) {
  121. Log.e("Error", "Error", e);
  122. } finally {
  123. if (myDB != null) myDB.close();
  124. }
  125. refreshList();
  126. }
  127. public void promjeni (View v){
  128. try {
  129. EditText edit1 = (EditText) findViewById(R.id.ime);
  130. EditText edit2 = (EditText) findViewById(R.id.vlasnik);
  131. EditText edit3 = (EditText) findViewById(R.id.god);
  132. myDB = this.openOrCreateDatabase("veterinar", MODE_PRIVATE, null);
  133. myDB.execSQL("UPDATE pacijenti SET ime='"+edit1.getText().toString()+
  134. "' , vlasnik='"+edit2.getText().toString()+
  135. "' , god='"+edit3.getText().toString()+
  136. "' WHERE id="+_ID);
  137.  
  138. } catch (Exception e) {
  139. Log.e("Error", "Error", e);
  140. } finally {
  141. if (myDB != null) myDB.close();
  142. }
  143. refreshList();
  144. }
  145. public void refreshList()
  146. {
  147. String podaci_iz_baze="";
  148. try {
  149. myDB = this.openOrCreateDatabase("veterinar", MODE_PRIVATE, null);
  150. Cursor c = myDB.rawQuery("SELECT * FROM pacijenti", null);
  151. listItems.clear();
  152. c.moveToFirst(); // pomak na prvi dohvaćeni zapis
  153. if (c != null) {
  154. // prolaz kroz podatke
  155. do {
  156. int id = c.getInt(c.getColumnIndex("id"));
  157. String ime = c.getString(c.getColumnIndex("ime"));
  158. String vlas = c.getString(c.getColumnIndex("vlasnik"));
  159. int god = c.getInt(c.getColumnIndex("god"));
  160. podaci_iz_baze = id+" "+ime + " " + vlas + " " + god;
  161. listItems.add(podaci_iz_baze);
  162. } while (c.moveToNext());
  163. }
  164.  
  165. adapter.notifyDataSetChanged();
  166. } catch (Exception e) {
  167. Log.e("Error", "Error", e);
  168. } finally {
  169. if (myDB != null) myDB.close();
  170. }
  171. }
  172. public void klik(View view)
  173. {
  174. EditText korisnicko = (EditText)findViewById(R.id.edit1);
  175. EditText password = (EditText)findViewById(R.id.edit2);
  176.  
  177.  
  178. String username = ""+korisnicko.getText().toString();
  179. String passw = ""+password.getText().toString();
  180.  
  181.  
  182. boolean logInSuc=false;
  183. int id= 0;
  184. try {
  185. myDB = this.openOrCreateDatabase("korisnici", MODE_PRIVATE, null);
  186. Cursor c = myDB.rawQuery("SELECT * FROM pacijenti WHERE username ='"+username+"' AND password='"+passw+"'", null);
  187. c.moveToFirst();
  188. if (c.getCount()>0) {
  189. logInSuc=true;
  190. id = c.getInt(c.getColumnIndex("id"));
  191. }
  192. }
  193. catch(Exception e) {
  194. Log.e("Error", "Error", e);
  195. } finally {
  196. if (myDB != null) myDB.close();
  197. }
  198. if(logInSuc)
  199. {
  200. Intent i = new Intent(MainActivity.this, Zivotinje.class);
  201. i.putExtra("id", id);
  202. startActivity(i);
  203. }
  204. else
  205. Toast.makeText(view.getContext(), "Krivo korisničko ime ili lozinka", Toast.LENGTH_SHORT).show();
  206. logInSuc = false;
  207. }
  208.  
  209. }
  210.  
  211.  
  212.  
  213.  
  214.  
  215. <?xml version="1.0" encoding="utf-8"?>
  216. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  217. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  218. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  219. android:paddingRight="@dimen/activity_horizontal_margin"
  220. android:paddingTop="@dimen/activity_vertical_margin"
  221. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
  222.  
  223.  
  224.  
  225. <ListView
  226. android:layout_width="match_parent"
  227. android:layout_height="170dp"
  228. android:id="@+id/lista1"
  229. android:transcriptMode="alwaysScroll">
  230. </ListView>
  231.  
  232. <EditText
  233. android:layout_below="@+id/lista1"
  234. android:layout_width="match_parent"
  235. android:layout_height="wrap_content"
  236. android:id="@+id/ime"
  237. />
  238. <EditText
  239. android:layout_width="match_parent"
  240. android:layout_height="wrap_content"
  241. android:layout_below="@+id/ime"
  242. android:id="@+id/vlasnik"
  243. />
  244. <EditText
  245. android:layout_width="match_parent"
  246. android:layout_height="wrap_content"
  247. android:layout_below="@+id/vlasnik"
  248. android:id="@+id/god"/>
  249. <Button
  250. android:layout_below="@+id/god"
  251. android:layout_width="wrap_content"
  252. android:layout_height="wrap_content"
  253. android:id="@+id/dodaj"
  254. android:text="DODAJ"
  255. android:onClick="dodaj"
  256. />
  257. <Button
  258. android:layout_toRightOf="@+id/dodaj"
  259. android:layout_width="wrap_content"
  260. android:layout_height="wrap_content"
  261. android:id="@+id/brisi"
  262. android:layout_below="@+id/god"
  263. android:text="OBRISI"
  264. android:onClick="brisi"
  265. />
  266. <Button
  267. android:layout_toRightOf="@+id/brisi"
  268. android:layout_width="wrap_content"
  269. android:layout_height="wrap_content"
  270. android:layout_below="@+id/god"
  271. android:id="@+id/premeni"
  272. android:text="promjeni"
  273. android:onClick="promjeni"
  274. />
  275. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement