Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have started coding for Finder app which contains employee records and user will be able to find employee record by using 'id' when click on Search button in useractivity.java and data should display in Empdetails.java activity in textview contains 3 textfields to display. Below is my code Please help me in this

My requirement : I want to retrieve a single record by giving an id in UserActivity.java and when i click on Search button it should get a (select * from emp;)record details in Empdetails.java in text view

Output:

id: 12345

name : vijay kumar

Email: vijay@gmail.com

like above shown

Thanks inadvance,
Vijay Kumar


1) AdminActivity.java

Java
package com.example.finder;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class AdminActivity extends Activity {

	String id,name,email;
	SQLiteDatabase db;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_admin);
		db=openOrCreateDatabase("EMP",MODE_PRIVATE,null);
		db.execSQL("CREATE TABLE IF NOT EXISTS EMP(id integer primary key,name varchar,email varchar);");
	}

	public void Submit(View view)
	{		
		EditText edittext1=(EditText) findViewById(R.id.id);
		EditText edittext2=(EditText) findViewById(R.id.name);
		EditText edittext3=(EditText) findViewById(R.id.email);
		id=edittext1.getText().toString();
		name=edittext2.getText().toString();
		email=edittext3.getText().toString();
		db.execSQL("INSERT INTO EMP(id,name,email) values ('"+id+"','"+name+"','"+email+"');");
		
		if(id.equals("") && name.equals("") && email.equals(""))
	    {
			Toast.makeText(getApplicationContext(), "Please enter details and submit", Toast.LENGTH_SHORT).show();
		}
		else 
			if(id.equals(""))
		   {
				Toast toast=new Toast (getApplicationContext());
				toast.setDuration(Toast.LENGTH_LONG);
				Toast.makeText(getApplicationContext(), "Enter id....", Toast.LENGTH_SHORT).show();
		   }
		else
			if(name.equals(""))
			   {
				Toast toast=new Toast (getApplicationContext());
				toast.setDuration(Toast.LENGTH_LONG);
			    Toast.makeText(getApplicationContext(), "Enter Name....", Toast.LENGTH_SHORT).show();
			   }
			else
		         if(email.equals(""))
			    {
		        Toast toast=new Toast (getApplicationContext());
		        toast.setDuration(Toast.LENGTH_LONG);
			     Toast.makeText(getApplicationContext(), "Enter email.....", Toast.LENGTH_SHORT).show();
			    }
		         else 
		            { 
		        	    Toast toast=new Toast (getApplicationContext());
		        	   	toast.setDuration(Toast.LENGTH_LONG);
		        	   	edittext1.setText("");
			        	edittext2.setText("");
			        	edittext3.setText("");
			        	Toast.makeText(getApplicationContext(), "Data submitted successfully", Toast.LENGTH_SHORT).show();
			        	db.close();
		        	 }
				}
	
	public void Exit(View view){
		System.exit(0);
	}
	
	}


2) UserActivity.java

Java
package com.example.finder;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class UserActivity extends Activity {
	
	SQLiteDatabase db;
	String id;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user);

	}

public void Search(View view){

////what is the logic/code here i have to use?????
}

public void Update(View view){

////what is the logic/code here i have to use?????
}

public void Delete(View view){

////what is the logic/code here i have to use?????
}


public void Exit(View view){
	System.exit(0);
}

}
Posted
Updated 23-Jan-14 2:52am
v5
Comments
maramganti 13-Jan-14 23:05pm    
anybody please suggest the code for the above ........

1 solution

All of the CRUD operations are briefly explained below links :
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/[^]
http://hmkcode.com/android-simple-sqlite-database-tutorial/[^]
http://javapapers.com/android/android-sqlite-database/[^]

So with the help of this, you can perform your own CRUD functions !

Good Luck !
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900