no such column _id Android,Java,SQL - java

I'm pretty new to Java and Android apps, just started several weeks ago, everything went pretty well until now, when I stuck for several hours with a problem I cannot resolve, even with checking dozens of threads here.
I'm working on pretty simple android app that is supposed to show movies and years from a database, with a possibility to add record, show them all, search by a year or title. I stopped on a problem with
no such column _id (code 1): , while compiling SELECT _id, title, year FROM movies
I will be grateful for any help.
There are my files :
MainActivity
package com.example.imdbproject;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MainActivity extends Activity {
ListView moviesList;
Button searchYear;
Button searchTitle;
Button showAll;
Button addbtn;
Cursor cursor;
adapter adapter_ob;
MySQLiteHelper helper_ob;
SQLiteDatabase db_ob;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InitDataBase();
moviesList = (ListView)findViewById(R.id.listView1);
searchYear = (Button)findViewById(R.id.buttonYear);
searchTitle =(Button)findViewById(R.id.buttonTitle);
addbtn = (Button)findViewById(R.id.buttonAdd);
showAll = (Button)findViewById(R.id.buttonShowAll);
adapter_ob = new adapter(this);
String[] from = { helper_ob.KEY_TITLE, helper_ob.KEY_YEAR };
int[] to = { R.id.tv_title, R.id.tv_year };
//PROBLEM
//cursor = adapter_ob.queryName();
//PROBLEM
//SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to, 1);
/*
moviesList.setAdapter(cursorAdapter);
moviesList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
Bundle passdata = new Bundle();
Cursor listCursor = (Cursor) arg0.getItemAtPosition(arg2);
int nameId = listCursor.getInt(listCursor.getColumnIndex(helper_ob.KEY_ID));
passdata.putInt("keyid", nameId);
Intent passIntent = new Intent(MainActivity.this,EditActivity.class);
passIntent.putExtras(passdata);
startActivity(passIntent);
}
});
*/
addbtn.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Intent addsomemoviesIntent = new Intent(MainActivity.this, AddSomeMovies.class);
startActivity(addsomemoviesIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void InitDataBase() {
MySQLiteHelper sqh = new MySQLiteHelper(this);
SQLiteDatabase sqdb = sqh.getWritableDatabase();
long result = sqh.addMovie("movietitle", "year");
}
}
adapter.java
package com.example.imdbproject;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.content.ContentValues;
import android.database.Cursor;
public class adapter {
SQLiteDatabase database_ob;
MySQLiteHelper openHelper_ob;
Context context;
public adapter(Context c)
{
context = c;
}
public adapter openToRead()
{
openHelper_ob = new MySQLiteHelper(context,
MySQLiteHelper.DATABASE_NAME, null, MySQLiteHelper.DATABASE_VERSION);
database_ob = openHelper_ob.getWritableDatabase();
return this;
}
public adapter openToWrite()
{
openHelper_ob = new MySQLiteHelper(context,
MySQLiteHelper.DATABASE_NAME, null, MySQLiteHelper.DATABASE_VERSION);
database_ob = openHelper_ob.getWritableDatabase();
return this;
}
public void Close()
{
database_ob.close();
}
public long insertDetails(String title, String year)
{
ContentValues cv = new ContentValues();
cv.put(MySQLiteHelper.KEY_TITLE, title);
cv.put(MySQLiteHelper.KEY_YEAR, year);
openToWrite();
long val = database_ob.insert(MySQLiteHelper.TABLE_NAME, null, cv);
Close();
return val;
}
public Cursor queryName()
{
String[] cols = { MySQLiteHelper.KEY_ID, MySQLiteHelper.KEY_TITLE,
MySQLiteHelper.KEY_YEAR };
openToWrite();
Cursor c = database_ob.query(MySQLiteHelper.TABLE_NAME, cols, null, null, null, null, null);
return c;
}
public Cursor queryAll(int nameId)
{
String[] cols = { MySQLiteHelper.KEY_ID, MySQLiteHelper.KEY_TITLE, MySQLiteHelper.KEY_YEAR };
openToWrite();
Cursor c = database_ob.query(MySQLiteHelper.TABLE_NAME, cols, MySQLiteHelper.KEY_ID + "=" + nameId, null,null,null,null);
return c;
}
}
MySQLiteHelper
package com.example.imdbproject;
import java.util.LinkedList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "movie_data.db";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "movies";
public static final String KEY_ID = "_id";
public static final String KEY_TITLE = "title";
public static final String KEY_YEAR = "year";
public static final String SCRIPT = "Create table " + TABLE_NAME + " ("
+ KEY_ID + " integer primary key autoincrement, " + KEY_TITLE
+ " text, " + KEY_YEAR + " text);";
public MySQLiteHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public MySQLiteHelper(Context context, String name, CursorFactory factory, int version)
{
super(context, name, factory, version);
}
#Override public void onCreate(SQLiteDatabase db)
{
db.execSQL(SCRIPT);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE " + TABLE_NAME);
onCreate(db);
}
public long addMovie(String movietitle, String year){
ContentValues cv = new ContentValues();
cv.put(KEY_TITLE, movietitle);
cv.put(KEY_YEAR, year);
SQLiteDatabase sd = getWritableDatabase();
long result = sd.insert(TABLE_NAME, KEY_TITLE, cv);
return result;
}
}
AddSomeMovies
package com.example.imdbproject;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class AddSomeMovies extends Activity {
adapter adapter;
MySQLiteHelper helper;
EditText titleEdit, yearEdit;
Button addButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_some_movies);
titleEdit = (EditText)findViewById(R.id.etTitle);
yearEdit = (EditText)findViewById(R.id.etYear);
addButton = (Button)findViewById(R.id.button1);
adapter = new adapter(this);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0)
{
String titleValue = titleEdit.getText().toString();
String yearValue = yearEdit.getText().toString();
long val = adapter.insertDetails(titleValue, yearValue);
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add_some_movies, menu);
return true;
}
}
activity_main.xml
<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:background="#color/black"
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=".MainActivity" >
<Button
android:id="#+id/buttonShowAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonTitle"
android:layout_below="#+id/buttonTitle"
android:text="#string/showall"
/>
<Button
android:id="#+id/buttonYear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/buttonTitle"
android:layout_alignBottom="#+id/buttonTitle"
android:layout_alignParentRight="true"
android:text="#string/yearsearch"
/>
<Button
android:id="#+id/buttonTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/titlesearch"
/>
<Button
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/buttonShowAll"
android:layout_alignBottom="#+id/buttonShowAll"
android:layout_toRightOf="#+id/buttonShowAll"
android:text="#string/add"
android:onClick="add" />
<Button
android:id="#+id/buttonOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/buttonYear"
android:layout_below="#+id/buttonYear"
android:enabled="false"
android:onClick="ok"
android:text="#string/ok"
android:visibility="invisible" />
<EditText
android:id="#+id/edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/buttonShowAll"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="#string/searcheditview" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttonShowAll"
android:layout_below="#+id/edittext" >
</ListView>
</RelativeLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tv_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp" />
</LinearLayout>

SORTED!
At least I think so. Problem disappeared when I changed database version to 2 from 1. So I can guess that it needs more attention now.

try following
public static final String KEY_ID = BaseColumns._ID;
instead of
public static final String KEY_ID = "_id";

Related

Why Todo app not showing the added tasks?

Todo app doesn't show the added task...its like not refreshing the added task. When i close and open the app again, then it shows. I am a beginner so i can,t guess out the problem.
Here is MainActivity.java
package com.ashtiv.dooo;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.ashtiv.dooo.db.TaskContract;
import com.ashtiv.dooo.db.TaskDbHelper;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private TaskDbHelper mHelper;
private ListView mTaskListView;
private ArrayAdapter<String> mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHelper = new TaskDbHelper(this);
mTaskListView = (ListView) findViewById(R.id.list_todo);
updateUI();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_task:
final EditText taskEditText = new EditText(this);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Add a new task")
.setMessage("What do you want to do next?")
.setView(taskEditText)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String task = String.valueOf(taskEditText.getText());
SQLiteDatabase db = mHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(TaskContract.TaskEntry.COL_TASK_TITLE, task);
db.insertWithOnConflict(TaskContract.TaskEntry.TABLE,
null,
values,
SQLiteDatabase.CONFLICT_REPLACE);
db.close();
}
})
.setNegativeButton("Cancel", null)
.create();
dialog.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void deleteTask(View view) {
View parent = (View) view.getParent();
TextView taskTextView = (TextView) parent.findViewById(R.id.task_title);
String task = String.valueOf(taskTextView.getText());
SQLiteDatabase db = mHelper.getWritableDatabase();
db.delete(TaskContract.TaskEntry.TABLE,
TaskContract.TaskEntry.COL_TASK_TITLE + " = ?",
new String[]{task});
db.close();
updateUI();
}
private void updateUI() {
ArrayList<String> taskList = new ArrayList<>();
SQLiteDatabase db = mHelper.getReadableDatabase();
Cursor cursor = db.query(TaskContract.TaskEntry.TABLE,
new String[]{TaskContract.TaskEntry._ID, TaskContract.TaskEntry.COL_TASK_TITLE},
null, null, null, null, null);
while (cursor.moveToNext()) {
int idx = cursor.getColumnIndex(TaskContract.TaskEntry.COL_TASK_TITLE);
taskList.add(cursor.getString(idx));
}
if (mAdapter == null) {
mAdapter = new ArrayAdapter<>(this,
R.layout.item_todo,
R.id.task_title,
taskList);
mTaskListView.setAdapter(mAdapter);
} else {
mAdapter.clear();
mAdapter.addAll(taskList);
mAdapter.notifyDataSetChanged();
}
cursor.close();
db.close();
}
}
Here is TaskContract.java
package com.ashtiv.dooo.db;
import android.provider.BaseColumns;
public class TaskContract {
public static final String DB_NAME = "com.ashtiv.dooo.db";
public static final int DB_VERSION = 1;
public class TaskEntry implements BaseColumns {
public static final String TABLE = "tasks";
public static final String COL_TASK_TITLE = "title";
}
}
Here is TaskDbHelper.java
package com.ashtiv.dooo.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class TaskDbHelper extends SQLiteOpenHelper {
public TaskDbHelper(Context context) {
super(context, TaskContract.DB_NAME, null, TaskContract.DB_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TaskContract.TaskEntry.TABLE + " ( " +
TaskContract.TaskEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
TaskContract.TaskEntry.COL_TASK_TITLE + " TEXT NOT NULL);";
db.execSQL(createTable);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TaskContract.TaskEntry.TABLE);
onCreate(db);
}
}
hERE is actvity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.ashtiv.dooo.MainActivity">
<ListView
android:id="#+id/list_todo"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Here is item_todo.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical">
<TextView
android:id="#+id/task_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="Hello"
android:textSize="20sp" />
<Button
android:id="#+id/task_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="Done"
android:onClick="deleteTask"/>
</RelativeLayout>
The issue was you are not updating the UI after adding a new Todo.
To tackle the issue modify your code as follows.
Edit your onOptionsItemSelected to
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_task:
addTodo();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
and add this function.
private void addTodo() {
final EditText taskEditText = new EditText(this);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Add a new task")
.setMessage("What do you want to do next?")
.setView(taskEditText)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String task = String.valueOf(taskEditText.getText());
SQLiteDatabase db = mHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(TaskContract.TaskEntry.COL_TASK_TITLE, task);
db.insertWithOnConflict(TaskContract.TaskEntry.TABLE,
null,
values,
SQLiteDatabase.CONFLICT_REPLACE);
db.close();
}
})
.setNegativeButton("Cancel", null)
.create();
dialog.show();
updateUI();
}
The issue you are seeing is because function updateUI() only gets called when your activity is created. You would have to devise a method where updateUI() gets called for each time you make an update to the table.
You could try calling updateUI() whenever you add or remove some tasks. For example:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_task:
final EditText taskEditText = new EditText(this);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Add a new task")
.setMessage("What do you want to do next?")
.setView(taskEditText)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String task = String.valueOf(taskEditText.getText());
SQLiteDatabase db = mHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(TaskContract.TaskEntry.COL_TASK_TITLE, task);
db.insertWithOnConflict(TaskContract.TaskEntry.TABLE,
null,
values,
SQLiteDatabase.CONFLICT_REPLACE);
db.close();
//NEW LINE
updateUI();
}
})
.setNegativeButton("Cancel", null)
.create();
dialog.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Sqlite+Recyclerview , app has stopped

I wanna create a recyclerview which can retrieve data from database, and followed this video step by step. The application "has stopped" when I tried to run it.I I have no idea where I got it wrong. Please help.
activity_main.xml
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:padding="16dp">
<EditText
android:id="#+id/edittext_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textviewamount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/edittext_name"
android:layout_centerHorizontal="true"
android:text="0"
android:textSize="50sp"
android:layout_marginTop="8dp"
/>
<Button
android:id="#+id/button_decrease"
android:layout_width="50dp"
android:layout_height="72dp"
android:layout_alignTop="#+id/textviewamount"
android:layout_toStartOf="#+id/textviewamount"
android:layout_toLeftOf="#+id/textviewamount"
android:text="-" />
<Button
android:id="#+id/button_increase"
android:layout_width="50dp"
android:layout_height="72dp"
android:layout_alignTop="#+id/textviewamount"
android:layout_toEndOf="#+id/textviewamount"
android:layout_toRightOf="#+id/textviewamount"
android:text="+" />
<Button
android:id="#+id/button_add"
android:layout_width="wrap_content"
android:layout_height="72dp"
android:layout_alignEnd="#+id/edittext_name"
android:layout_alignRight="#+id/edittext_name"
android:layout_alignTop="#+id/textviewamount" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/button_decrease"
android:layout_centerHorizontal="true">
</android.support.v7.widget.RecyclerView>
grocery_item.xml
android:layout_margin="8dp"
>
<TextView
android:id="#+id/textview_amount_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="30sp"
/>
<TextView
android:id="#+id/textview_name_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Empty Item"
android:textSize="30sp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
/>
MainActivity.java
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private SQLiteDatabase mDatabase;
private GroceryAdapter mAdapter;
private EditText mEditTextName;
private TextView mTextViewAmount;
private int mAmount = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GroceryDBHelper dbHelper = new GroceryDBHelper(this);
mDatabase = dbHelper.getWritableDatabase();
//能往database里写数据的必须语句
RecyclerView recyclerView = findViewById(R.id.recyclerview);
//MainActivit.xml里的recyclerView
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new GroceryAdapter(this,getAllItems());
//cursor的位置放getAllItems方法来传指针
recyclerView.setAdapter(mAdapter);
mEditTextName = findViewById(R.id.edittext_name);
mTextViewAmount = findViewById(R.id.textview_amount_item);
Button buttonIncrease = findViewById(R.id.button_increase);
Button buttonDecrease = findViewById(R.id.button_decrease);
Button buttonAdd = findViewById(R.id.button_add);
buttonIncrease.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
increase();
}
});
buttonDecrease.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
decrease();
}
});
buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addItem();
}
});
}
private void increase()
{
mAmount++;
mTextViewAmount.setText(String.valueOf(mAmount));
}
private void decrease() {
if (mAmount > 0) {
mAmount--;
mTextViewAmount.setText(String.valueOf(mAmount));
}
}
private void addItem()
//往database里加item的方法
{
if (mEditTextName.getText().toString().trim().length() == 0 || mAmount == 0)
//trim:删除句首和句尾的空格
{
return;
//如果每输入或输入数量为0,就不添加item,直接返回additem方法
}
String name = mEditTextName.getText().toString();
ContentValues cv = new ContentValues();
cv.put(GroceryContract.GroceryEntry.COLUMN_NAME, name);
//将name放入database的name column里
cv.put(GroceryContract.GroceryEntry.COLUMN_AMOUNT, mAmount);
//将数量放入database的amount column里
//无需手动添加id和stamp,会自动添加
mDatabase.insert(GroceryContract.GroceryEntry.TABLE_NAME,null, cv);
//将cv放入database的table里
mAdapter.swapCursor(getAllItems());
//添加新item后转换指针
mEditTextName.getText().clear();
//清空输入栏
}
private Cursor getAllItems()
{
return mDatabase.query(
GroceryContract.GroceryEntry.TABLE_NAME,
null,
null,
null,
null,
null,
GroceryContract.GroceryEntry.COLUMN_TIMESTAMP + " DESC"
//decending order
);
}
}
GroceryDBHelper.java
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.playah.recyclerdatabase.GroceryContract.*;
public class GroceryDBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "grocerylist.db";
public static final int DATABASE_VERSION = 1;
//这个数字是database的scheme
public GroceryDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
final String SQL_CREATE_GROCERYLIST_TABLE = "CREATE TABLE "+
GroceryEntry.TABLE_NAME + " (" +
GroceryEntry._ID + "INTEGER PRIMARY KEY AUTOINCREMENT, "+
//ID没创建,但是已经GroceryContract.java里通过BaseColumn引入
//primary key:unique identify of the row
//autoincrement: 每往table加一行就添加一次
GroceryEntry.COLUMN_NAME + "TEXT NOT NULL," +
//not null: we have to provide a value
GroceryEntry.COLUMN_AMOUNT + "INTEGER NOT NULL," +
GroceryEntry.COLUMN_TIMESTAMP +"TIMESTAMP DEFAULT CURRENT_TIMESTAMP" +
//每往table加一行就在当前位置创建一次time stamp
");";
db.execSQL(SQL_CREATE_GROCERYLIST_TABLE);
//执行创建database
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + GroceryEntry.TABLE_NAME);
//如果database已存在,drop并重建
onCreate(db);
}
}
**GroceryContract**
import android.provider.BaseColumns;
public class GroceryContract
{ private GroceryContract() {}
//引入interface
public static final class GroceryEntry implements BaseColumns {
//展示table的id columns
public static final String TABLE_NAME = "groceryList";
//table name
public static final String COLUMN_NAME = "name";
//first column of the table
public static final String COLUMN_AMOUNT = "amount";
//amount of the item
public static final String COLUMN_TIMESTAMP = "timestemp";
}
}
GroceryContract.java
import android.provider.BaseColumns;
public class GroceryContract
//为table定义字符实体和行数
{ private GroceryContract() {}
//用的是下面的方法,用不到这个方法来构造函数
//引入interface
public static final class GroceryEntry implements BaseColumns {
//展示table的id columns
public static final String TABLE_NAME = "groceryList";
//table name
public static final String COLUMN_NAME = "name";
//first column of the table
public static final String COLUMN_AMOUNT = "amount";
//amount of the item
public static final String COLUMN_TIMESTAMP = "timestemp";
//改变item的数量
}
}
GroceryAdapter.java
import android.content.Context;
import android.database.Cursor;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class GroceryAdapter extends RecyclerView.Adapter<GroceryAdapter.GroceryViewHolder> {
private Context mContext;
private Cursor mCursor;
public GroceryAdapter(Context context, Cursor cursor)
//cursor使能够从database取出数据
{
mContext = context;
mCursor = cursor;
}
public class GroceryViewHolder extends RecyclerView.ViewHolder
{
public TextView nameText;
public TextView countText;
public GroceryViewHolder(#NonNull View itemView) {
super(itemView);
nameText = itemView.findViewById(R.id.textview_name_item);
countText = itemView.findViewById(R.id.textview_amount_item);
}
}
#NonNull
#Override
public GroceryViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.grocery_item, parent, false);
//使用recyclervew的layout
return new GroceryViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull GroceryViewHolder holder, int position) {
if (!mCursor.moveToPosition(position)){
//判断cursor是否存在
return;
}
String name = mCursor.getString(mCursor.getColumnIndex(GroceryContract.GroceryEntry.COLUMN_NAME));
int amount = mCursor.getInt(mCursor.getColumnIndex(GroceryContract.GroceryEntry.COLUMN_AMOUNT));
//从database中取得上面两种数据
holder.nameText.setText(name);
holder.countText.setText(String.valueOf(amount));
}
#Override
public int getItemCount() {
return mCursor.getCount();
}
public void swapCursor(Cursor newCursor)
//转换cursor,为了使数据库更新后cursor在正确的位置
{
if (mCursor !=null){
mCursor.close();
}
mCursor = newCursor;
if(newCursor != null){
notifyDataSetChanged();
}
}
Here is the logcat
--------- beginning of crash
08-13 15:51:49.125 5572-5572/com.playah.recyclerdatabase E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.playah.recyclerdatabase, PID: 5572
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.playah.recyclerdatabase/com.playah.recyclerdatabase.MainActivity}: android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY (code 1): , while compiling: CREATE TABLE groceryList (_idINTEGER PRIMARY KEY AUTOINCREMENT, nameTEXT NOT NULL,amountINTEGER NOT NULL,timestempTIMESTAMP DEFAULT CURRENT_TIMESTAMP);
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY (code 1): , while compiling: CREATE TABLE groceryList (_idINTEGER PRIMARY KEY AUTOINCREMENT, nameTEXT NOT NULL,amountINTEGER NOT NULL,timestempTIMESTAMP DEFAULT CURRENT_TIMESTAMP);
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1677)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608)
at com.playah.recyclerdatabase.GroceryDBHelper.onCreate(GroceryDBHelper.java:33)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:294)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:194)
at com.playah.recyclerdatabase.MainActivity.onCreate(MainActivity.java:35)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
Add space before INTEGER in double qots
GroceryEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
It looks like your CREATE clause misses spaces between the column names and their types.
Try to add a blank space after the column ids, like this :
GroceryEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
Right before the keyword "INTEGER". Do the same for the other lines.

TextView not displaying final result

I am new to android , I creating an app for sql , its a simple app where user enters a name and the name appears on the display using a textview but unfortunately text is not displaying
Here is the code:
activity_main.xml
<?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.ashis.mmm.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="52dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp" />
</RelativeLayout>
MainActivity.java
package com.example.ashis.mmm;
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.TextView;
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
TextView textView;
Database db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText) findViewById(R.id.editText);
button=(Button) findViewById(R.id.button);
textView=(TextView) findViewById(R.id.textView);
db = new Database(getApplicationContext(),null,null,1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String input = editText.getText().toString() ;
Person person = new Person(input);
db.add_row(person);
editText.setText("");
printDatabse();
}
});
}
private void printDatabse() {
String dbString = db.printData();
textView.setText(dbString);
}
}
Person.java
package com.example.ashis.mmm;
/**
* Created by ashis on 8/27/2016.
*/
public class Person {
private int _id;
private String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
DataBase.java
package com.example.ashis.mmm;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by ashis on 8/27/2016.
*/
public class Database extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "persons.db";
private static final String TABLE_NAME = "Persons";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_NAME = "Name";
public Database(Context context, String DATABASE_NAME, SQLiteDatabase.CursorFactory factory, int DATABASE_VERSION) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
String onCreate = "CREATE TABLE " + TABLE_NAME + "( " +
COLUMN_ID + " INTEGER ," +
COLUMN_NAME + " TEXT );";
db.execSQL(onCreate);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
}
public void add_row(Person person)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, person.getName());
db.insert(TABLE_NAME, null, values);
db.close();
}
public String printData()
{
String dbqry = "";
SQLiteDatabase db = getReadableDatabase();
String printqry = "SELECT * FROM " + TABLE_NAME;
Cursor cursor = db.rawQuery(printqry,null);
cursor.moveToFirst();
while (!cursor.isAfterLast())
{
if (cursor.getString(cursor.getColumnIndex("Name")) != null)
{
dbqry += cursor.getString(1);
dbqry +="\n";
}
}
db.close();
cursor.close();
return dbqry;
}
}
the best way to see why the text is not appearing inside your TextView is to debug, but it seems that you are not so good to do it. In this case you can use Log.d(); method. I will suggest to you the places where you may have problems, just put some logs on those places and check what is happening with your variable and values.
Check does your TextView is visible on the screen and does some other view is not cover it. You can do that for example by setting some background colour of the textview and some fixed width and height (50dp), this will show you the TextView.
Inside printData() method put some logs to see what is coming from the DB, just to print your string values;
Check the Cursor inside printData() just to be sure that it contains some records , maybe your DB name is wrong or DB table is wrong or something else.
If your view is visible on the screen and printData() method is working you will not have any other problems to see the text!

database id's are interchanged android sqlite

Good Day everyone. I have this code and i face problem in deleting. For example, my list of names are Junko, Gemina and Fukar. If i view each, they show same details. Now, if I delete any of them, ex. I delete Fukar, it will be deleted successfully but when I click on Gemina, the detail that appears is of Junko. If i click Junko, it says "Unfortunately the app stopped working". If I add new Name, ex. Nimrod, It will add successfully, but when i view Nimrod, the detail of Gemina will appear. And if i add another name, Vourin, the data of Gemina will appear its like their ids are interchanged and I think the error is in the delete part. I appreciate any comment. Thanks.
Below is my DBHelper.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.HashMap;
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "contactdb.sqlite";
public static final String CONTACTS_TABLE_NAME = "mycontacts";
public static final String CONTACTS_COLUMN_ID = "id";
public static final String CONTACTS_COLUMN_STUNAME = "name";
public static final String CONTACTS_COLUMN_STUPHONE = "phone";
public static final String CONTACTS_COLUMN_STUSTREET = "street";
public static final String CONTACTS_COLUMN_STUEMAIL = "email";
public static final String CONTACTS_COLUMN_STUCITY = "place";
private HashMap hp;
public DBHelper(Context context)
{
super(context, DATABASE_NAME , null, 3);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(
"create table mycontacts " +
"(id integer primary key autoincrement, name text,phone text,email text, street text,place text)"
);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS mycontacts");
onCreate(db);
}
public boolean addStudentContact(String contactname,String contactphone,String contactstreet,String contactemail, String contactplace){
/*,*/
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contantValues = new ContentValues();
contantValues.put("name",contactname);
contantValues.put("phone", contactphone);
contantValues.put("street",contactstreet);
contantValues.put("email",contactemail);
contantValues.put("place",contactplace);
db.insert("mycontacts", null, contantValues);
db.close();
return true;
}
public boolean updateStudentContact(Integer contactid,String contactname,String contactphone,String contactstreet,String contactemail, String contactplace)
{
/*,String contactname,*/
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contantValues = new ContentValues();
contantValues.put("name",contactname);
contantValues.put("phone", contactphone);
contantValues.put("street",contactstreet);
contantValues.put("email",contactemail);
contantValues.put("place",contactplace);
db.update("mycontacts", contantValues, "id = ?", new String[]{Integer.toString(contactid)});
db.close();
return true;
}
public Integer deleteContact(Integer id){
SQLiteDatabase db=this.getWritableDatabase();
return db.delete("mycontacts","id = ?",new String[]{Integer.toString(id)});
}
public Cursor getData(int contactid){
SQLiteDatabase db=this.getWritableDatabase();
Cursor res=db.rawQuery("Select * from mycontacts where id = " + contactid + "", null);
return res;
}
public int numberOfRows(){
SQLiteDatabase db=this.getWritableDatabase();
int numRows=(int) DatabaseUtils.queryNumEntries(db,CONTACTS_TABLE_NAME);
return numRows;
}
public ArrayList<Person> getAllStudentContacts(){
ArrayList<Person> arraylist= new ArrayList<>();
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor=db.rawQuery("Select * from mycontacts",null);
if (cursor.moveToFirst()) {
do {
Person person = new Person();
person.name = cursor.getString(cursor.getColumnIndex(CONTACTS_COLUMN_STUNAME));
person.ID = cursor.getInt(cursor.getColumnIndex(CONTACTS_COLUMN_ID));
arraylist.add(person);
} while (cursor.moveToNext());
}
return arraylist;
}
}
DisplayContact.java
package com.test.ppandey.contactapp;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class DisplayContact extends AppCompatActivity {
int from_Where_I_Am_Coming = 0;
private DBHelper mydb;
TextView name;
TextView phone;
TextView email;
TextView street;
TextView place;
int id_To_Update = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_contact);
name = (TextView) findViewById(R.id.editTextName);
phone = (TextView) findViewById(R.id.editTextPhone);
email = (TextView) findViewById(R.id.editTextStreet);
street = (TextView) findViewById(R.id.editTextEmail);
place = (TextView) findViewById(R.id.editTextCity);
mydb = new DBHelper(this);
Bundle extras = getIntent().getExtras();
{
int Value = extras.getInt("id");
if (Value > 0) {
//means this is the view part not the add contact part.
Cursor rs = mydb.getData(Value);
id_To_Update = Value;
rs.moveToFirst();
String stuname = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_STUNAME));
String stuphone = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_STUPHONE));
String stuemail = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_STUEMAIL));
String stustreet = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_STUSTREET));
String stuplace = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_STUCITY));
if (!rs.isClosed()) {
rs.close();
}
Button b = (Button) findViewById(R.id.button1);
b.setVisibility(View.INVISIBLE);
name.setText((CharSequence) stuname);
name.setFocusable(false);
name.setClickable(false);
phone.setText((CharSequence) stuphone);
phone.setFocusable(false);
phone.setClickable(false);
email.setText((CharSequence) stuemail);
email.setFocusable(false);
email.setClickable(false);
street.setText((CharSequence) stustreet);
street.setFocusable(false);
street.setClickable(false);
place.setText((CharSequence) stuplace);
place.setFocusable(false);
place.setClickable(false);
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
Bundle extras = getIntent().getExtras();
if (extras != null) {
int Value = extras.getInt("id");
if (Value > 0) {
getMenuInflater().inflate(R.menu.menu_display_contact, menu);
} else {
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.
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.Edit_Contact:
Button b = (Button) findViewById(R.id.button1);
b.setVisibility(View.VISIBLE);
name.setEnabled(true);
name.setFocusableInTouchMode(true);
name.setClickable(true);
phone.setEnabled(true);
phone.setFocusableInTouchMode(true);
phone.setClickable(true);
email.setEnabled(true);
email.setFocusableInTouchMode(true);
email.setClickable(true);
street.setEnabled(true);
street.setFocusableInTouchMode(true);
street.setClickable(true);
place.setEnabled(true);
place.setFocusableInTouchMode(true);
place.setClickable(true);
return true;
case R.id.Delete_Contact:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.deleteContact)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mydb.deleteContact(id_To_Update);
Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog d = builder.create();
d.setTitle("Are you sure ?");
d.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void saveData(View view) {
/*, */
/* mydb.addContact(name.getText().toString(),email.getText().toString(), street.getText().toString(), place.getText().toString(), phone.getText().toString());
finish();*/
Bundle extras = getIntent().getExtras();
if (extras != null) {
int Value = extras.getInt("id");
if (Value > 0) {
if (mydb.updateStudentContact(id_To_Update, name.getText().toString(), phone.getText().toString(), street.getText().toString(),email.getText().toString(), place.getText().toString())) {
Toast.makeText(getApplicationContext(), "Successfully Updated", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "Record not updated", Toast.LENGTH_SHORT).show();
}
} else {
if (mydb.addStudentContact(name.getText().toString(), phone.getText().toString(),street.getText().toString(), email.getText().toString(), place.getText().toString())) {
Toast.makeText(getApplicationContext(), "Successfully Added", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Record not added", Toast.LENGTH_SHORT).show();
}
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
}
}
}
MainActivity.java
package com.test.ppandey.contactapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE="MESSAGE";
private ListView objListView;
DBHelper db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db=new DBHelper(this);
ArrayList<Person> arrayList=db.getAllStudentContacts();
ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,arrayList);
objListView=(ListView)findViewById(R.id.listView1);
objListView.setAdapter(arrayAdapter);
objListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
long id_to_search = id;
Bundle dataBundle = new Bundle();
dataBundle.putLong("id", id_to_search);
Intent intent = new Intent(getApplicationContext(), DisplayContact.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
}
#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);*/
super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case R.id.item1:Bundle dataBundle = new Bundle();
dataBundle.putInt("id", 0);
Intent intent = new Intent(getApplicationContext(),DisplayContact.class);
intent.putExtras(dataBundle);
startActivityForResult(intent, 0);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
db=new DBHelper(this);
ArrayList<Person> arrayList=db.getAllStudentContacts();
ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,arrayList);
objListView=(ListView)findViewById(R.id.listView1);
objListView.setAdapter(arrayAdapter);
}
}
activity_display_contact.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollView1"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="350dp"
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="com.test.ppandey.contactapp.DisplayContact">
<EditText
android:id="#+id/editTextName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="5dp"
android:layout_marginLeft="82dp"
android:ems="10"
android:inputType="text" >
</EditText>
<EditText
android:id="#+id/editTextEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editTextStreet"
android:layout_below="#+id/editTextStreet"
android:layout_marginTop="22dp"
android:ems="10"
android:inputType="textEmailAddress" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editTextName"
android:layout_alignParentLeft="true"
android:text="#string/name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editTextCity"
android:layout_alignParentBottom="true"
android:layout_marginBottom="28dp"
android:onClick="saveData"
android:text="#string/save" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editTextEmail"
android:layout_alignLeft="#+id/textView1"
android:text="#string/email"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editTextPhone"
android:layout_alignLeft="#+id/textView1"
android:text="#string/phone"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editTextEmail"
android:layout_alignLeft="#+id/textView5"
android:text="#string/street"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editTextCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/editTextName"
android:layout_below="#+id/editTextEmail"
android:layout_marginTop="30dp"
android:ems="10"
android:inputType="text" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editTextCity"
android:layout_alignBottom="#+id/editTextCity"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/editTextEmail"
android:text="#string/country"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editTextStreet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editTextName"
android:layout_below="#+id/editTextPhone"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editTextPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editTextStreet"
android:layout_below="#+id/editTextName"
android:ems="10"
android:inputType="phone|text" />
</RelativeLayout>
</ScrollView>
Person.java
package com.test.ppandey.contactapp;
/**
* Created by Ariel on 08/03/2016.
*/
public class Person {
public String name;
public long ID;
private Person person; {
name = new String(name);
}
private Person id; {
ID = new long(id);
}
}
I added Person.java and edited MainActivity.java and DBHelper.java
This is your problem:
int id_to_search = position + 1;
You are assuming that the IDs and the positions are in sync. This is only true as long as you do not remove any entries. Instead of using the position to reference an entry, you have to use the ID, as that's immutable.
When you pull the entries from your database, you need to pull the name and the ID, and keep that data in your adapter, so you can reference an entry by its ID.
Some code below (just a skeleton):
1) Create a model for your data
public class Person {
// consider making private and adding accessors
public String name;
public long id;
}
2) when you pull your data, pull the name and the ID (you may expand the Person class and this function to pull additional data):
public List<Person> getAllStudentContacts(){
List<Person> arraylist= new ArrayList<>();
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor=db.rawQuery("Select * from mycontacts",null);
if (cursor.moveToFirst()) {
do {
Person person = new Person();
person.name = cursor.getString(cursor.getColumnIndex(CONTACTS_COLUMN_STUNAME));
person.id = cursor.getInt(cursor.getColumIndex(CONTACTS_COLUMN_ID));
arraylist.add(prson);
} while (cursor.moveToNext());
}
return arraylist;
}
Then in your adapter you use the ID in the Person object to reference it, instead of the position.
Are you refreshing your "List of Names" from your android App afer every insert/delete?
Maybe by calling getAllStudentContacts() from your code after every insert/delete to refill your List of names of your android app?
Good Luck

Uri argument and LoaderCallbacks

I have four class :
MainActivity.java - MyAdapter.java - MyDataBase.java - MyListActivity.java
now, what is Uri in the MyListActiviy ? How can I fill it ?
when I Click "showlist_btn" button in MainActivity, a list without content is displayed ! why ?
MainActivity:
package com.example.ex22;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText edittext ;
Button add_btn ;
Button showlist_btn ;
MyDataBase mydatabase ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydatabase = new MyDataBase(MainActivity.this);
edittext = (EditText)findViewById(R.id.edittext);
showlist_btn = (Button)findViewById(R.id.showlist_btn);
add_btn = (Button)findViewById(R.id.add_btn);
add_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str = edittext.getText().toString();
mydatabase.addName(str);
edittext.setText("");
}
});
showlist_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, MyListActivity.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
MyAdapter:
package com.example.ex22;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
List<String> mylist ;
LayoutInflater inflater ;
public MyAdapter(Context context){
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mylist.size();
}
void getList(List<String> list){
mylist = list ;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewholder ;
if(convertView == null){
convertView = inflater.inflate(R.layout.row_for_list, null);
viewholder = new ViewHolder();
viewholder.text = (TextView)convertView.findViewById(R.id.textview_forrow);
convertView.setTag(viewholder);
}else {
viewholder = (ViewHolder)convertView.getTag();
}
viewholder.text.setText(mylist.get(position));
return convertView;
}
static class ViewHolder{
TextView text ;
}
}
MyDataBase:
package com.example.ex22;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDataBase extends SQLiteOpenHelper {
private final static String DB_NAME = "mydb" ;
private final static String TABLE_NAME = "mytable" ;
//private static final String NAME = "name" ;
private final static String NAME_COLOUM = "_name" ;
private static final String CREATE_TABLE = "CREATE TABLE "+ TABLE_NAME + "("
+ NAME_COLOUM + " TEXT" + ")";
public MyDataBase(Context context){
super(context,DB_NAME,null,1);
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
}
public void addName(String str){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NAME_COLOUM, str);
db.insert(TABLE_NAME, null, values);
db.close();
}
public List<String> getAllNames(){
List<String> list = new ArrayList<String>() ;
SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.rawQuery("SELECT * FROM "+ TABLE_NAME, null);
if(c.moveToFirst()){
do{String str = c.getString(0);
list.add(str);
}while(c.moveToNext());
}
return list ;
}
public String getCulomName(){
return this.NAME_COLOUM;
}
}
MyListActivity:
package com.example.ex22;
import android.app.ListActivity;
import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;
public class MyListActivity extends ListActivity
implements LoaderCallbacks<Cursor>{
SimpleCursorAdapter simplecursorradapter ;
MyDataBase mydb ;
CursorLoader cursorloader ;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(R.layout.show_list);
mydb = new MyDataBase(this);
String[] from = new String[] { mydb.getCulomName() };
int[] to = new int[]{
R.id.forlist
};
simplecursorradapter = new SimpleCursorAdapter(this, android.R.layout.simple_expandable_list_item_2, null,
from , to ,0);
setListAdapter(simplecursorradapter);
LoaderManager loadermanager = getLoaderManager();
loadermanager.initLoader(0, null, this);
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// TODO Auto-generated method stub
String[] projection = {mydb.getCulomName()};
final String SCHEME = "content";
// The provider's authority
final String AUTHORITY = "com.example.ex22";
final Uri src = Uri.parse(SCHEME + "://" + AUTHORITY);
Uri uri = Uri.withAppendedPath(src, "mytable");
cursorloader = new CursorLoader(this, uri, projection, null, null, null);
return cursorloader;
}
#Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
// TODO Auto-generated method stub
simplecursorradapter.swapCursor(arg1);
}
#Override
public void onLoaderReset(Loader<Cursor> arg0) {
// TODO Auto-generated method stub
simplecursorradapter.swapCursor(null);
}
}
activity_main :
<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"
tools:context=".MainActivity" >
<EditText
android:id="#+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Write Your Name..."
/>
<Button
android:id="#+id/add_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add Name"
/>
<Button
android:id="#+id/showlist_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Show Name List"
/>
</LinearLayout>
forlist:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/forlist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
row_for_list:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textview_forrow"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="40sp"
/>
</LinearLayout>
show_list:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

Categories

Resources