最近、いくつかの小さな変更を加えましたが、エラーがこれらすべての原因となっていることがわかりません。どんな助けでも大歓迎です
主要
package com.androidhive.androidsqlite;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class AndroidAss2SQL extends Activity {
/** Called when the activity is first created. */
TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DatabaseHandler db = new DatabaseHandler(this);
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addContact(new Coms("Gold", "100.00", "0"));
db.addContact(new Coms("Silver", "300.00", "3"));
db.addContact(new Coms("Wheat", "500.00", "4"));
db.addContact(new Coms("Sugar", "750.00", "0"));
db.addContact(new Coms("Coffee", "433.00", "9"));
// Reading all contacts
Log.d("Reading: ", "Reading all coms..");
List<Coms> contacts = db.getAllContacts();
for (Coms cn : contacts) {
String log = "Id: "+cn.getID()+" ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber() + " ,Quantity: " + cn.getQuan();
// Writing Contacts to log
Log.d("Name: ", log);
}
Coms rando = db.getContact(1);
text = (TextView) findViewById(R.id.textView2);
text.setText(String.valueOf(rando));
Coms rando1 = db.getContact(2);
text = (TextView) findViewById(R.id.textView4);
text.setText(String.valueOf(rando1));
Coms rando2 = db.getContact(3);
text = (TextView) findViewById(R.id.textView6);
text.setText(String.valueOf(rando2));
Coms rando3 = db.getContact(4);
text = (TextView) findViewById(R.id.textView8);
text.setText(String.valueOf(rando3));
Coms rando4 = db.getContact(5);
text = (TextView) findViewById(R.id.textView10);
text.setText(String.valueOf(rando4));
}
}
コムクラス
package com.androidhive.androidsqlite;
public class Coms {
//private variables
int _id;
String _name;
String _price;
String _quan;
// Empty constructor
public Coms(){
}
// constructor
public Coms(int id, String name, String price){
this._id = id;
this._name = name;
this._price = price;
}
// constructor
public Coms(String name, String price){
this._name = name;
this._price = price;
}
// constructor
public Coms(String name){
this._name = name;
}
public Coms(String name, String price, String quan){
this._name = name;
this._price = price;
this._quan = quan;
}
public Coms(int id, String name, String price, String quan){
this._id = id;
this._name = name;
this._price = price;
this._quan = quan;
}
@Override
public String toString(){
return this._price;
}
// getting ID
public int getID(){
return this._id;
}
// setting id
public void setID(int id){
this._id = id;
}
// getting name
public String getName(){
return this._name;
}
// setting name
public void setName(String name){
this._name = name;
}
// getting phone number
public String getPhoneNumber(){
return this._price;
}
// setting phone number
public void setPhoneNumber(String phone_number){
this._price = phone_number;
}
// getting name
public String getQuan(){
return this._quan;
}
// setting name
public void setQuan(String name){
this._quan = name;
}
}
DB ハンドラー - 私の変更のほとんどはここで行われ、Quantity (quan) を追加しようとしました
package com.androidhive.androidsqlite;
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 DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "comstuff";
// Contacts table name
private static final String TABLE_CONTACTS = "coms";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PRICE = "price";
private static final String KEY_QUAN = "quantity";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_PRICE + " TEXT," + KEY_QUAN + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
void addContact(Coms contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName()); // Contact Name
values.put(KEY_PRICE, contact.getPhoneNumber()); // Contact Phone
values.put(KEY_QUAN, contact.getQuan()); // Contact Phone
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}
// Getting single contact
Coms getContact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_NAME, KEY_PRICE, KEY_QUAN }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Coms contact = new Coms(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2), cursor.getString(3));
// return contact
return contact;
}
Coms getName(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] {
KEY_NAME, KEY_PRICE, KEY_QUAN }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Coms contact = new Coms(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2), cursor.getString(3));
// return contact
return contact;
}
// Getting All Contacts
public List<Coms> getAllContacts() {
List<Coms> contactList = new ArrayList<Coms>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Coms contact = new Coms();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
//when this is changed from 2 to 3 it causes an error
contact.setQuan(cursor.getString(3));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
// Updating single contact
public int updateContact(Coms contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
values.put(KEY_PRICE, contact.getPhoneNumber());
values.put(KEY_QUAN, contact.getQuan());
// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
}
// Deleting single contact
public void deleteContact(Coms contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
db.close();
}
// Getting contacts Count
public int getContactsCount() {
String countQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}