Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SettingActivity extends Activity implements OnClickListener {
- Button btn;
- EditText etIPAddress,etDBname,etUserName,etPassword;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_ayarlar);
- Controls();
- SetButtonText();
- }
- private void Controls() {
- btn = (Button) findViewById(R.id.btn);
- etIPAddress = (EditText) findViewById(R.id.etIPAddress);
- etDBname = (EditText) findViewById(R.id.etDBname);
- etUserName = (EditText) findViewById(R.id.etUserName);
- etPassword = (EditText) findViewById(R.id.etPassword);
- btn.setOnClickListener(this);
- }
- private void SetButtonText() {
- DB db = new DB(this);
- db.open();
- Cursor c = db.Query();
- int count = c.getCount();
- if (count == 0) {
- btn.setText("SAVE");
- } else if (count == 1) {
- btn.setText("UPDATE");
- SetResults(c);
- }
- db.close();
- }
- private void SetResults(Cursor c) {
- while (c.moveToNext()) {
- String ipAddress = c.getString(c.getColumnIndex("IpAddress"));
- String dbName = c.getString(c
- .getColumnIndex("DBname"));
- String userName = c.getString(c.getColumnIndex("UserName"));
- String password = c.getString(c.getColumnIndex("Password"));
- etIPAddress.setText(ipAddress);
- etVeriTabaniAdi.setText(dbName);
- etKullaniciAdi.setText(userName);
- etSifre.setText(password);
- }
- }
- @Override
- public void onClick(View v) {
- String ipAddress = etIPAdresi.getText().toString();
- String dbName = etVeriTabaniAdi.getText().toString();
- String userNmae = etKullaniciAdi.getText().toString();
- String password = etSifre.getText().toString();
- String btnText = btn.getText().toString();
- DB db = new DB(this);
- db.open();
- boolean insertResult = false, updateResult = false;
- if (btnText.equals("SAVE")) {
- insertResult = db.Insert(ipAdress,dbName, userName,
- password);
- } else if (btnText.equals("UPDATE")) {
- updateResult = db.Update(ipAddress, dbName, userName,
- password);
- }
- if (insertResult) {
- Toast.makeText(this, "Data is saved.", Toast.LENGTH_SHORT).show();
- } else if (updateResult) {
- Toast.makeText(this, "Data is updated.", Toast.LENGTH_SHORT)
- .show();
- }
- db.close();
- } }
- public class DB {
- private static final String DB_NAME = "Conn";
- private static final int DB_VERSION = 1;
- private static final String DB_CREATE = "CREATE TABLE Info ("
- + "ID INTEGER PRIMARY KEY AUTOINCREMENT, "
- + "IpAddress TEXT, DBname TEXT, "
- + "UserName TEXT, Passsword TEXT);";
- private static final String DB_DROP = "DROP TABLE IF EXISTS Info";
- private DbHelper helper;
- private Context context;
- private SQLiteDatabase database;
- private static class DbHelper extends SQLiteOpenHelper {
- public DbHelper(Context context) {
- super(context, DB_NAME, null, DB_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(DB_CREATE);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL(DB_DROP);
- onCreate(db);
- }
- }
- public DB(Context context) {
- this.context = context;
- }
- public DB open() {
- helper = new DbHelper(context);
- database = helper.getWritableDatabase();
- return this;
- }
- public void close() {
- helper.close();
- }
- public boolean Insert(String ipAddress, String dbName,
- String userName, String password) {
- try {
- ContentValues cv = new ContentValues();
- cv.put("IpAddress", ipAddress);
- cv.put("DBname", dbName);
- cv.put("UserNmae", userName);
- cv.put("password", password);
- database.insert("Info", null, cv);
- return true;
- } catch (Exception ex) {
- return false;
- }
- }
- public boolean Update(String ipAddress, String dbName,
- String userName, String password) {
- try {
- ContentValues cv = new ContentValues();
- cv.put("IpAddress", ipAddress);
- cv.put("DBname", dbName);
- cv.put("UserName", userName);
- cv.put("Password", password);
- database.update("Info", cv, "ID=?", new String[] { "1" });
- return true;
- } catch (Exception ex) {
- return false;
- }
- }
- public Cursor Query() {
- Cursor c = database.query("Info", new String[] { "ID", "IpAddress",
- "DBname", "UserName", "Password" }, null, null, null,null, null);
- return c;
- }
- public void Delete() {
- database.delete("Info", null, null);
- } }
- public class FragmentEmployee extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- View view = inflater.inflate(R.layout.fragment_employee, container, false);
- return view;
- }
- public class MainActivity extends AppCompatActivity {
- Toolbar toolbar;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- getSupportActionBar().setDisplayHomeAsUpEnabled(true);
- TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
- tabLayout.addTab(tabLayout.newTab().setText("Employee"));
- tabLayout.addTab(tabLayout.newTab().setText("About Company"));
- tabLayout.addTab(tabLayout.newTab().setText("Products"));
- tabLayout.addTab(tabLayout.newTab().setText("Clients"));
- tabLayout.addTab(tabLayout.newTab().setText("Managers"));
- tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
- final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
- final PagerAdapter adapter = new PagerAdapter
- (getSupportFragmentManager(), tabLayout.getTabCount());
- viewPager.setAdapter(adapter);
- viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
- tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
- @Override
- public void onTabSelected(TabLayout.Tab tab) {
- viewPager.setCurrentItem(tab.getPosition());
- }
- @Override
- public void onTabUnselected(TabLayout.Tab tab) {
- }
- @Override
- public void onTabReselected(TabLayout.Tab tab) {
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- Intent intent=new Intent(this,SettingsActivity.class);
- startActivity(intent);
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement