title: to save Listview data in sqlite database
body: <p>I have to add 2 buttons with each row in ListView. The buttons are Add and Delete. When user selects one of the buttons then some actions should be taken.
  The user selects data from datepicker.I have created save button.When save button gets clicked the data inside listview along with that date must get saved in Sqlite database.How can I do it?
  Following is my code.</p>
  
  <pre><code>activity_main.xml
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent"&gt;
      &lt;ImageView
          android:layout_width="90dp"
          android:layout_height="50dp"
          android:id="@+id/calender"
          android:background="@drawable/calendar"/&gt;
  
  
      &lt;EditText
          android:layout_width="250dp"
          android:layout_height="50dp"
          android:hint="Enter Date..."
          android:layout_centerHorizontal="true"
  
          android:textColorHint="#992e28"
          android:textSize="20sp"
          android:textStyle="bold"
          android:textAlignment="center"
          android:layout_toRightOf="@+id/calender"
          android:id="@+id/date"/&gt;
  
      &lt;ListView
      android:layout_width="match_parent"
      android:layout_height="400dp"
      android:id="@+id/my_listview"&gt;&lt;/ListView&gt;
  
  
      &lt;Button
          android:layout_width="90dp"
          android:layout_height="40dp"
          android:id="@+id/save"
          android:text="Save"/&gt;
  &lt;/LinearLayout&gt;
  
  
  custom_list.xml
  &lt;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:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"&gt;
  
      &lt;TextView
      android:id="@+id/list_item_string"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerVertical="true"
      android:layout_alignParentLeft="true"
      android:paddingLeft="8dp"
      android:textSize="18sp"
      android:textStyle="bold" /&gt;
  
      &lt;Button
      android:id="@+id/delete_btn"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:layout_centerVertical="true"
      android:layout_marginRight="5dp"
      android:text="Delete" /&gt;
  
      &lt;Button
      android:id="@+id/add_btn"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_toLeftOf="@id/delete_btn"
      android:layout_centerVertical="true"
      android:layout_marginRight="10dp"
      android:text="Add" /&gt;
  &lt;/RelativeLayout&gt;
  
  MainActivity.java
  package com.example.abc.lvtodb2;
  import android.app.Activity;
  import android.app.DatePickerDialog;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Button;
  import android.widget.DatePicker;
  import android.widget.EditText;
  import android.widget.ImageView;
  import android.widget.ListView;
  
  import java.util.ArrayList;
  import java.util.Calendar;
  
  public class MainActivity extends Activity {
      ListView lView;
      Button insert;
  
      EditText date;
      ImageView cal;
  
      DatePickerDialog datePickerDialog;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          // initiate the date picker and a button
          date = (EditText) findViewById(R.id.date);
          cal = (ImageView) findViewById(R.id.calender);
          // perform click event on edit text
          cal.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  final Calendar c = Calendar.getInstance();
                  int mYear = c.get(Calendar.YEAR);//current year
                  int mMonth = c.get(Calendar.MONTH); // current month
                  int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
                  // date picker dialog
                  datePickerDialog = new DatePickerDialog(MainActivity.this,
                          new DatePickerDialog.OnDateSetListener() {
  
                              @Override
                              public void onDateSet(DatePicker view, int year,
                                                    int monthOfYear, int dayOfMonth) {
                                  // set day of month , month and year value in the edit text
                                  date.setText(dayOfMonth + "/"
                                          + (monthOfYear + 1) + "/" + year);
  
                              }
                          }, mYear, mMonth, mDay);
                  datePickerDialog.show();
              }
  
  
          });
  
  
          //generate list
          ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
          list.add("item1");
          list.add("item2");
          list.add("item3");
          list.add("item4");
          list.add("item5");
  
          insert=(Button)findViewById(R.id.save);
  
          //instantiate custom adapter
          CustomAdapter adapter = new CustomAdapter(list,MainActivity.this);
  
          //handle listview and assign adapter
           lView = (ListView)findViewById(R.id.my_listview);
          lView.setAdapter(adapter);
  
  
      }
  
  
  }
  
  
  CustomAdapter.java
  package com.example.abc.lvtodb2;
  
  import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.Button;
  import android.widget.ListAdapter;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import java.util.ArrayList;
  
  /**
   * Created by ABC on 2/14/2017.
   */
  public class CustomAdapter extends BaseAdapter implements ListAdapter {
      private ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
      private Context context;
  
      public CustomAdapter(ArrayList&lt;String&gt; list, Context context) {
          this.list = list;
          this.context = context;
      }
  
      @Override
      public int getCount() {
          return list.size();
      }
  
      @Override
      public Object getItem(int pos) {
          return list.get(pos);
      }
  
      @Override
      public long getItemId(int pos) {
          return 0;
  
      }
  
      @Override
      public View getView(final int position, View convertView, ViewGroup parent) {
          View view = convertView;
          if (view == null) {
              LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              view = inflater.inflate(R.layout.custom_list, null);
          }
  
          //Handle TextView and display string from your list
          TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
          listItemText.setText(list.get(position));
  
          //Handle buttons and add onClickListeners
          Button deleteBtn = (Button)view.findViewById(R.id.delete_btn);
          Button addBtn = (Button)view.findViewById(R.id.add_btn);
  
          deleteBtn.setOnClickListener(new View.OnClickListener(){
              @Override
              public void onClick(View v) {
                  //do something
                  list.remove(position); //or some other task
                  notifyDataSetChanged();
              }
          });
          addBtn.setOnClickListener(new View.OnClickListener(){
              @Override
              public void onClick(View v) {
                  Toast.makeText(context,"Item added",Toast.LENGTH_SHORT).show();
                  notifyDataSetChanged();
              }
          });
  
          return view;
      }
  }
  </code></pre>