Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. public class SettingActivity extends Activity implements OnClickListener {
  2. Button btn;
  3. EditText etIPAddress,etDBname,etUserName,etPassword;
  4.  
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. // TODO Auto-generated method stub
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_ayarlar);
  10.  
  11.  
  12. Controls();
  13.  
  14. SetButtonText();
  15.  
  16. }
  17.  
  18. private void Controls() {
  19. btn = (Button) findViewById(R.id.btn);
  20. etIPAddress = (EditText) findViewById(R.id.etIPAddress);
  21. etDBname = (EditText) findViewById(R.id.etDBname);
  22. etUserName = (EditText) findViewById(R.id.etUserName);
  23. etPassword = (EditText) findViewById(R.id.etPassword);
  24.  
  25. btn.setOnClickListener(this);
  26. }
  27.  
  28. private void SetButtonText() {
  29. DB db = new DB(this);
  30. db.open();
  31. Cursor c = db.Query();
  32. int count = c.getCount();
  33.  
  34. if (count == 0) {
  35. btn.setText("SAVE");
  36. } else if (count == 1) {
  37. btn.setText("UPDATE");
  38. SetResults(c);
  39. }
  40. db.close();
  41. }
  42.  
  43. private void SetResults(Cursor c) {
  44. while (c.moveToNext()) {
  45. String ipAddress = c.getString(c.getColumnIndex("IpAddress"));
  46. String dbName = c.getString(c
  47. .getColumnIndex("DBname"));
  48. String userName = c.getString(c.getColumnIndex("UserName"));
  49. String password = c.getString(c.getColumnIndex("Password"));
  50.  
  51. etIPAddress.setText(ipAddress);
  52. etVeriTabaniAdi.setText(dbName);
  53. etKullaniciAdi.setText(userName);
  54. etSifre.setText(password);
  55. }
  56. }
  57.  
  58. @Override
  59. public void onClick(View v) {
  60. String ipAddress = etIPAdresi.getText().toString();
  61. String dbName = etVeriTabaniAdi.getText().toString();
  62. String userNmae = etKullaniciAdi.getText().toString();
  63. String password = etSifre.getText().toString();
  64.  
  65. String btnText = btn.getText().toString();
  66.  
  67. DB db = new DB(this);
  68. db.open();
  69.  
  70. boolean insertResult = false, updateResult = false;
  71.  
  72. if (btnText.equals("SAVE")) {
  73. insertResult = db.Insert(ipAdress,dbName, userName,
  74. password);
  75. } else if (btnText.equals("UPDATE")) {
  76. updateResult = db.Update(ipAddress, dbName, userName,
  77. password);
  78. }
  79.  
  80. if (insertResult) {
  81. Toast.makeText(this, "Data is saved.", Toast.LENGTH_SHORT).show();
  82. } else if (updateResult) {
  83. Toast.makeText(this, "Data is updated.", Toast.LENGTH_SHORT)
  84. .show();
  85. }
  86.  
  87. db.close();
  88. } }
  89.  
  90. public class DB {
  91. private static final String DB_NAME = "Conn";
  92. private static final int DB_VERSION = 1;
  93. private static final String DB_CREATE = "CREATE TABLE Info ("
  94. + "ID INTEGER PRIMARY KEY AUTOINCREMENT, "
  95. + "IpAddress TEXT, DBname TEXT, "
  96. + "UserName TEXT, Passsword TEXT);";
  97.  
  98. private static final String DB_DROP = "DROP TABLE IF EXISTS Info";
  99.  
  100. private DbHelper helper;
  101. private Context context;
  102. private SQLiteDatabase database;
  103.  
  104.  
  105.  
  106. private static class DbHelper extends SQLiteOpenHelper {
  107. public DbHelper(Context context) {
  108. super(context, DB_NAME, null, DB_VERSION);
  109. }
  110.  
  111. @Override
  112. public void onCreate(SQLiteDatabase db) {
  113. db.execSQL(DB_CREATE);
  114. }
  115.  
  116. @Override
  117. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  118. db.execSQL(DB_DROP);
  119. onCreate(db);
  120. }
  121. }
  122.  
  123. public DB(Context context) {
  124. this.context = context;
  125. }
  126.  
  127. public DB open() {
  128. helper = new DbHelper(context);
  129. database = helper.getWritableDatabase();
  130. return this;
  131. }
  132.  
  133. public void close() {
  134. helper.close();
  135. }
  136.  
  137. public boolean Insert(String ipAddress, String dbName,
  138. String userName, String password) {
  139. try {
  140. ContentValues cv = new ContentValues();
  141. cv.put("IpAddress", ipAddress);
  142. cv.put("DBname", dbName);
  143. cv.put("UserNmae", userName);
  144. cv.put("password", password);
  145. database.insert("Info", null, cv);
  146. return true;
  147. } catch (Exception ex) {
  148. return false;
  149. }
  150. }
  151.  
  152. public boolean Update(String ipAddress, String dbName,
  153. String userName, String password) {
  154. try {
  155. ContentValues cv = new ContentValues();
  156. cv.put("IpAddress", ipAddress);
  157. cv.put("DBname", dbName);
  158. cv.put("UserName", userName);
  159. cv.put("Password", password);
  160. database.update("Info", cv, "ID=?", new String[] { "1" });
  161. return true;
  162. } catch (Exception ex) {
  163. return false;
  164. }
  165. }
  166.  
  167. public Cursor Query() {
  168. Cursor c = database.query("Info", new String[] { "ID", "IpAddress",
  169. "DBname", "UserName", "Password" }, null, null, null,null, null);
  170. return c;
  171. }
  172.  
  173. public void Delete() {
  174. database.delete("Info", null, null);
  175. } }
  176.  
  177. public class FragmentEmployee extends Fragment {
  178. @Override
  179. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  180. Bundle savedInstanceState) {
  181. // TODO Auto-generated method stub
  182.  
  183. View view = inflater.inflate(R.layout.fragment_employee, container, false);
  184. return view;
  185. }
  186.  
  187. public class MainActivity extends AppCompatActivity {
  188. Toolbar toolbar;
  189.  
  190. @Override
  191. protected void onCreate(Bundle savedInstanceState) {
  192. super.onCreate(savedInstanceState);
  193. setContentView(R.layout.activity_main);
  194. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  195. setSupportActionBar(toolbar);
  196.  
  197. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  198.  
  199.  
  200. TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
  201. tabLayout.addTab(tabLayout.newTab().setText("Employee"));
  202. tabLayout.addTab(tabLayout.newTab().setText("About Company"));
  203. tabLayout.addTab(tabLayout.newTab().setText("Products"));
  204. tabLayout.addTab(tabLayout.newTab().setText("Clients"));
  205. tabLayout.addTab(tabLayout.newTab().setText("Managers"));
  206. tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
  207.  
  208. final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
  209. final PagerAdapter adapter = new PagerAdapter
  210. (getSupportFragmentManager(), tabLayout.getTabCount());
  211. viewPager.setAdapter(adapter);
  212. viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
  213. tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
  214. @Override
  215. public void onTabSelected(TabLayout.Tab tab) {
  216. viewPager.setCurrentItem(tab.getPosition());
  217. }
  218.  
  219. @Override
  220. public void onTabUnselected(TabLayout.Tab tab) {
  221.  
  222. }
  223.  
  224. @Override
  225. public void onTabReselected(TabLayout.Tab tab) {
  226.  
  227. }
  228. });
  229. }
  230. @Override
  231. public boolean onCreateOptionsMenu(Menu menu) {
  232. // Inflate the menu; this adds items to the action bar if it is present.
  233. getMenuInflater().inflate(R.menu.menu_main, menu);
  234. return true;
  235. }
  236.  
  237. @Override
  238. public boolean onOptionsItemSelected(MenuItem item) {
  239.  
  240. int id = item.getItemId();
  241.  
  242. //noinspection SimplifiableIfStatement
  243. if (id == R.id.action_settings) {
  244. Intent intent=new Intent(this,SettingsActivity.class);
  245. startActivity(intent);
  246. return true;
  247. }
  248.  
  249. return super.onOptionsItemSelected(item);
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement