· 6 years ago · Jun 18, 2019, 09:10 AM
1StoreDbHelper db;
2Button add_data;
3EditText add_name;
4ArrayList<String> listItem;
5ArrayAdapter adapter;
6ListView userlist;
7@Override
8protected void onCreate(Bundle savedInstanceState) {
9 super.onCreate(savedInstanceState);
10 setContentView(R.layout.activity_welcome);
11 FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
12 fab.setOnClickListener(new View.OnClickListener() {
13 @Override
14 public void onClick(View view) {
15 Intent intent = new Intent(Welcome.this, EditorActivity.class);
16 startActivity(intent);
17 }
18 });
19
20
21 db = new StoreDbHelper(this);
22 add_data = findViewById(R.id.add_data);
23 add_name = findViewById(R.id.add_name);
24 userlist = findViewById(R.id.users_list);
25
26 listItem = new ArrayList<>();
27 viewData();
28 userlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
29 @Override
30 public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
31 String text = userlist.getItemAtPosition(i).toString();
32 Toast.makeText(Welcome.this, ""+text, Toast.LENGTH_SHORT).show();
33
34 }
35 });
36
37 add_data.setOnClickListener(new View.OnClickListener() {
38 @Override
39 public void onClick(View v) {
40 String name = add_name.getText().toString();
41 if (!name.equals("") && db.insertChemical(name)) {
42 Toast.makeText(Welcome.this, "Data added", Toast.LENGTH_SHORT).show();
43 add_name.setText("");
44 listItem.clear();
45 viewData();
46 }else {
47 Toast.makeText(Welcome.this, "Data not added", Toast.LENGTH_SHORT).show();
48
49 }
50 }
51
52 });
53}
54
55private static final int DATABASE_VERSION = 1;
56private static final String CREATE_TABLE = "CREATE TABLE " + StoreEntry.TABLE_NAME + " ("
57 + StoreEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
58 + StoreEntry.COLUMN_NAME + " TEXT NOT NULL, "
59 + StoreEntry.COLUMN_ItemNumber + " INTEGER NOT NULL, "
60 + StoreEntry.COLUMN_CodeNumber + " INTEGER NOT NULL, "
61 + StoreEntry.COLUMN_QUANTITY + " INTEGER NOT NULL DEFAULT 0);";
62
63 public StoreDbHelper(Context context) {
64 super(context, DATABASE_NAME, null, DATABASE_VERSION);
65 }
66
67 /**
68 * This is called when the database is created for the first time.
69 */
70 @Override
71 public void onCreate(SQLiteDatabase db) {
72 // Create a String that contains the SQL statement to create the pets table
73
74
75 // Execute the SQL statement
76 db.execSQL(CREATE_TABLE);
77 }
78@Override
79public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
80 // The database is still at version 1, so there's nothing to do be done here.
81 db.execSQL("DROP TABLE IF EXISTS "+"TABLE_NAME");
82 onCreate(db);
83}
84public boolean insertChemical(String name){
85 SQLiteDatabase db = this.getWritableDatabase();
86 ContentValues values = new ContentValues();
87 values.put(StoreEntry.COLUMN_NAME, name);
88 long result = db.insert(StoreEntry.TABLE_NAME, null, values);
89 return result !=-1;
90}
91
92public Cursor viewData(){
93 SQLiteDatabase db = this.getReadableDatabase();
94 String query = "select * from "+StoreEntry.TABLE_NAME;
95 Cursor cursor = db.rawQuery(query,null);
96 return cursor;
97}