Subscribe via email

Enter your email address and get Latest Material :

Tag

%temp% Android Application to understand Activity Android Applicaton login button should remain disabled Android Program C Language GTU MCA Syllabus PPT/Notes C full ppt by keval nagaria C++ GTU MCA CPI/CGPA To Percentage Calculator Core JAVA GTU MCA Core Java Create Bootable USB flash Drive Using DISKPART utility Create USB Pen Drive Create an application that will create database with table of User credential. Create an application that will have spinner with list of animation names. On selecting animation name Create an application to call specific entered number by user in the EditText Create an application to make Insert Crimping DBMS GTU MCA Syllabus PPT/Note DISKPART DOS Command to Erase temp file DS GTU MCA Delete and retrieve operation on the database. Erase/Delete Temp file Ethernet FON GTU MCA FON Practical Solution FON Program Fundamental Of Networking GTU MCA PPT GTU Student FInd CPI/CGPA To Percentage Converter Intent Java Chapter Wise Slide Show For GTU MCA Student Sem-3 LAN Cable Online CPI/CGPA Converter/Calculator PL_SQL GTU MCA Syllabus PPT/Note RG45 Connector Read messages from the mobile and display it on the screen. SS(System Software) GTU MCA Understand Menu option. a. Create an application that will change color of the screen Understand resource folders : a. Create spinner with strings taken from resource folder b. On changing spinner value WTAD GTU MCA WTAD Practical Solution Web Technology And Application Development (WTAD) Windwos 7 USB Pen Drive application that will play a media file from the memory car application to send message between two emulators based on selected options from the menu. change image. delete temp file into C drive draw line on the screen as user drag his finger gtu mca android program open any URL inside the application and clicking on any link from that URl should not open Native browser but that URL should open the same screen. read file from asset folder and copy it in memory card. take picture using native application that animation should affect on the images displayed below. to pick up any image from the native application gallery and display it on the screen. update

Exercise : 18
Create an application to make Insert , update , Delete and retrieve operation on the database.
Screen Shot:







XML :
main.xml :



    
    
        
    
    
    
    
    




search.xml :



    
    
    



    
    
        
    
    
    
    
    


JAVA :
EighteenActivity.java :
package kmn.Eighteen;

