title: How to store and fetch list view data from sqlite in android?
body: <p>I have created a simple demo in which I have simply stored the two value that is first name and last name  in sqlite and display that in list view.But when I tried two add more value like phone no.address,state,country etc . I got error.and it display the message like could not read the row and column data.
  please help me two add 9 or 10 value and display in list view.here is my code.</p>
  
  <pre><code>AddActivity.Java
  
  public class AddActivity extends Activity implements OnClickListener {
  private Button btn_save;
  private EditText edit_first,edit_last,phoneNo;
  private DbHelper mHelper;
  private SQLiteDatabase dataBase;
  private String id,fname,lname,pno;
  private boolean isUpdate;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.add);
  
          btn_save=(Button)findViewById(R.id.save_btn);
          edit_first=(EditText)findViewById(R.id.frst_editTxt);
          edit_last=(EditText)findViewById(R.id.last_editTxt);
          phoneNo=(EditText)findViewById(R.id.phoneNo);
  
         isUpdate=getIntent().getExtras().getBoolean("update");
          if(isUpdate)
          {
              id=getIntent().getExtras().getString("ID");
              fname=getIntent().getExtras().getString("Fname");
              lname=getIntent().getExtras().getString("Lname");
              pno=getIntent().getExtras().getString("Pno");
  
              edit_first.setText(fname);
              edit_last.setText(lname);
              phoneNo.setText(pno);
  
          }
  
           btn_save.setOnClickListener(this);
  
           mHelper=new DbHelper(this);
  
      }
  
  
      public void onClick(View v) {
          fname=edit_first.getText().toString().trim();
          lname=edit_last.getText().toString().trim();
          pno=phoneNo.getText().toString().trim();
          if(fname.length()&gt;0 &amp;&amp; lname.length()&gt;0)
          {
              saveData();
          }
          else
          {
              AlertDialog.Builder alertBuilder=new AlertDialog.Builder(AddActivity.this);
              alertBuilder.setTitle("Invalid Data");
              alertBuilder.setMessage("Please, Enter valid data");
              alertBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  
                  public void onClick(DialogInterface dialog, int which) {
                         dialog.cancel();
  
                  }
              });
              alertBuilder.create().show();
          }
  
      }
  
  
      private void saveData(){
          dataBase=mHelper.getWritableDatabase();
          ContentValues values=new ContentValues();
  
          values.put(DbHelper.KEY_FNAME,fname);
          values.put(DbHelper.KEY_LNAME,lname );
          values.put(DbHelper.KEY_PHONENO,pno );
  
          System.out.println("");
          if(isUpdate)
          {    
  
              dataBase.update(DbHelper.TABLE_NAME, values, DbHelper.KEY_ID+"="+id, null);
          }
          else
          {
  
              dataBase.insert(DbHelper.TABLE_NAME, null, values);
          }
  
          dataBase.close();
          finish();
  
  
      }
  
  }
  </code></pre>
  
  <p>DisplayAdapter.java</p>
  
  <pre><code>public class DisplayAdapter extends BaseAdapter {
      private Context mContext;
      private ArrayList&lt;String&gt; id;
      private ArrayList&lt;String&gt; firstName;
      private ArrayList&lt;String&gt; lastName;
      private ArrayList&lt;String&gt; phoneNo;
  
  
      public DisplayAdapter(Context c, ArrayList&lt;String&gt; id,ArrayList&lt;String&gt; fname, ArrayList&lt;String&gt; lname,ArrayList&lt;String&gt; pno) {
          this.mContext = c;
  
          this.id = id;
          this.firstName = fname;
          this.lastName = lname;
          this.phoneNo = pno;
      }
  
  
  
      public int getCount() {
          // TODO Auto-generated method stub
          return id.size();
      }
  
      public Object getItem(int position) {
          // TODO Auto-generated method stub
          return null;
      }
  
      public long getItemId(int position) {
          // TODO Auto-generated method stub
          return 0;
      }
  
      public View getView(int pos, View child, ViewGroup parent) {
          Holder mHolder;
          LayoutInflater layoutInflater;
          if (child == null) {
              layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              child = layoutInflater.inflate(R.layout.listcell, null);
              mHolder = new Holder();
              mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id);
              mHolder.txt_fName = (TextView) child.findViewById(R.id.txt_fName);
              mHolder.txt_lName = (TextView) child.findViewById(R.id.txt_lName);
              mHolder.phoneTextView = (TextView) child.findViewById(R.id.phoneTextView);
              child.setTag(mHolder);
          } else {
              mHolder = (Holder) child.getTag();
          }
          mHolder.txt_id.setText(id.get(pos));
          mHolder.txt_fName.setText(firstName.get(pos));
          mHolder.txt_lName.setText(lastName.get(pos));
          mHolder.phoneTextView.setText(phoneNo.get(pos));
  
          return child;
      }
  
      public class Holder {
          TextView txt_id;
          TextView txt_fName;
          TextView txt_lName;
          TextView phoneTextView;
      }
  
  }
  </code></pre>
  
  <p>Database Helper.java</p>
  
  <pre><code>public class DbHelper extends SQLiteOpenHelper {
      static String DATABASE_NAME="userdata";
      public static final String TABLE_NAME="user";
      public static final String KEY_FNAME="fname";
      public static final String KEY_LNAME="lname";
      public static final String KEY_PHONENO="pno";
      public static final String KEY_ID="id";
      public DbHelper(Context context) {
          super(context, DATABASE_NAME, null, 1);
  
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) {
          String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+" ("+KEY_ID+" INTEGER PRIMARY KEY, "+KEY_FNAME+" TEXT, "+KEY_LNAME+" TEXT, "+KEY_PHONENO+" TEXT)";
          db.execSQL(CREATE_TABLE);
  
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
          onCreate(db);
  
      }
  
  }
  </code></pre>
  
  <p>DisplayActivity.java</p>
  
  <pre><code>public class DisplayActivity extends Activity {
  
      private DbHelper mHelper;
      private SQLiteDatabase dataBase;
  
      private ArrayList&lt;String&gt; userId = new ArrayList&lt;String&gt;();
      private ArrayList&lt;String&gt; user_fName = new ArrayList&lt;String&gt;();
      private ArrayList&lt;String&gt; user_lName = new ArrayList&lt;String&gt;();
      private ArrayList&lt;String&gt; user_phoneNo = new ArrayList&lt;String&gt;();
  
      private ListView userList;
      private AlertDialog.Builder build;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.display_activity);
  
          userList = (ListView) findViewById(R.id.List);
  
          mHelper = new DbHelper(this);
  
          //add new record
          findViewById(R.id.btnAdd).setOnClickListener(new OnClickListener() {
  
              public void onClick(View v) {
  
                  Intent i = new Intent(getApplicationContext(), AddActivity.class);
                  i.putExtra("update", false);
                  startActivity(i);
  
              }
          });
  
          //click to update data
          userList.setOnItemClickListener(new OnItemClickListener() {
  
              public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) {
  
                  Intent i = new Intent(getApplicationContext(), AddActivity.class);
                  i.putExtra("Fname", user_fName.get(arg2));
                  i.putExtra("Lname", user_lName.get(arg2));
                  i.putExtra("Lname", user_phoneNo.get(arg2));
                  i.putExtra("ID", userId.get(arg2));
                  i.putExtra("update", true);
                  startActivity(i);
  
              }
          });
  
          //long click to delete data
          userList.setOnItemLongClickListener(new OnItemLongClickListener() {
  
              public boolean onItemLongClick(AdapterView&lt;?&gt; arg0, View arg1, final int arg2, long arg3) {
  
                  build = new AlertDialog.Builder(DisplayActivity.this);
                  build.setTitle("Delete " + user_fName.get(arg2) + " " + user_lName.get(arg2));
                  build.setMessage("Do you want to delete ?");
                  build.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
  
                              public void onClick(DialogInterface dialog, int which) {
  
                                  Toast.makeText( getApplicationContext(),
                                          user_fName.get(arg2) + " "
                                                  + user_lName.get(arg2)
                                                  +user_phoneNo.get(arg2)
                                                  + " is deleted.", 3000).show();
  
                                  dataBase.delete(
                                          DbHelper.TABLE_NAME,
                                          DbHelper.KEY_ID + "="
                                                  + userId.get(arg2), null);
                                  displayData();
                                  dialog.cancel();
                              }
                          });
  
                  build.setNegativeButton("No", new DialogInterface.OnClickListener() {
  
                              public void onClick(DialogInterface dialog, int which) {
                                      dialog.cancel();
                              }
                          });
                  AlertDialog alert = build.create();
                  alert.show();
  
                  return true;
              }
          });
      }
  
      @Override
      protected void onResume() {
          displayData();
          super.onResume();
      }
  
      /**
       * displays data from SQLite
       */
      private void displayData() {
          dataBase = mHelper.getWritableDatabase();
          Cursor mCursor = dataBase.rawQuery("SELECT * FROM " + DbHelper.TABLE_NAME, null);
  
          userId.clear();
          user_fName.clear();
          user_lName.clear();
          user_phoneNo.clear();
          if (mCursor.moveToFirst()) {
              do {
                  userId.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ID)));
                  user_fName.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_FNAME)));
                  user_lName.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_LNAME)));
                  user_phoneNo.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_PHONENO)));
  
              } while (mCursor.moveToNext());
          }
          DisplayAdapter disadpt = new DisplayAdapter(DisplayActivity.this,userId, user_fName, user_lName,user_phoneNo);
          userList.setAdapter(disadpt);
          mCursor.close();
      }
  
  
  
  }
  </code></pre>