" Jai Saraswati Maa"
Hi ... Dear All ..Today I am uploading a superb example of Customized List View with Edit & Delete Functionality on each Row - Reflecting with SQLite DataBase.
***** Please refer first my previous post
**** It's Just a Mini App you can add such functionality with your App.
**** Please have a patience.
FirstName i am taken as userName for login Purpose.
Please check.
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box.
A. Output Screen Will be like ---
1. First Screen on Which you have all options ... Please Register First
2 Registration Form
3 Login Screen
4 View All Saved data in a List with Buttons
5 If you click on Edit so you can change First Name & Last Name only ....
B. All xml files are
1. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
2. loglay.xml
1. MainActivity.java is as
Hi ... Dear All ..Today I am uploading a superb example of Customized List View with Edit & Delete Functionality on each Row - Reflecting with SQLite DataBase.
***** Please refer first my previous post
Registration & Login Simple One
http://androidclue4u.blogspot.in/2016/04/registration-login-simple-one.html
**** It's Just a Mini App you can add such functionality with your App.
**** Please have a patience.
FirstName i am taken as userName for login Purpose.
Please check.
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box.
A. Output Screen Will be like ---
1. First Screen on Which you have all options ... Please Register First
2 Registration Form
3 Login Screen
4 View All Saved data in a List with Buttons
5 If you click on Edit so you can change First Name & Last Name only ....
B. All xml files are
1. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.exam.ravi.customlistwithbutton.MainActivity"> <Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/logBut"
android:onClick="logUser"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp" /> <Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New User"
android:id="@+id/newuserBut"
android:onClick="registerUser"
android:layout_below="@+id/logBut"
android:layout_alignEnd="@+id/logBut"
android:layout_marginTop="75dp" /> <Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ViewAll"
android:id="@+id/button2"
android:layout_below="@+id/newuserBut"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:onClick="viewAll" /> </RelativeLayout>
2. loglay.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/logUser"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="54dp"
android:hint="UserName"
android:layout_alignParentEnd="true" /> <EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/logPass" android:layout_below="@+id/logUser"
android:layout_alignParentStart="true"
android:layout_marginTop="40dp"
android:hint="Password"
android:inputType="textPassword"
android:layout_alignEnd="@+id/logUser" /> <Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login Me"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="loginUser" /> </RelativeLayout>
3. reguser.xml
C. Java Code<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"> <EditText android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/fName"android:layout_alignParentTop="true"android:layout_alignParentStart="true"android:layout_marginTop="58dp"android:hint="First Name"android:layout_alignParentEnd="true" /> <EditText android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/lName"android:layout_below="@+id/fName"android:layout_alignParentStart="true"android:layout_marginTop="42dp"android:hint="Last Name" android:layout_alignParentEnd="true" /> <EditText android:layout_width="wrap_content"android:layout_height="wrap_content"android:inputType="textPassword"android:ems="10" android:id="@+id/userPass"android:layout_centerVertical="true"android:hint=" Password " android:layout_alignParentStart="true"android:layout_alignParentEnd="true" /> <Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Register"android:id="@+id/regist"android:layout_below="@+id/userPass"android:layout_centerHorizontal="true"android:layout_marginTop="50dp"android:onClick="regMe"/> </RelativeLayout>4. single_row.xml<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent" android:layout_height="match_parent"><LinearLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal" > <LinearLayout android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"android:layout_weight="2"><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceLarge"android:text="Large Text"android:id="@+id/textFname"android:layout_alignParentTop="true"android:layout_alignParentStart="true" /> <TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceLarge"android:text="Large Text"android:id="@+id/textLname"android:layout_below="@+id/textView"android:layout_alignParentStart="true" /></LinearLayout> <LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="Delete"android:id="@+id/rowDel"/><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="Edit" android:id="@+id/rowEdit"/> </LinearLayout></LinearLayout> </RelativeLayout>5. viewalllay.xml<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/listView"android:layout_alignParentTop="true"android:layout_alignParentStart="true" /> </RelativeLayout>6. edit_lay.xml<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"> <EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/fChname"android:layout_alignParentTop="true"android:layout_alignParentStart="true"android:layout_marginTop="58dp"android:hint="First Name" android:layout_alignParentEnd="true" /> <EditText android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/lChname"android:hint="Last Name"android:layout_below="@+id/fChname"android:layout_alignParentStart="true"android:layout_marginTop="60dp"android:layout_alignParentEnd="true" /> <Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Change"android:id="@+id/chButton"android:layout_below="@+id/lChname"android:layout_centerHorizontal="true"android:layout_marginTop="77dp" /> </RelativeLayout>
1. MainActivity.java is as
package com.exam.ravi.customlistwithbutton; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void logUser(View view) { startActivity(new Intent(this, Login.class)); } public void registerUser(View view) { startActivity(new Intent(this,RegUser.class)); } public void viewAll(View view) { startActivity(new Intent(this,ViewAll.class)); } }
2. Login.java
package com.exam.ravi.customlistwithbutton; import android.database.Cursor; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class Login extends AppCompatActivity { EditText EdLogUser, EdLogPass; String user, pass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.loglay); EdLogUser = (EditText) findViewById(R.id.logUser); EdLogPass = (EditText) findViewById(R.id.logPass); } public void loginUser(View view) { user = EdLogUser.getText().toString(); pass = EdLogPass.getText().toString(); MyHelp myHelp = new MyHelp(this); Cursor crob = myHelp.getInfo(myHelp); crob.moveToFirst(); boolean logstatus = false; String NAME = ""; do { if ((user.equals(crob.getString(0))) && (pass.equals(crob.getString(1)))){ logstatus = true; NAME = crob.getString(0); } } while (crob.moveToNext()); if (logstatus) { Toast.makeText(getBaseContext(), "Login Success .. . \n Welcome " + NAME,Toast.LENGTH_LONG).show(); finish(); } else { Toast.makeText(getBaseContext(), "Login Failed \n Please Check your detail",Toast.LENGTH_LONG).show(); finish(); } } }
3. RegUser.javapackage com.exam.ravi.customlistwithbutton; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class RegUser extends AppCompatActivity { EditText EdFname,EdLname,EdPass; String sFname,sLname,sPass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.reguser); EdFname = (EditText) findViewById(R.id.fName); EdLname = (EditText) findViewById(R.id.lName); EdPass = (EditText) findViewById(R.id.userPass); } public void regMe(View view) { sFname = EdFname.getText().toString(); sLname = EdLname.getText().toString(); sPass = EdPass.getText().toString(); MyHelp myob = new MyHelp(this); myob.putInfo(myob, sFname, sLname,sPass); Toast.makeText(RegUser.this, "Registered Successfully....",Toast.LENGTH_SHORT).show(); finish(); } }
4. ViewAll.javapackage com.exam.ravi.customlistwithbutton; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ListView; import java.util.ArrayList; public class ViewAll extends AppCompatActivity { ArrayList<DisInfo> showData; MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.viewalllay); MyHelp myHelper = new MyHelp(this); showData= myHelper.getAllRows(myHelper); adapter= new MyAdapter(this,R.layout.single_row,showData); ListView listView = (ListView) findViewById(R.id.listView); listView.setAdapter(adapter); } }5. DisInfo.javapackage com.exam.ravi.customlistwithbutton; /** * Created by Ravi on 4/28/2016. */public class DisInfo { String dis_fname; String dis_lname; String dis_pass; public String getDis_fname() { return dis_fname; } public void setDis_fname(String dis_fname) { this.dis_fname = dis_fname; } public String getDis_lname() { return dis_lname; } public void setDis_lname(String dis_lname) { this.dis_lname = dis_lname; } public String getDis_pass() { return dis_pass; } public void setDis_pass(String dis_pass) { this.dis_pass = dis_pass; } }6. Edit_Act.javapackage com.exam.ravi.customlistwithbutton; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; public class Edit_Act extends AppCompatActivity { Context context =this; EditText EdFName,EdLName,EdPass; String getFName,getLName,pass; Button change; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.edit_lay); EdFName = (EditText) findViewById(R.id.fChname); EdLName = (EditText) findViewById(R.id.lChname); Intent iget = getIntent(); getFName = iget.getStringExtra("tit"); getLName = iget.getStringExtra("des"); pass = iget.getStringExtra("pass"); EdFName.setText(getFName); EdLName.setText(getLName); change = (Button) findViewById(R.id.chButton); change.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MyHelp myob = new MyHelp(context); myob.updateInfo(myob,pass,EdFName.getText().toString(),EdLName.getText().toString()); Intent intent = new Intent(Edit_Act.this, ViewAll.class); startActivity(intent); finish(); } }); } }7. MyAdapter.javapackage com.exam.ravi.customlistwithbutton; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import java.io.ByteArrayInputStream; import java.util.ArrayList; public class MyAdapter extends ArrayAdapter<DisInfo> { Context context; String ret_fname,ret_lname,ret_pass; String newUser; int layoutResourceId; ArrayList<DisInfo> data = new ArrayList<DisInfo>(); public MyAdapter(Context context, int layoutResourceId, ArrayList<DisInfo> data){ super(context, layoutResourceId, data); this.layoutResourceId = layoutResourceId; this.context = context; this.data = data; } @Overridepublic View getView(final int position, View convertView, ViewGroup parent) { View row = convertView; Holder holder = null; if(row == null) { LayoutInflater inflater = ((Activity)context).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false); holder = new Holder(); holder.txtTitle = (TextView)row.findViewById(R.id.textFname); holder.txtDesc = (TextView)row.findViewById(R.id.textLname); holder.butDel = (Button) row.findViewById(R.id.rowDel); holder.butEdit = (Button) row.findViewById(R.id.rowEdit); row.setTag(holder); } else { holder = (Holder)row.getTag(); } DisInfo picture = data.get(position); ret_fname = picture.dis_fname; ret_lname = picture.dis_lname; ret_pass = picture.dis_pass; holder.txtTitle.setText(ret_fname); holder.txtDesc.setText(ret_lname); holder.butDel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { DisInfo picture = data.get(position); ret_fname = picture.dis_fname; ret_pass = picture.dis_pass; MyHelp myob = new MyHelp(context); myob.deleteUser(myob, ret_fname, ret_pass); Intent intent = new Intent(context, MainActivity.class); context.startActivity(intent); } }); holder.butEdit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent iob = new Intent(context,Edit_Act.class); DisInfo picture = data.get(position); ret_fname = picture.dis_fname; ret_lname = picture.dis_lname; ret_pass = picture.dis_pass; iob.putExtra("tit", ret_fname); iob.putExtra("des",ret_lname); iob.putExtra("pass",ret_pass); context.startActivity(iob); } }); return row; } static class Holder { TextView txtTitle; TextView txtDesc; Button butDel,butEdit; } }8. MyHelp.javapackage com.exam.ravi.customlistwithbutton; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import android.widget.Toast; import java.util.ArrayList; public class MyHelp extends SQLiteOpenHelper { public static final String KEY_ID="_id"; private static final String DB_NAME="cont.db"; private static final String TAB_NAME="info"; private static final int DB_VER=1; private static final String CREATE_QUERY = "create table " + TAB_NAME + " ( " +KEY_ID +" integer primary key autoincrement, " + UserInfo.F_NAME + " text not null, " + UserInfo.L_NAME + " text not null, " +UserInfo.PASS + " text not null);"; Context ctx; public MyHelp(Context context) { super(context,DB_NAME,null,DB_VER); ctx=context; } @Override public void onCreate(SQLiteDatabase sdb) { sdb.execSQL(CREATE_QUERY); Log.d("rrrr", "oncreate"); } @Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ db.execSQL("DROP TABLE IF EXISTS " + TAB_NAME); Log.d("rrrr", "onupgrade"); onCreate(db); } public void putInfo(MyHelp mob,String fname,String lname,String pass) { SQLiteDatabase SQ= mob.getWritableDatabase(); ContentValues CV=new ContentValues(); CV.put(UserInfo.F_NAME, fname); CV.put(UserInfo.L_NAME, lname); CV.put(UserInfo.PASS, pass); SQ.insert(TAB_NAME, null, CV); } public Cursor getInfo(MyHelp mob) { SQLiteDatabase SQ= mob.getWritableDatabase(); String cols[]={UserInfo.F_NAME,UserInfo.PASS}; Cursor CR= SQ.query(TAB_NAME,cols,null,null,null,null,null); return CR; } public void deleteUser(MyHelp mob,String username,String userpass) { String selection = UserInfo.F_NAME + " LIKE ? AND " + UserInfo.PASS + " LIKE ?"; String args[]={username,userpass}; SQLiteDatabase SQ = mob.getWritableDatabase(); SQ.delete(TAB_NAME,selection,args); } public void updateInfo(MyHelp mob,String pass,String newuserFname,String newuserLname) { SQLiteDatabase SQ = mob.getWritableDatabase(); String selection = UserInfo.PASS + " LIKE ? "; String args[]={pass}; ContentValues CV = new ContentValues(); CV.put(UserInfo.F_NAME,newuserFname); CV.put(UserInfo.L_NAME,newuserLname); SQ.update(TAB_NAME, CV, selection, args); } public ArrayList<DisInfo> getAllRows(MyHelp mob) { ArrayList<DisInfo> data = new ArrayList<DisInfo>(); SQLiteDatabase SQ = mob.getReadableDatabase(); String col[] = {UserInfo.F_NAME,UserInfo.L_NAME,UserInfo.PASS}; Cursor cob = SQ.query(TAB_NAME,col,null,null,null,null,null); if(cob!=null) { cob.moveToFirst(); do { DisInfo displayData = new DisInfo(); displayData.setDis_fname(cob.getString(0)); displayData.setDis_lname(cob.getString(1)); displayData.setDis_pass(cob.getString(2)); data.add(displayData); }while(cob.moveToNext()); } Toast.makeText(ctx, "Retrieved", Toast.LENGTH_LONG).show(); return data; }}
9. UserInfo.javapackage com.exam.ravi.customlistwithbutton; public class UserInfo { public static final String F_NAME = "f_name"; public static final String L_NAME = "l_name"; public static final String PASS = "pass";}
Great Example Sir, I've been looking for this everywhere Now i can add more functionality into my project <3 :)
ReplyDeleteThanks Vipul..... I know what my students want........ Thanks.....
ReplyDeleteThanks for all your information.Website is very nice and informative content.
ReplyDeletePersonalised Phone Covers
Thanks Martinez for your valuable feedback...........
ReplyDelete