Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.30 KB | None | 0 0
  1. =================================================================================
  2.  
  3. >>>>>>>>>>>> Main Activity <<<<<<<<<<<<<<<
  4.  
  5. =================================================================================
  6. public class mainActivity extends AppCompatActivity {
  7.  
  8.     Button subjects;
  9.     ListView lst;
  10.     sqlite db = new sqlite(this);
  11.  
  12.     @Override
  13.     protected void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.         setContentView(R.layout.main);
  16.  
  17.         subjects = findViewById(R.id.subjects);
  18.  
  19.         lst = findViewById(R.id.lst);
  20.  
  21.         subjects.setOnClickListener(new View.OnClickListener() {
  22.             @Override
  23.             public void onClick(View v) {
  24.                 openDialog();
  25.                 showData();
  26.             }
  27.         });
  28.         }
  29.  
  30.     public void openDialog(){
  31.  
  32.         showDialog dialog = new showDialog();
  33.         dialog.show(getSupportFragmentManager(),"dialog");
  34.     }
  35.  
  36.     public void showData(){
  37.         ArrayList arrayList = db.showTableName();
  38.         ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,arrayList);
  39.         lst.setAdapter(arrayAdapter);
  40.     }
  41. }
  42.  
  43. =================================================================================
  44.  
  45. >>>>>>>>>>>> sqlite  <<<<<<<<<<<<<<<
  46.  
  47. =================================================================================
  48. public class sqlite extends SQLiteOpenHelper{
  49.  
  50.     private static final String dbName = "data.db";
  51.  
  52.     public sqlite(Context context) {
  53.         super(context, dbName, null, 1);
  54.     }
  55.  
  56.     @Override
  57.     public void onCreate(SQLiteDatabase db) {
  58.         db.execSQL("CREATE TABLE IF NOT EXISTS tableName (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
  59.                 "NAME TEXT)");
  60.  
  61.     }
  62.  
  63.     @Override
  64.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  65.         onCreate(db);
  66.     }
  67.  
  68.  
  69.     public Boolean addTableName(String mytableName) {
  70.         SQLiteDatabase db = this.getWritableDatabase();
  71.         ContentValues content = new ContentValues();
  72.  
  73.         content.put("NAME",mytableName);
  74.  
  75.         long resualt = db.insert("tableName",null,content);
  76.         return resualt != -1;
  77.     }
  78.  
  79.     public ArrayList showTableName(){
  80.         ArrayList array = new ArrayList();
  81.         SQLiteDatabase db = this.getReadableDatabase();
  82.  
  83.         Cursor res = db.rawQuery("SELECT NAME FROM tableName",null);
  84.         res.moveToFirst();
  85.         while (!res.isAfterLast()){
  86.            String name = res.getString(res.getColumnIndex("NAME"));
  87.            array.add(name);
  88.            res.moveToNext();
  89.         }
  90.         return array;
  91.     }
  92. }
  93. =================================================================================
  94.  
  95. >>>>>>>>>>>> AlertDialog  <<<<<<<<<<<<<<<
  96.  
  97. =================================================================================
  98. public class showDialog extends AppCompatDialogFragment {
  99.     private EditText createTable;
  100.     Button yes,no;
  101.     sqlite db = new sqlite(getActivity());
  102.  
  103.  
  104.     @Override
  105.     public Dialog onCreateDialog(Bundle savedInstanceState) {
  106.         AlertDialog.Builder alert = new AlertDialog.Builder(getActivity(),R.style.CustomDialog);
  107.         LayoutInflater inflater = getActivity().getLayoutInflater();
  108.         View view = inflater.inflate(R.layout.dialog,null);
  109.         alert.setView(view);
  110.  
  111.         yes = view.findViewById(R.id.btn_yes);
  112.         no = view.findViewById(R.id.btn_no);
  113.  
  114.         createTable = view.findViewById(R.id.createTable);
  115.  
  116.         yes.setOnClickListener(new View.OnClickListener() {
  117.             @Override
  118.             public void onClick(View v) {
  119.                 String tableName = createTable.getText().toString();
  120.                 if (!tableName.isEmpty()){
  121.                     db.addTableName(tableName);
  122.                     dismiss();
  123.                 } else {
  124.                     Toast.makeText(getContext(), "Please add a valid Name", Toast.LENGTH_SHORT).show();
  125.                 }
  126.             }
  127.         });
  128.         no.setOnClickListener(new View.OnClickListener() {
  129.             @Override
  130.             public void onClick(View v) {
  131.                 dismiss();
  132.             }
  133.         });
  134.         return alert.create();
  135.     }
  136. }
  137. =================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement