1 ) First Activity XML
<?xml version="1.0" encoding="utf-8"?>
<?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.example.lenovo.task_of_test.FirstActivity"> <ListView android:layout_width="match_parent" android:layout_height="420dp" android:id="@+id/list"></ListView> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnadd" android:text="Add Family Member" android:textSize="16dp" android:textColor="#ffffff" android:textStyle="bold" android:background="@color/colorPrimary" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="10dp" /> </RelativeLayout>
2 ) Activity Second XML
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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.example.lenovo.task_of_test.SecondActivity" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add Family Member" android:textSize="25dp" android:textStyle="bold" android:textColor="@color/colorPrimaryDark" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:layout_marginLeft="75dp" android:shadowColor="@color/colorPrimary" android:shadowDx="1" android:shadowDy="1" android:shadowRadius="2"/> <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content"> <TableLayout android:id="@+id/tableLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:stretchColumns="1"> <TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="First Name :->"> </TextView> <EditText android:id="@+id/etFName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp"> <requestFocus> </requestFocus> </EditText> </TableRow> <TableRow android:id="@+id/tableRow3" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/textView3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Mobile No :->"> </TextView> <EditText android:id="@+id/etNo" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp"> </EditText> </TableRow> <TableRow android:id="@+id/tableRow6" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/textView6" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Address"> </TextView> <EditText android:id="@+id/etAddress" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp"> </EditText> </TableRow> <TableRow android:id="@+id/tableRow7" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp"> <TextView android:id="@+id/textView7" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Select Education :->" android:layout_marginTop="17dp"> </TextView> <Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/education" android:hint="Select Education"> </Spinner> </TableRow> <TableRow android:id="@+id/tableRow8" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp"> <TextView android:id="@+id/textView8" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Select Education :->" android:layout_marginTop="17dp"> </TextView> <Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/relation" android:hint="Select Relation"> </Spinner> </TableRow> </TableLayout> </ScrollView> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="160dp" android:layout_height="60dp" android:id="@+id/add" android:text="Save" android:textStyle="bold" android:textColor="@android:color/white" android:background="@drawable/button_design" android:layout_margin="16dp" android:layout_weight="1.69" /> <Button android:layout_width="160dp" android:layout_height="60dp" android:id="@+id/display" android:text="Display" android:textStyle="bold" android:textColor="@android:color/white" android:background="@drawable/button_design" android:layout_margin="16dp" android:layout_weight="1.69" /> </LinearLayout> </LinearLayout>3 ) Activity Update XML<?xml version="1.0" encoding="utf-8"?><LinearLayout 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.example.lenovo.task_of_test.ActivityUpdate" android:orientation="vertical"> <TextView android:id="@+id/txtid" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/textview1" android:layout_centerHorizontal="true" android:ems="10" android:hint="id Show Here" android:gravity="center" android:layout_marginTop="20dp" android:textSize="20sp"/> <EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/textview1" android:layout_centerHorizontal="true" android:ems="10" android:hint="Name Show Here" android:gravity="center" android:layout_marginTop="20dp" /> <EditText android:id="@+id/editText2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/editText1" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:ems="10" android:inputType="phone" android:hint="Phone Number Show Here" android:gravity="center" /> <EditText android:id="@+id/editText3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/editText2" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:ems="10" android:hint="Address Show Here" android:gravity="center" /> <EditText android:id="@+id/editText4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/editText2" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:ems="10" android:hint="Education Show Here" android:gravity="center" /> <EditText android:id="@+id/editText5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/editText2" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:ems="10" android:hint="Religion Show Here" android:gravity="center" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="25dp"> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="59dp" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Show Next Record" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="59dp" android:layout_below="@+id/button1" android:layout_centerHorizontal="true" android:text="Show Previous Record" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button3" android:layout_width="158dp" android:layout_height="wrap_content" android:layout_below="@+id/button2" android:layout_centerHorizontal="true" android:text="Update Record" /> <Button android:id="@+id/button4" android:layout_width="208dp" android:layout_height="wrap_content" android:layout_below="@+id/button3" android:layout_centerHorizontal="true" android:text="Delete Record" /> </LinearLayout> </LinearLayout>4 ) RAW XML<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:text="First Name :-> " android:textColor="@android:color/holo_blue_dark"/> <TextView android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="2dip" android:paddingTop="6dip" android:textColor="@android:color/holo_blue_dark" android:textSize="16sp" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="200dp" android:layout_height="wrap_content" android:text="Mobile Number :-> " android:textColor="#FFFF3F93"/> <TextView android:id="@+id/number" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="2dip" android:textColor="#FFFF3F93" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:text="Address :-> " android:textColor="@android:color/holo_green_light"/> <TextView android:id="@+id/address" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_green_light" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:text="Education :-> " android:textColor="@android:color/holo_purple"/> <TextView android:id="@+id/education" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_purple" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:text="Relation :-> " android:textColor="#5d5d5d"/> <TextView android:id="@+id/relation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#5d5d5d" android:textStyle="bold" /> </LinearLayout> </LinearLayout>5 ) First Activitypublic class FirstActivity extends AppCompatActivity { Button Add; ListView listView; ArrayList<member> memberArrayList; private static int REQUESTCODE_ADDIITION = 11,REQUESTCODE_UPDATION=12,REQUESTCODE_DELETE=13; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_first); Add=(Button)findViewById(R.id.btnadd); listView=(ListView)findViewById(R.id.list); final DatabaseHelper databaseHelper = new DatabaseHelper(FirstActivity.this); memberArrayList=databaseHelper.getAllContact(); MemberAdapter memberAdapter= new MemberAdapter(FirstActivity.this,memberArrayList); listView.setAdapter(memberAdapter); Add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(getApplicationContext(),SecondActivity.class); startActivityForResult(intent,REQUESTCODE_ADDIITION); } }); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent=new Intent(getApplicationContext(),ActivityUpdate.class); startActivityForResult(intent,REQUESTCODE_UPDATION); int item=position; } }); } protected void onActivityResult(int requestCode,int resultCode,Intent intent){ super.onActivityResult(requestCode, resultCode, intent); if(requestCode == REQUESTCODE_ADDIITION) { if(resultCode ==RESULT_OK) { if(intent!=null) { final DatabaseHelper databaseHelper = new DatabaseHelper(FirstActivity.this); memberArrayList=databaseHelper.getAllContact(); MemberAdapter memberAdapter= new MemberAdapter(FirstActivity.this,memberArrayList); listView.setAdapter(memberAdapter); } } } if(requestCode == REQUESTCODE_UPDATION) { if(resultCode ==RESULT_OK) { if(intent!=null) { final DatabaseHelper databaseHelper = new DatabaseHelper(FirstActivity.this); memberArrayList=intent.getParcelableArrayListExtra("list"); MemberAdapter memberAdapter= new MemberAdapter(FirstActivity.this,memberArrayList); listView.setAdapter(memberAdapter); } } } if(requestCode == REQUESTCODE_DELETE) { if(resultCode ==RESULT_OK) { if(intent!=null) { memberArrayList=intent.getParcelableArrayListExtra("list"); MemberAdapter memberAdapter= new MemberAdapter(FirstActivity.this,memberArrayList); listView.setAdapter(memberAdapter); } } } } }6 ) Model Classpublic class member implements Parcelable{ Integer id; String fname, number, address, education, relation; public static final Creator<member> CREATOR = new Creator<member>() { @Override public member createFromParcel(Parcel in) { return new member(in); } @Override public member[] newArray(int size) { return new member[size]; } }; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getFname() { return fname; } public void setFname(String fname) { this.fname = fname; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getEducation() { return education; } public void setEducation(String education) { this.education = education; } public String getRelation() { return relation; } public void setRelation(String relation) { this.relation = relation; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(this.fname); dest.writeString(this.number); dest.writeString(this.address); dest.writeString(this.education); dest.writeString(this.relation); } public member() { } @Override public int describeContents() { return 0; } protected member(Parcel in) { this.fname = in.readString(); this.number = in.readString(); this.address = in.readString(); this.education = in.readString(); this.relation = in.readString(); } }7 ) Adapter Classpublic class MemberAdapter extends BaseAdapter { private Activity activity; private List contactList; private static LayoutInflater inflater = null; // public Resources res; member tempValues = null; int i = 0; public MemberAdapter(Activity activity, ArrayList contactList) { /********** Take passed values **********/ this.activity = activity; this.contactList = contactList; inflater = (LayoutInflater) activity. getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return contactList.size(); } @Override public member getItem(int position) { return (member) contactList.get(position); } @Override public long getItemId(int position) { return position; } public static class ViewHolder { public TextView name; public TextView number; public TextView address; public TextView education; public TextView relation; } @Override public View getView(int position, View convertView, ViewGroup parent) { View vi = convertView; ViewHolder holder; if (convertView == null) { vi = inflater.inflate(R.layout.raw, null); holder = new ViewHolder(); holder.name = (TextView) vi.findViewById(R.id.name); holder.number = (TextView) vi.findViewById(R.id.number); holder.address = (TextView) vi.findViewById(R.id.address); holder.education = (TextView) vi.findViewById(R.id.education); holder.relation = (TextView) vi.findViewById(R.id.relation); vi.setTag(holder); } else holder = (ViewHolder) vi.getTag(); if (contactList.size() <= 0) { holder.name.setText("No Data"); } else { /***** Get each Model object from Arraylist ********/ tempValues = null; tempValues = (member) contactList.get(position); holder.name.setText(tempValues.getFname()); holder.number.setText(tempValues.getNumber()); holder.address.setText(tempValues.getAddress()); holder.education.setText(tempValues.getEducation()); holder.relation.setText(tempValues.getRelation()); } return vi; } }8 ) Second Activitypublic class SecondActivity extends AppCompatActivity { Button save; Spinner sp1,sp2; public EditText e1,e2,e3; ArrayList<member> memberArrayList; String[] Education = { "M.C.A", "M.B.A", "MSC.IT", "BTEC", }; String[] Relation = { "SELF", "FATHER", "MOTHER", "BROTHER", "SISTER" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); e1=(EditText)findViewById(R.id.etFName); e2=(EditText)findViewById(R.id.etNo); e3=(EditText)findViewById(R.id.etAddress); sp1=(Spinner)findViewById(R.id.education); sp2=(Spinner)findViewById(R.id.relation); save=(Button)findViewById(R.id.add) ; memberArrayList=new ArrayList<>(); final DatabaseHelper databaseHelper = new DatabaseHelper(SecondActivity.this); ArrayAdapter<String> adapter= new ArrayAdapter<String>(this,android. R.layout.simple_spinner_dropdown_item ,Education); ArrayAdapter<String> adapter1= new ArrayAdapter<String>(this,android. R.layout.simple_spinner_dropdown_item ,Relation); sp1.setAdapter(adapter); sp2.setAdapter(adapter1); save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name=e1.getText().toString(); String number=e2.getText().toString(); String address=e3.getText().toString(); String education=sp1.getSelectedItem().toString(); String relation=sp2.getSelectedItem().toString(); member member=new member(); member.setFname(name); member.setNumber(number); member.setAddress(address); member.setEducation(education); member.setRelation(relation); memberArrayList.add(member); Toast.makeText(getApplicationContext(),"Data Saved",Toast.LENGTH_SHORT).show(); e1.setText(""); e2.setText(""); e3.setText(""); databaseHelper.insert(member); Intent intent=new Intent(SecondActivity.this,FirstActivity.class); // intent.putExtra("list",databaseHelper.getAllContact()); setResult(RESULT_OK,intent); finish(); } }); } }9 ) Activity Updatepublic class ActivityUpdate extends AppCompatActivity { TextView idt; EditText fnm,mno,add,edu,rel; Button btnnxt,btnprs,btnupdate,btndelete; String GetSQliteQuery, UpdateRecordQuery, DeleteQuery ; SQLiteDatabase SQLITEDATABASE; Cursor cursor; ArrayList<member> memberArrayList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_update); idt=(TextView)findViewById(R.id.txtid); fnm=(EditText)findViewById(R.id.editText1); mno=(EditText)findViewById(R.id.editText2); add=(EditText)findViewById(R.id.editText3); edu=(EditText)findViewById(R.id.editText4); rel=(EditText)findViewById(R.id.editText5); btnnxt=(Button)findViewById(R.id.button1); btnprs=(Button)findViewById(R.id.button2); btnupdate=(Button)findViewById(R.id.button3); btndelete=(Button)findViewById(R.id.button4); memberArrayList=new ArrayList<>(); GetSQliteQuery=("SELECT * FROM Memberlist"); SQLITEDATABASE=openOrCreateDatabase("Member", Context.MODE_PRIVATE,null); cursor=SQLITEDATABASE.rawQuery(GetSQliteQuery, null); cursor.moveToFirst(); GetSQLiteDatabaseRecords(); final DatabaseHelper databaseHelper = new DatabaseHelper(ActivityUpdate.this); btnnxt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!cursor.isLast()) { cursor.moveToNext(); } GetSQLiteDatabaseRecords(); } }); btnprs.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!cursor.isFirst()) { cursor.moveToPrevious(); } GetSQLiteDatabaseRecords(); } }); btnupdate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String id=idt.getText().toString(); String name=fnm.getText().toString(); String number=mno.getText().toString(); String address=add.getText().toString(); String education=edu.getText().toString(); String relation=rel.getText().toString(); member member=new member(); member.setId(Integer.parseInt(id)); member.setFname(name); member.setNumber(number); member.setAddress(address); member.setEducation(education); member.setRelation(relation); memberArrayList.add(member); databaseHelper.updateContact(member); Toast.makeText(getApplicationContext(),"Data Updated",Toast.LENGTH_SHORT).show(); fnm.setText(""); mno.setText(""); add.setText(""); edu.setText(""); rel.setText(""); Intent intent=new Intent(ActivityUpdate.this,FirstActivity.class); intent.putExtra("list",databaseHelper.getAllContact()); setResult(RESULT_OK,intent); finish(); } }); btndelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String id=idt.getText().toString(); String name=fnm.getText().toString(); String number=mno.getText().toString(); String address=add.getText().toString(); String education=edu.getText().toString(); String relation=rel.getText().toString(); member member=new member(); member.setId(Integer.parseInt(id)); member.setFname(name); member.setNumber(number); member.setAddress(address); member.setEducation(education); member.setRelation(relation); memberArrayList.add(member); Toast.makeText(getApplicationContext(),"Data Updated",Toast.LENGTH_SHORT).show(); fnm.setText(""); mno.setText(""); add.setText(""); edu.setText(""); rel.setText(""); final DatabaseHelper databaseHelper = new DatabaseHelper(ActivityUpdate.this); databaseHelper.deleteContact(member); Intent intent=new Intent(ActivityUpdate.this,FirstActivity.class); intent.putExtra("list",databaseHelper.getAllContact()); setResult(RESULT_OK,intent); finish(); } }); } public void GetSQLiteDatabaseRecords() { idt.setText(cursor.getString(0).toString()); fnm.setText(cursor.getString(1).toString()); mno.setText(cursor.getString(2).toString()); add.setText(cursor.getString(3).toString()); edu.setText(cursor.getString(4).toString()); rel.setText(cursor.getString(5).toString()); } }Notes :1 ) Require Graddlecompile 'com.android.support:appcompat-v7:25.1.1'compile 'com.android.support:design:25.1.1'compile 'com.android.support:support-v4:25.1.1'
Comments
Post a Comment