SQLLITE_GUJ_INFO_ListView
Step 1:activity_main &java
Step 2:delete xml & java
Step 3:update xml & java
Atep 4;single.xml
Step 5:dbhelper.java
Step 6:persondata.java(pojo)
Step 1:activity_main &java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="demoproject.aalap.com.myapplication.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtemail"
android:hint="enter email"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtpass"
android:hint="enter password"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="enter re-password"
android:id="@+id/rpass"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="insert"
android:id="@+id/insert"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list"></ListView>
</LinearLayout>
Step 1:MainAvtivity.java
package demoproject.aalap.com.myapplication;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
EditText e1,e2,e3;
Button b1;
persondata p;
dbhelper db;
ListView lv;
private ArrayList<persondata> ar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText)findViewById(R.id.txtemail);
e2=(EditText)findViewById(R.id.txtpass);
e3=(EditText)findViewById(R.id.rpass);
b1=(Button)findViewById(R.id.insert);
lv=(ListView)findViewById(R.id.list);
db=new dbhelper(MainActivity.this);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String em=e1.getText().toString();
String pws=e2.getText().toString();
String rpwd=e3.getText().toString();
if(em.length()==0||pws.length()==0||rpwd.length()==0)
{
Toast.makeText(MainActivity.this,"fill all field",Toast.LENGTH_LONG).show();
e1.setError("enter email");
e2.setError("enter password");
e3.setError("enter re-paaword");
}
else if(!pws.equals(rpwd))
{
Toast.makeText(MainActivity.this,"password is not match",Toast.LENGTH_LONG).show();
e2.setError("password is not match");
e3.setError("password is not match");
}
else if(pws.length()<6)
{
Toast.makeText(MainActivity.this,"minimum length is 6 character",Toast.LENGTH_LONG).show();
e2.setError("enter password");
}
else
{
p=new persondata();
p.setEmail(em);
p.setPass(pws);
db.insertdata(p);
finish();
startActivity(getIntent());
}
}
});
ar=db.getdata();
custumadeptert ad=new custumadeptert(MainActivity.this,ar);
lv.setAdapter(ad);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
persondata p=(persondata)ar.get(i);
Intent intent=new Intent(MainActivity.this,delete.class);
int k=p.getId();
intent.putExtra("id",String.valueOf(k));
intent.putExtra("email",p.getEmail());
intent.putExtra("pass",p.getPass());
startActivity(intent);
}
});
}
public class custumadeptert extends BaseAdapter {
Context context;
LayoutInflater inflater;
ArrayList<persondata>ar;
TextView tv;
persondata p;
custumadeptert(Context context,ArrayList<persondata> ar)
{
this.context=context;
this.ar=ar;
}
@Override
public int getCount() {
return ar.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) {
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v=inflater.inflate(R.layout.single,parent,false);
tv=(TextView)v.findViewById(R.id.txtviw);
p=(persondata)ar.get(position);
tv.setText(p.getId()+" "+p.getEmail()+" "+p.getPass());
return v;
}
}
}
Step 2:update.xml & java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="demoproject.aalap.com.myapplication.update">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/uemail"
android:hint="enter email"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pwd"
android:hint="enter password"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/update"
android:text="update"/>
</LinearLayout>
Step 2:update.java
package demoproject.aalap.com.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class update extends AppCompatActivity {
EditText e1,e2;
Button b1;
persondata p;
dbhelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
e1=(EditText)findViewById(R.id.uemail);
e2=(EditText)findViewById(R.id.pwd);
b1=(Button)findViewById(R.id.update);
db=new dbhelper(update.this);
Intent a=getIntent();
final String id1=a.getStringExtra("id1");
final String eml=a.getStringExtra("email");
String pwd=a.getStringExtra("pass");
e1.setText(id1+" "+eml);
e2.setText(pwd);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
p=new persondata();
p.setEmail(e1.getText().toString());
p.setPass(e2.getText().toString());
db.updatedata(p,id1);
Intent i=new Intent(update.this,MainActivity.class);
startActivity(i);
}
});
}
}
Step 3:single.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtviw"
android:text="text"/>
</LinearLayout>
Step 4:delete xml & java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="demoproject.aalap.com.myapplication.delete">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn1"
android:text="delete"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="update"
android:id="@+id/upd"/>
</LinearLayout>
Step 4:delete.java
package demoproject.aalap.com.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class delete extends AppCompatActivity {
TextView t1;
Button b1,b2;
dbhelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delete);
t1=(TextView)findViewById(R.id.tv1);
b1=(Button)findViewById(R.id.btn1);
b2=(Button)findViewById(R.id.upd);
db=new dbhelper(delete.this);
Intent i=getIntent();
final String id=i.getStringExtra("id");
final String em=i.getStringExtra("email");
final String ps=i.getStringExtra("pass");
t1.setText(id+" "+em+" "+ps);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int k=Integer.parseInt(id);
db.deletedata(k);
Intent i=new Intent(delete.this,MainActivity.class);
startActivity(i);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent a=new Intent(delete.this,update.class);
a.putExtra("email",em);
a.putExtra("pass",ps);
a.putExtra("id1",id);
startActivity(a);
}
});
}
}
Step 5:dbhelper.java
package demoproject.aalap.com.myapplication;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
/**
* Created by ADMIN on 25-07-2018.
*/
public class dbhelper extends SQLiteOpenHelper {
private static String dbname="mydatabase";
private static int dbvirsion=1;
private static String tbname="persiond";
private static String uid="id";
private static String uemail="email";
private static String upass="pass";
ArrayList<persondata>ar;
dbhelper(Context context)
{
super(context,dbname,null,dbvirsion);
}
@Override
public void onCreate(SQLiteDatabase db)
{
String sql="create table "+tbname+" ("+uid+" integer primary key autoincrement, "+uemail+" varchar(30), "+upass+" varchar(30))";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void insertdata(persondata p)
{
SQLiteDatabase db=getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(uemail,p.getEmail());
cv.put(upass,p.getPass());
db.insert(tbname,null,cv);
}
public ArrayList<persondata> getdata()
{
ar=new ArrayList<>();
SQLiteDatabase db=this.getReadableDatabase();
String sql="Select *, "+uid+" as _id from "+tbname;
Cursor c=db.rawQuery(sql,null);
while (c.moveToNext())
{
persondata p=new persondata();
int id=c.getInt(0);
String em=c.getString(1);
String ps=c.getString(2);
p.setId(id);
p.setEmail(em);
p.setPass(ps);
ar.add(p);
}
return ar;
}
public void deletedata(int id)
{
SQLiteDatabase db=this.getWritableDatabase();
String sql="delete from "+tbname+" where "+uid+"="+id;
db.execSQL(sql);
}
public void updatedata(persondata p,String id)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(uemail,p.getEmail());
cv.put(upass,p.getPass());
db.update(tbname,cv,uid+"=?",new String[]{id});
}
}
Step 6:persondata.java(pojo class)
package demoproject.aalap.com.myapplication;
/**
* Created by ADMIN on 25-07-2018.
*/
public class persondata {
int id;
String email,pass;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Step 2:delete xml & java
Step 3:update xml & java
Atep 4;single.xml
Step 5:dbhelper.java
Step 6:persondata.java(pojo)
Step 1:activity_main &java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="demoproject.aalap.com.myapplication.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtemail"
android:hint="enter email"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtpass"
android:hint="enter password"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="enter re-password"
android:id="@+id/rpass"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="insert"
android:id="@+id/insert"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list"></ListView>
</LinearLayout>
Step 1:MainAvtivity.java
package demoproject.aalap.com.myapplication;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
EditText e1,e2,e3;
Button b1;
persondata p;
dbhelper db;
ListView lv;
private ArrayList<persondata> ar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText)findViewById(R.id.txtemail);
e2=(EditText)findViewById(R.id.txtpass);
e3=(EditText)findViewById(R.id.rpass);
b1=(Button)findViewById(R.id.insert);
lv=(ListView)findViewById(R.id.list);
db=new dbhelper(MainActivity.this);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String em=e1.getText().toString();
String pws=e2.getText().toString();
String rpwd=e3.getText().toString();
if(em.length()==0||pws.length()==0||rpwd.length()==0)
{
Toast.makeText(MainActivity.this,"fill all field",Toast.LENGTH_LONG).show();
e1.setError("enter email");
e2.setError("enter password");
e3.setError("enter re-paaword");
}
else if(!pws.equals(rpwd))
{
Toast.makeText(MainActivity.this,"password is not match",Toast.LENGTH_LONG).show();
e2.setError("password is not match");
e3.setError("password is not match");
}
else if(pws.length()<6)
{
Toast.makeText(MainActivity.this,"minimum length is 6 character",Toast.LENGTH_LONG).show();
e2.setError("enter password");
}
else
{
p=new persondata();
p.setEmail(em);
p.setPass(pws);
db.insertdata(p);
finish();
startActivity(getIntent());
}
}
});
ar=db.getdata();
custumadeptert ad=new custumadeptert(MainActivity.this,ar);
lv.setAdapter(ad);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
persondata p=(persondata)ar.get(i);
Intent intent=new Intent(MainActivity.this,delete.class);
int k=p.getId();
intent.putExtra("id",String.valueOf(k));
intent.putExtra("email",p.getEmail());
intent.putExtra("pass",p.getPass());
startActivity(intent);
}
});
}
public class custumadeptert extends BaseAdapter {
Context context;
LayoutInflater inflater;
ArrayList<persondata>ar;
TextView tv;
persondata p;
custumadeptert(Context context,ArrayList<persondata> ar)
{
this.context=context;
this.ar=ar;
}
@Override
public int getCount() {
return ar.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) {
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v=inflater.inflate(R.layout.single,parent,false);
tv=(TextView)v.findViewById(R.id.txtviw);
p=(persondata)ar.get(position);
tv.setText(p.getId()+" "+p.getEmail()+" "+p.getPass());
return v;
}
}
}
Step 2:update.xml & java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="demoproject.aalap.com.myapplication.update">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/uemail"
android:hint="enter email"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pwd"
android:hint="enter password"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/update"
android:text="update"/>
</LinearLayout>
Step 2:update.java
package demoproject.aalap.com.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class update extends AppCompatActivity {
EditText e1,e2;
Button b1;
persondata p;
dbhelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
e1=(EditText)findViewById(R.id.uemail);
e2=(EditText)findViewById(R.id.pwd);
b1=(Button)findViewById(R.id.update);
db=new dbhelper(update.this);
Intent a=getIntent();
final String id1=a.getStringExtra("id1");
final String eml=a.getStringExtra("email");
String pwd=a.getStringExtra("pass");
e1.setText(id1+" "+eml);
e2.setText(pwd);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
p=new persondata();
p.setEmail(e1.getText().toString());
p.setPass(e2.getText().toString());
db.updatedata(p,id1);
Intent i=new Intent(update.this,MainActivity.class);
startActivity(i);
}
});
}
}
Step 3:single.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtviw"
android:text="text"/>
</LinearLayout>
Step 4:delete xml & java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="demoproject.aalap.com.myapplication.delete">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn1"
android:text="delete"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="update"
android:id="@+id/upd"/>
</LinearLayout>
Step 4:delete.java
package demoproject.aalap.com.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class delete extends AppCompatActivity {
TextView t1;
Button b1,b2;
dbhelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delete);
t1=(TextView)findViewById(R.id.tv1);
b1=(Button)findViewById(R.id.btn1);
b2=(Button)findViewById(R.id.upd);
db=new dbhelper(delete.this);
Intent i=getIntent();
final String id=i.getStringExtra("id");
final String em=i.getStringExtra("email");
final String ps=i.getStringExtra("pass");
t1.setText(id+" "+em+" "+ps);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int k=Integer.parseInt(id);
db.deletedata(k);
Intent i=new Intent(delete.this,MainActivity.class);
startActivity(i);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent a=new Intent(delete.this,update.class);
a.putExtra("email",em);
a.putExtra("pass",ps);
a.putExtra("id1",id);
startActivity(a);
}
});
}
}
Step 5:dbhelper.java
package demoproject.aalap.com.myapplication;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
/**
* Created by ADMIN on 25-07-2018.
*/
public class dbhelper extends SQLiteOpenHelper {
private static String dbname="mydatabase";
private static int dbvirsion=1;
private static String tbname="persiond";
private static String uid="id";
private static String uemail="email";
private static String upass="pass";
ArrayList<persondata>ar;
dbhelper(Context context)
{
super(context,dbname,null,dbvirsion);
}
@Override
public void onCreate(SQLiteDatabase db)
{
String sql="create table "+tbname+" ("+uid+" integer primary key autoincrement, "+uemail+" varchar(30), "+upass+" varchar(30))";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void insertdata(persondata p)
{
SQLiteDatabase db=getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(uemail,p.getEmail());
cv.put(upass,p.getPass());
db.insert(tbname,null,cv);
}
public ArrayList<persondata> getdata()
{
ar=new ArrayList<>();
SQLiteDatabase db=this.getReadableDatabase();
String sql="Select *, "+uid+" as _id from "+tbname;
Cursor c=db.rawQuery(sql,null);
while (c.moveToNext())
{
persondata p=new persondata();
int id=c.getInt(0);
String em=c.getString(1);
String ps=c.getString(2);
p.setId(id);
p.setEmail(em);
p.setPass(ps);
ar.add(p);
}
return ar;
}
public void deletedata(int id)
{
SQLiteDatabase db=this.getWritableDatabase();
String sql="delete from "+tbname+" where "+uid+"="+id;
db.execSQL(sql);
}
public void updatedata(persondata p,String id)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(uemail,p.getEmail());
cv.put(upass,p.getPass());
db.update(tbname,cv,uid+"=?",new String[]{id});
}
}
Step 6:persondata.java(pojo class)
package demoproject.aalap.com.myapplication;
/**
* Created by ADMIN on 25-07-2018.
*/
public class persondata {
int id;
String email,pass;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Comments
Post a Comment