ScreenShots:-
Step 1:- main_activity.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:orientation="vertical"
android:gravity="center_vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:id="@+id/edtName"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone"
android:id="@+id/edtPhone"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Gender"
android:textSize="25sp"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rdGroup"
android:orientation="horizontal">
<RadioButton
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/rdMale"
android:text="Male"/>
<RadioButton
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/rdFemale"
android:text="FeMale"/>
</RadioGroup>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Insert"
android:id="@+id/btnInsert"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View"
android:id="@+id/btnView"/>
</LinearLayout>
Step 2:- MainActivity.java
Step 1:- main_activity.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:orientation="vertical"
android:gravity="center_vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:id="@+id/edtName"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone"
android:id="@+id/edtPhone"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Gender"
android:textSize="25sp"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rdGroup"
android:orientation="horizontal">
<RadioButton
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/rdMale"
android:text="Male"/>
<RadioButton
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/rdFemale"
android:text="FeMale"/>
</RadioGroup>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Insert"
android:id="@+id/btnInsert"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View"
android:id="@+id/btnView"/>
</LinearLayout>
Step 2:- MainActivity.java
package androidhubb.sqliteexample; import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; public class MainActivity extends AppCompatActivity { EditText edtname,edtNumber; Button btnInsert,btnView; RadioButton rdMale,rdFemale; RadioGroup rdGroup; SQLiteDatabase db; String gender=""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edtname=(EditText)findViewById(R.id.edtName); edtNumber=(EditText)findViewById(R.id.edtPhone); btnInsert=(Button) findViewById(R.id.btnInsert); btnView=(Button) findViewById(R.id.btnView); rdGroup=(RadioGroup) findViewById(R.id.rdGroup); rdMale=(RadioButton) findViewById(R.id.rdMale); rdFemale=(RadioButton) findViewById(R.id.rdFemale); //database db=openOrCreateDatabase("MyData3", Context.MODE_PRIVATE,null); db.execSQL("create table if not exists MyTable3(ID integer primary key autoincrement not null,NAME varchar,PHONE varchar,GENDER varchar)"); btnInsert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ContentValues cv=new ContentValues(); cv.put("NAME",edtname.getText().toString().trim()); cv.put("PHONE",edtNumber.getText().toString().trim()); cv.put("GENDER",gender.toString().trim()); Long i=db.insert("MyTable3",null,cv); if(i>0){ Toast.makeText(MainActivity.this,"Data inserted",Toast.LENGTH_LONG).show(); edtname.setText(""); edtNumber.setText(""); rdGroup.clearCheck(); } } }); btnView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(MainActivity.this,ListData.class)); } }); rdGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { if(i==R.id.rdMale){ gender="Male"; } else if(i==R.id.rdFemale){ gender="Female"; } } }); } }
Step 3:- listdata.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">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fab"
android:layout_gravity="right|bottom"
android:src="@android:drawable/ic_input_add"
android:layout_margin="16dp"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list1"></ListView>
</FrameLayout>
</LinearLayout>
Step 4: ListItem.java
package androidhubb.sqliteexample;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
import java.util.ArrayList;
/** * Created by Supriya on 8/31/2016. */public class ListData extends AppCompatActivity {
ListView listView;
SQLiteDatabase db;
FloatingActionButton fab;
@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listdata);
listView=(ListView)findViewById(R.id.list1);
fab=(FloatingActionButton)findViewById(R.id.fab);
db=openOrCreateDatabase("MyData3", Context.MODE_PRIVATE,null);
onViewData();
fab.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
startActivity(new Intent(ListData.this,MainActivity.class));
}
});
}
public void onViewData(){
ArrayList<String> listname=new ArrayList<String>();
ArrayList<String> listPhone=new ArrayList<String>();
ArrayList<String> listgender=new ArrayList<String>();
listname.clear();
listPhone.clear();
listgender.clear();
Cursor c=db.rawQuery("select * from MyTable3",null);
while(c.moveToNext()){
listname.add(c.getString(1));
listPhone.add(c.getString(2));
listgender.add(c.getString(3));
}
listView.setAdapter(new Adapter(ListData.this,listname,listPhone,listgender));
}
}
Step:-5 adapter.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Name"
android:id="@+id/tvName"
android:textSize="20sp"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Name"
android:id="@+id/tvphone"
android:textSize="20sp"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Name"
android:id="@+id/tvgender"
android:textSize="20sp"/>
</LinearLayout>
Step6:- AdapterList.java
package androidhubb.sqliteexample;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/** * Created by Supriya on 8/31/2016. */public class Adapter extends BaseAdapter {
ArrayList<String> n,p,g;
ListData l;
LayoutInflater inflater;
public Adapter(ListData data,ArrayList<String> name,ArrayList<String> phone,ArrayList<String> gender){
this.l=data;
this.n=name;
this.p=phone;
this.g=gender;
this.inflater=LayoutInflater.from(data);
}
@Override public int getCount() {
return n.size();
}
@Override public Object getItem(int i) {
return null;
}
@Override public long getItemId(int i) {
return 0;
}
@Override public View getView(int i, View view, ViewGroup viewGroup) {
view=inflater.inflate(R.layout.adapter,null);
TextView tvname=(TextView)view.findViewById(R.id.tvName);
TextView tvnumber=(TextView)view.findViewById(R.id.tvphone);
TextView tvgender=(TextView)view.findViewById(R.id.tvgender);
tvname.setText(n.get(i));
tvnumber.setText(p.get(i));
tvgender.setText(g.get(i));
return view;
}
}
step 7;- For Floating Actionbar we have to add following in build.gradl
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:24.1.1' compile 'com.android.support:design:24.1.1' }