0% found this document useful (0 votes)
36 views

Coding - Mobile App My Words

The document contains code for an Android application that allows users to insert words, meanings, and usages into an SQLite database and view them in a list. It includes classes for the database controller, main activity for data entry, and custom adapter for the list view. The database controller creates and manages the database. The main activity inserts data and starts the list activity. The custom adapter populates the list view from the database contents.

Uploaded by

Shalu Ojha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Coding - Mobile App My Words

The document contains code for an Android application that allows users to insert words, meanings, and usages into an SQLite database and view them in a list. It includes classes for the database controller, main activity for data entry, and custom adapter for the list view. The database controller creates and manages the database. The main activity inserts data and starts the list activity. The custom adapter populates the list view from the database contents.

Uploaded by

Shalu Ojha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

5.

2 Coding Details

Database Controller Module

package com.example.mywords;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Controllerdb extends SQLiteOpenHelper


{
private static final String DATABASE_NAME="SqliteListviewDB";
public Controllerdb(Context context) {
super(context, DATABASE_NAME, null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//create table to insert data
String query;
query = "CREATE TABLE IF NOT EXISTS Vocab(Id INTEGER PRIMARY KEY
AUTOINCREMENT,word VARCHAR,meaning VARCHAR,usage VARCHAR);";
db.execSQL(query);

}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query ;
query = "DROP TABLE IF EXISTS Vocab";
db.execSQL(query);
onCreate(db);
}
}

Data Insertion Module


package com.example.mywords;

import android.content.Intent;

import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener


{

Controllerdb db =new Controllerdb(this);

SQLiteDatabase database;

EditText word,meaning,usage;

Button Submitdatabtn,Showdatabtn;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

word= (EditText) findViewById(R.id.etWord);

meaning= (EditText) findViewById(R.id.etMeaning);

usage= (EditText) findViewById(R.id.etUsage);

Submitdatabtn= (Button) findViewById(R.id.btnSave);

Showdatabtn=(Button) findViewById(R.id.btnShow);

Submitdatabtn.setOnClickListener(this);

Showdatabtn.setOnClickListener(this);

@Override

public void onClick(View v)

if(v.getId()==R.id.btnSave)

{
database=db.getWritableDatabase();

database.execSQL("INSERT INTO
Vocab(word,meaning,usage)VALUES('"+word.getText()+"','"+meaning.getText()
+"','"+usage.getText()+"')" );

Toast.makeText(this,"Data Inserted To Sqlite


Database",Toast.LENGTH_LONG).show();

word.setText("");

meaning.setText("");

usage.setText("");

else if(v.getId()==R.id.btnShow)

Intent intent=new Intent(this,ShowDataListView.class);

startActivity(intent);

Adapter Module
package com.example.mywords;

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.TextView;
import java.util.ArrayList;

public class CustomAdapter extends BaseAdapter {

private Context mContext;

Controllerdb controldb;

SQLiteDatabase db;

private ArrayList<String> Id = new ArrayList<String>();

private ArrayList<String> word = new ArrayList<String>();

private ArrayList<String> meaning = new ArrayList<String>();

private ArrayList<String> usage = new ArrayList<String>();

public CustomAdapter(Context context,ArrayList<String> Id,ArrayList<String>


word, ArrayList<String> meaning,ArrayList<String> usage

this.mContext = context;

this.Id = Id;

this.word = word;

this.meaning = meaning;

this.usage=usage;

@Override

public int getCount() {

return Id.size();

@Override

public Object getItem(int position) {

return null;

@Override

public long getItemId(int position) {

return 0;

}
@Override

public View getView(int position, View convertView, ViewGroup parent) {

You might also like