import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EighteenActivity extends Activity
{
    /** @author Y@@D * */
	SQLiteDatabase db;	 
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        createDB();
      //do insert
		Button btnInsert=(Button)findViewById(R.id.btnInsert );
        btnInsert.setOnClickListener(new OnClickListener() 
        {
			
			public void onClick(View arg0) 
			{
				 
				insert();
			}
		});
        Button btnClear=(Button)findViewById(R.id.btnClear );
        btnClear.setOnClickListener(new OnClickListener() 
        {			
			public void onClick(View arg0) 
			{				 
				clear();
			}
		});
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
    	super.onCreateOptionsMenu(menu);
    	CreateMenu(menu);
    	return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	return MenuChoice(item);
    }
    
    private void CreateMenu(Menu menu)
    {
    	MenuItem mnu1 = menu.add(0, 0, 0, "Insert");
    	{
    		mnu1.setAlphabeticShortcut('i');
    		 
    	}
    	MenuItem mnu2 = menu.add(0, 1, 1, "Search");
    	{
    		mnu2.setAlphabeticShortcut('s');
    		 
    	}
    	MenuItem mnu3 = menu.add(0, 2, 2, "Delete");
    	{
    		mnu3.setAlphabeticShortcut('d');
    		//mnu3.setIcon(R.drawable.icon);
    	}
    	MenuItem mnu4 = menu.add(0, 3, 3, "View");
    	{
    		mnu4.setAlphabeticShortcut('d');
    		//mnu3.setIcon(R.drawable.icon);
    	}
     }
    private boolean MenuChoice(MenuItem item)
    {
    	Intent intent=new Intent();
    	switch (item.getItemId()) 
    	{
    		case 0:
    			insert();
    			return true;
    		case 1:
    			 intent.setClass(EighteenActivity.this, Search.class);
				startActivity(intent);
				return true;
    		case 2:
    	//		intent.setClass(E18Activity.this, SelectRecord.class);
				startActivity(intent);		
    			return true;

    		case 3:
    			intent.setClass(EighteenActivity.this, ViewRecord.class);
    			startActivity(intent);		
    	    	return true;

    	}
    	return false;
    }
    public void createDB()
	{		 
		db=openOrCreateDatabase("Student.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
		db.setLocale(Locale.getDefault());
		db.setLockingEnabled(true);
		db.setVersion(1);
		String sql="create table if not exists Stud(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)";
		db.execSQL(sql);
	}
	public void insert()
	{		 
		 EditText txtName=(EditText)findViewById(R.id.txtName);
		 EditText txtAge=(EditText)findViewById(R.id.txtAge);
		 if(txtName.getText().toString().equals(""))
		 {
			 Toast.makeText(EighteenActivity.this, "Enter Name.", Toast.LENGTH_SHORT).show();
	     }
		 else if (txtAge.getText().toString().equals(""))
		 {
			 Toast.makeText(EighteenActivity.this, "Enter Age.", Toast.LENGTH_SHORT).show();
		 }
		 else
		 {
		
			 String sql="insert into Stud(name,age) values('"+ txtName.getText().toString() +"',"+txtAge.getText().toString()+")";
			 db.execSQL(sql);
			 clear();
			 Toast.makeText(EighteenActivity.this, "Record Successfully Inserted.", Toast.LENGTH_SHORT).show();
		 }	     
	}
	public void clear()
	{
		EditText txtName=(EditText)findViewById(R.id.txtName);
		 EditText txtAge=(EditText)findViewById(R.id.txtAge);
		txtName.setText("");
		txtAge.setText("");
	 	
		txtName.clearFocus();
		txtAge.clearFocus();
		txtName.requestFocus();
		
	
	}
	@Override
    public void onDestroy()
	{
		super.onDestroy();
		db.close();
	}
}
Search.java :
package kmn.Eighteen;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;
/** @author Y@@D * */
public class Search extends Activity 
{
	SQLiteDatabase db;
	EditText txtSearch;
	EditText txtName;
	EditText txtAge;
	Button btnEdit;
	Button btnDelete;
	RelativeLayout rlRecord;
    RelativeLayout rlSearch;
    String recID="0";
	@Override
    public void onCreate(Bundle savedInstanceState) 
	{
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.search);
        
