I'm new in Android. When I'm using this code. My Emulator is getting "Unfortunately Myapp has stopped." In this code I'm trying to add some sort of common person's details in Sqlite and also selecting and deleting from Sqlite. This is my XML File that I'm using it.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name:"
android:id="@+id/textView" />
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Name:"
android:id="@+id/textlastname" />
<EditText
android:id="@+id/editlastname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender:"
android:id="@+id/textgender" />
<EditText
android:id="@+id/editgender"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age:"
android:id="@+id/textage" />
<EditText
android:id="@+id/editage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address:"
android:id="@+id/textaddress" />
<EditText
android:id="@+id/editaddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add New Person"
/>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
This is my MainActivity.java File that I'm using it.
package com.example.gaurang.myhomes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
class MyDbHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "mydb";
private static final int DB_VERSION = 1;
public static final String TABLE_NAME = "students";
public static final String COL_NAME = "pName";
public static final String COL_LAST = "pLast";
public static final String COL_GENDER = "pGender";
public static final String COL_AGE = "pAge";
public static final String COL_ADDRESS = "pAddress";
public static final String COL_DATE = "pDate";
private static final String STRING_CREATE = "CREATE TABLE "+TABLE_NAME+" (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+COL_NAME+" TEXT, "+COL_LAST+" TEXT, "+COL_GENDER+" TEXT, "+COL_AGE+" TEXT, "+COL_ADDRESS+" TEXT, "+COL_DATE+" DATE);";
public MyDbHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(STRING_CREATE);
ContentValues cv = new ContentValues(6);
cv.put(COL_NAME, "New Entry");
cv.put(COL_LAST, "New Entry");
cv.put(COL_GENDER, "New Entry");
cv.put(COL_AGE, "New Entry");
cv.put(COL_ADDRESS, "New Entry");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cv.put(COL_DATE, dateFormat.format(new Date()));
db.insert(TABLE_NAME, null, cv);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
}
public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
EditText mText;
EditText mLast;
EditText mGender;
EditText mAge;
EditText mAddress;
Button mAdd;
ListView mList;
MyDbHelper mHelper;
SQLiteDatabase mDb;
Cursor mCursor;
SimpleCursorAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mText = (EditText)findViewById(R.id.name);
mLast = (EditText)findViewById(R.id.editlastname);
mGender = (EditText)findViewById(R.id.editgender);
mAge = (EditText)findViewById(R.id.editage);
mAddress = (EditText)findViewById(R.id.editaddress);
mAdd = (Button)findViewById(R.id.add);
mAdd.setOnClickListener(this);
mList = (ListView)findViewById(R.id.list);
mList.setOnItemClickListener(this);
mHelper = new MyDbHelper(this);
}
@Override
public void onResume() {
super.onResume();
mDb = mHelper.getWritableDatabase();
String[] columns = new String[] {"_id", MyDbHelper.COL_NAME,MyDbHelper.COL_LAST,MyDbHelper.COL_GENDER,MyDbHelper.COL_AGE ,MyDbHelper.COL_ADDRESS, MyDbHelper.COL_DATE};
mCursor = mDb.query(MyDbHelper.TABLE_NAME, columns, null, null, null, null, null, null);
String[] headers = new String[] {MyDbHelper.COL_NAME,MyDbHelper.COL_LAST,MyDbHelper.COL_GENDER,MyDbHelper.COL_AG E,MyDbHelper.COL_ADDRESS, MyDbHelper.COL_DATE};
mAdapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item,
mCursor, headers, new int[]{android.R.id.text1, android.R.id.text2});
mList.setAdapter(mAdapter);
}
@Override
public void onPause() {
super.onPause();
mDb.close();
mCursor.close();
}
@Override
public void onClick(View v) {
ContentValues cv = new ContentValues(6);
cv.put(MyDbHelper.COL_NAME, mText.getText().toString());
cv.put(MyDbHelper.COL_LAST, mLast.getText().toString());
cv.put(MyDbHelper.COL_GENDER, mGender.getText().toString());
cv.put(MyDbHelper.COL_AGE, mAge.getText().toString());
cv.put(MyDbHelper.COL_ADDRESS, mAddress.getText().toString());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cv.put(MyDbHelper.COL_DATE, dateFormat.format(new Date())); //Insert 'now' as the date
mDb.insert(MyDbHelper.TABLE_NAME, null, cv);
mCursor.requery();
mAdapter.notifyDataSetChanged();
mText.setText(null);
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
mCursor.moveToPosition(position);
String rowId = mCursor.getString(0); //Column 0 of the cursor is the id
mDb.delete(MyDbHelper.TABLE_NAME, "_id = ?", new String[]{rowId});
mCursor.requery();
mAdapter.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}