0

所以,问题来了,我有两个班级,班级标题是一个人的信息,即姓名、地址、网站、电话等,当这个人选择保存按钮时,他的所有个人数据都保存在 SQlite 中,他被转移到班级具有编辑文本视图的新闻场景,人们在该视图上撰写故事。当他写这个故事时,我希望它能够根据最近保存的标题的 id 来保存。但是程序没有这样做,而是崩溃并给了我错误。这是我的标题类的代码,我从中将一个人的详细信息插入数据库

EditText title=(EditText)findViewById(R.id.editText1);
                EditText author=(EditText)findViewById(R.id.editText2);
                EditText company=(EditText)findViewById(R.id.editText3);
                EditText address=(EditText)findViewById(R.id.editText4);
                EditText email=(EditText)findViewById(R.id.editText5);
                EditText website=(EditText)findViewById(R.id.editText6);
                EditText phone=(EditText)findViewById(R.id.editText7);
                title1=title.getText().toString();
                author1=author.getText().toString();
                company1=company.getText().toString();
                address1=address.getText().toString();
                email1=email.getText().toString();
                website1=website.getText().toString();
                phone1=phone.getText().toString();

                DatabaseHandler db = new DatabaseHandler(this);

                /**
                 * CRUD Operations
                 * */
                // Inserting Contacts
                Log.d("Insert: ", "Inserting ..");
                db.addContact(new Contact(0, title1, author1,company1, address1, email1, website1, phone1, script1));

                // Reading all contacts
                Log.d("Reading: ", "Reading all contacts..");
                List<Contact> contacts = db.getAllContacts();

                for (Contact cn : contacts) {
                    String log = "Id: "+cn.getID()+" ,Name: " + cn.get_name() + " ,Author: " + cn.get_author() + " ,company: " + cn.get_company() + " ,address: " + cn.get_address()
                    + " ,email: " + cn.get_email() + " ,Website: " + cn.get_website() + " ,Phone: " + cn.getPhoneNumber() + " ,Script: " + cn.getscript();
                        // Writing Contacts to log
                Log.d("Name: ", log);
                name_title=cn.getName().toString();
                }  

                        //Passing id through intent 
                Intent i = new Intent(Title.this,Newscene.class);
                i.putExtra("EXTRA_ID", id );
                startActivity(i);

这是我在新闻场景类中得到它的地方

        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_newscene);

                Intent i = getIntent();
                idval = i.getStringExtra("EXTRA_ID").toString();
                // i am converting this idval string to integer below

                }

然后在同一个班级,一个人在edittext中写的所有内容都会立即保存在

         String strSaveindb = previous + scene1 + ending;

然后我在我的联系人类中设置它并像这样更新数据库(这是在新闻场景活动中完成的)

            cn.setscript(strSaveindb);
            db.updateContactscript(db.getContact(1));

现在在我的 Databasehandler 类中,我有 updatecontact 函数和 updatecontactscript 函数,以下是我的数据库处理程序类的代码

public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY,"
                + KEY_NAME + " TEXT,"
                + KEY_AUTHOR + " TEXT,"
                + KEY_COMPANY + " TEXT,"
                + KEY_ADDRESS + " TEXT,"
                + KEY_EMAIL + " TEXT,"
                + KEY_WEBSITE + " TEXT,"
                + KEY_PH_NO + " TEXT,"
                + KEY_SCRIPT + "TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

这是添加联系人功能

void addContact(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, contact.getName()); // Contact Name
        values.put(KEY_AUTHOR, contact.getPhoneNumber());
        values.put(KEY_COMPANY, contact.getPhoneNumber());
        values.put(KEY_ADDRESS, contact.getPhoneNumber());
        values.put(KEY_EMAIL, contact.getPhoneNumber());
        values.put(KEY_WEBSITE, contact.getPhoneNumber());
        values.put(KEY_PH_NO, contact.getPhoneNumber());
        values.put(KEY_SCRIPT, contact.getscript());// Contact Phone

        // Inserting Row
        db.insert(TABLE_CONTACTS, null, values);
        db.close(); // Closing database connection
    }

这是获取联系功能

    Contact getContact(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
                KEY_NAME, KEY_AUTHOR, KEY_COMPANY, KEY_ADDRESS, KEY_EMAIL, KEY_WEBSITE, KEY_PH_NO , KEY_SCRIPT }, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1), cursor.getString(2),cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6),cursor.getString(7),cursor.getString(8));
        // return contact
        return contact;
    }

这些是更新联系人的功能,并且只更新脚本

    // Updating single contact
    public int updateContact(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, contact.getName());
        values.put(KEY_AUTHOR, contact.get_author());
        values.put(KEY_COMPANY, contact.get_company());
        values.put(KEY_ADDRESS, contact.get_address());
        values.put(KEY_EMAIL, contact.get_email());
        values.put(KEY_WEBSITE, contact.get_website());
        values.put(KEY_PH_NO, contact.getPhoneNumber());
        values.put(KEY_SCRIPT, contact.getscript());

        // updating row
        return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
                new String[] { String.valueOf(contact.getID()) });
    }

    //UPDATING SCRIPT

    public int updateContactscript(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(KEY_SCRIPT, contact.getscript());

        // updating row
        return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
                new String[] { String.valueOf(contact.getID()) });
    }

我如何更新最近创建的联系人 ID 前面的脚本。我现在被困在这里两天了:(

4

1 回答 1

1

修复这一行:

KEY_SCRIPT + "TEXT" 

KEY_SCRIPT + " TEXT"
于 2014-02-19T01:09:52.973 回答