Hi can anyone help me in below code?
Once a record with 123 id is inserted and next time it should not accept for 123 id again to insert in database.... so please help me to complete this project...
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;
TableRow tableRow;
TextView textview,textview1,textview2,textview3,textview4,textview5;
@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 Showdata(View view)
{
Cursor c=db.rawQuery("SELECT * FROM EMP;",null);
int count=c.getCount();
c.moveToFirst();
TableLayout tableLayout= new TableLayout(getApplicationContext());
tableLayout.setVerticalScrollBarEnabled(true);
TableRow tableRow;
TextView textview,textview1,textview2,textview3,textview4,textview5;
tableRow=new TableRow(getApplicationContext());
textview=new TextView(getApplicationContext());
textview.setText("Empid");
textview.setTextColor(Color.RED);
textview.setTypeface(null,Typeface.BOLD);
textview.setPadding(0, 5, 3, 5);
tableRow.addView(textview);
textview4=new TextView(getApplicationContext());
textview4.setText("Ename");
textview4.setTextColor(Color.RED);
textview4.setTypeface(null,Typeface.BOLD);
textview4.setPadding(15, 10, 3, 5);
tableRow.addView(textview4);
textview5=new TextView(getApplicationContext());
textview5.setText("Email");
textview5.setTextColor(Color.RED);
textview5.setTypeface(null,Typeface.BOLD);
textview5.setPadding(25, 5, 3, 5);
tableRow.addView(textview5);
tableLayout.addView(tableRow);
for (Integer j=0; j< count; j++)
{
tableRow= new TableRow(getApplicationContext());
textview1=new TextView(getApplicationContext());
textview1.setText(c.getString(c.getColumnIndex("id")));
textview2=new TextView(getApplicationContext());
textview2.setText(c.getString(c.getColumnIndex("name")));
textview3=new TextView(getApplicationContext());
textview3.setText(c.getString(c.getColumnIndex("email")));
textview1.setPadding(0, 5, 3, 5);
textview2.setPadding(15, 5, 3, 5);
textview3.setPadding(25, 5, 3, 5);
tableRow.addView(textview1);
tableRow.addView(textview2);
tableRow.addView(textview3);
tableLayout.addView(tableRow);
c.moveToNext();
}
setContentView(tableLayout);
db.close();
}
public void Exit(View view){
System.exit(0);
}
}