         db=openOrCreateDatabase("Student.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
        
          
     	txtName = (EditText)findViewById(R.id.txtName);
     	txtAge = (EditText)findViewById(R.id.txtAge);
        txtSearch = (EditText)findViewById(R.id.txtSearch);
        btnEdit=(Button)findViewById(R.id.btnEdit);
        btnDelete=(Button)findViewById(R.id.btnDelete);
        
        txtSearch.requestFocus();
        txtName.setEnabled(false);
        txtAge.setEnabled(false);
        btnEdit.setEnabled(false);
        btnDelete.setEnabled(false);
        
        Button btnSearch=(Button)findViewById(R.id.btnSearch);
        btnSearch.setOnClickListener(new OnClickListener() 
        {
			public void onClick(View arg0) {
 				if (txtSearch.getText().toString().equals(""))
				 {
					 Toast.makeText(Search.this, "Enter value.", Toast.LENGTH_SHORT).show();
				 }
				 else
				 {
					 searchRecord();
				 }
			}
		});
        //---------------Edit/update---------------------------------
        final Button btnEdit=(Button)findViewById(R.id.btnEdit);
        btnEdit.setOnClickListener(new OnClickListener() 
        {
			public void onClick(View arg0) 
			{
				 
 				if (btnEdit.getText().toString().equals("Edit"))
				 {
 					 btnEdit.setText("Update");
 					 txtName.setEnabled(true);
 			         txtAge.setEnabled(true);
 			         
 			         txtName.requestFocus();
 			        
 			        btnDelete.setEnabled(false);
 			       
				 }
				 else
				 {
					 txtName.setEnabled(false);
 			         txtAge.setEnabled(false);
 			        
 			        btnDelete.setEnabled(true);
 			       
					 btnEdit.setText("Edit");
					 String sql="update Stud set name='"+txtName.getText().toString()+"', age="+txtAge.getText().toString()+" where id="+recID;
				     db.execSQL(sql);
				     Toast.makeText(Search.this, "  Record Updated Successfully" , Toast.LENGTH_LONG).show();
				 }
			}
		});
        //------------------------Delete ---------------------------
        
        btnDelete.setOnClickListener(new OnClickListener() 
        {
			
			public void onClick(View arg0) 
			{
				// TODO Auto-generated method stub
				AlertDialog.Builder alertbox = new AlertDialog.Builder(arg0.getContext());
				alertbox.setIcon(android.R.drawable.ic_dialog_alert);
				alertbox.setTitle("Confirm");				
				alertbox.setMessage("Are You Sure? You want to delete this record");
				alertbox.setPositiveButton("Delete", new DialogInterface.OnClickListener() 
				{
				 	public void onClick(DialogInterface arg0, int arg1) 
				 	{
						// TODO Auto-generated method stub
				 		String sql="Delete from Stud where id="+recID;
					    db.execSQL(sql);
						Toast.makeText(getApplicationContext(), "Record Deleted", Toast.LENGTH_LONG).show();
						//clear old search result 
						txtSearch.setText("");
						txtName.setText("");
						txtAge.setText("");
						txtSearch.requestFocus();
				 	}
				 });
		    	alertbox.setNegativeButton("  Cancel  ", new DialogInterface.OnClickListener() 
		    	{
			 		public void onClick(DialogInterface arg0, int arg1) 
					{
						// TODO Auto-generated method stub
						 
					}
				});
		    	alertbox.show();
		    }	
		});
    }
	public void searchRecord()
	{
		try
        {
        	txtSearch = (EditText)findViewById(R.id.txtSearch);
        	txtName = (EditText)findViewById(R.id.txtName);
        	txtAge = (EditText)findViewById(R.id.txtAge);
        	Cursor c=db.rawQuery("select id,name,age from Stud where id="+ txtSearch.getText().toString(), null);
       	    if(c.getCount()>0)
            {
            	 c.moveToNext();
               	 recID= c.getString(0);
            	 txtName.setText( c.getString(1));
                 txtAge.setText(c.getString(2));
          	     btnEdit.setEnabled(true);
                 btnDelete.setEnabled(true);
            }
            else
            {
            	Toast.makeText(this, "No Record Found" , Toast.LENGTH_LONG).show();
            }
        }
        catch(Exception e)
        {
        	Toast.makeText(this, ""+e, Toast.LENGTH_LONG).show();
        }
	} 
	public void onDestroy()
	{
		super.onDestroy();
		db.close();
	}
}
ViewRecord.java :
package kmn.Eighteen;
/** @author Y@@D **/
import java.util.ArrayList;

import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Toast;
 public class ViewRecord extends ListActivity 
 {
	SQLiteDatabase db;
	@Override
    public void onCreate(Bundle savedInstanceState) 
	{
        super.onCreate(savedInstanceState);
        try
        {
        	db=openOrCreateDatabase("Student.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);        	 
        	Cursor c=db.rawQuery("select id,name,age from Stud", null);
       	    ArrayList list  = new ArrayList();
        	 
            int count=c.getCount();
                        
            if(c.getCount()>0)
            {
               	while(c.moveToNext())
            	{
            	  list.add(c.getString(0)+" , "+c.getString(1)+" , "+c.getString(2));
               	}               	
            	c.close();
            	Toast.makeText(this,"Total Records: "+count, Toast.LENGTH_LONG).show();
            	ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
            	getListView().setAdapter(adapter);
            }
            else
            {
            	Toast.makeText(this, "No Record Found" , Toast.LENGTH_LONG).show();
            }
        }
        catch(Exception e)
        {
        	Toast.makeText(this, ""+e, Toast.LENGTH_LONG).show();
        }
    }
	public void onDestroy()
	{
		super.onDestroy();
		db.close();
	}
}
 

Related Posts Plugin for WordPress, Blogger...