Autore Topic: Database Dati cambiano posizione  (Letto 373 volte)

Offline Jekan

  • Nuovo arrivato
  • *
  • Post: 44
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    Umi Super
  • Sistema operativo:
    Windows 7
Database Dati cambiano posizione
« il: 25 Aprile 2017, 17:41:07 CEST »
0
Salve a tutti, come da titolo ho un problema con il mio db quando salvo i dati nel db i dati vengono salvati ma quando ricarico l'activity i dati si muovono di posizione, puo sembrare strano nn ho idea sono nuovo della programazzione.
Per esempio se inserisco nella edittext size quando ricarico l activity lo ritrovo nella edittext di dice o altri.
Spero qualcuno mi sappia aiutare grazie. :-) :-) :-)

db
Codice (Java): [Seleziona]
package jekan.mydb;

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;

/**
 * Created by obaro on 02/04/2015.
 */

public class ExampleDBHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "SQLiteExample.db";
    private static final int DATABASE_VERSION = 7;

    public static final String PERSON_TABLE_NAME = "person";
    public static final String PERSON_COLUMN_ID = "_id";
    public static final String PERSON_COLUMN_MONSTER_NAME = "name";
    //In verita è HIT DICE
    public static final String PERSON_COLUMN_MONSTER_GENDER = "gender";
    public static final String PERSON_COLUMN_MONSTER_INITIATIVE = "monster_initiative";
    public static final String PERSON_COLUMN_MONSTER_SPEED = "monster_speed";
    public static final String PERSON_COLUMN_MONSTER_ARMOR_CLASS = "monster_armor_class";
    public static final String PERSON_COLUMN_MONSTER_BASE_ATTACK = "monster_base_attack";
    public static final String PERSON_COLUMN_MONSTER_ATTACK = "monster_attack";
    public static final String PERSON_COLUMN_MONSTER_FULL_ATTACK = "monster_full_attack";
    public static final String PERSON_COLUMN_MONSTER_SPACE = "monster_space";
    public static final String PERSON_COLUMN_MONSTER_SPECIAL_ATTACK = "monster_special_attack";
    public static final String PERSON_COLUMN_MONSTER_SPECIAL_QUALITIES = "monster_special_qualities";
    public static final String PERSON_COLUMN_MONSTER_SAVE = "monster_save";
    public static final String PERSON_COLUMN_MONSTER_ABILITIE = "monster_abilitie";
    public static final String PERSON_COLUMN_MONSTER_SKILLS = "monster_skills";
    public static final String PERSON_COLUMN_MONSTER_FEATS = "monster_feats";
    public static final String PERSON_COLUMN_MONSTER_ENVIRONMNET = "monster_environmnet";
    public static final String PERSON_COLUMN_MONSTER_ORGANIZATION = "monster_organization";
    public static final String PERSON_COLUMN_MONSTER_CHALLENGE = "monster_challenge";
    public static final String PERSON_COLUMN_MONSTER_TREASURE = "monster_treasure";
    public static final String PERSON_COLUMN_MONSTER_ALIGMENT = "monster_aligment";
    public static final String PERSON_COLUMN_MONSTER_ADVANCEMENT = "monster_advancement";
    public static final String PERSON_COLUMN_MONSTER_LEVEL_ADJUSTMENT = "monster_level_adjustment";


    public static final String PERSON_COLUMN_MONSTER_SIZE = "size";

    public ExampleDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(
                "CREATE TABLE " + PERSON_TABLE_NAME +
                        "(" + PERSON_COLUMN_ID + " INTEGER PRIMARY KEY, " +
                        PERSON_COLUMN_MONSTER_NAME + " TEXT, " +
                        PERSON_COLUMN_MONSTER_GENDER + " TEXT, " +
                        PERSON_COLUMN_MONSTER_INITIATIVE + " TEXT, " +
                        PERSON_COLUMN_MONSTER_SPEED + " TEXT, " +
                        PERSON_COLUMN_MONSTER_ARMOR_CLASS + " TEXT, " +
                        PERSON_COLUMN_MONSTER_BASE_ATTACK + " TEXT, " +
                        PERSON_COLUMN_MONSTER_ATTACK + " TEXT, " +
                        PERSON_COLUMN_MONSTER_FULL_ATTACK + " TEXT, " +
                        PERSON_COLUMN_MONSTER_SPACE + " TEXT, " +
                        PERSON_COLUMN_MONSTER_SPECIAL_ATTACK + " TEXT, " +
                        PERSON_COLUMN_MONSTER_SPECIAL_QUALITIES + " TEXT, " +
                        PERSON_COLUMN_MONSTER_SAVE + " TEXT, " +
                        PERSON_COLUMN_MONSTER_ABILITIE + " TEXT, " +
                        PERSON_COLUMN_MONSTER_SKILLS + " TEXT, " +
                        PERSON_COLUMN_MONSTER_FEATS + " TEXT, " +
                        PERSON_COLUMN_MONSTER_ENVIRONMNET + " TEXT, " +
                        PERSON_COLUMN_MONSTER_ORGANIZATION + " TEXT, " +
                        PERSON_COLUMN_MONSTER_CHALLENGE + " TEXT, " +
                        PERSON_COLUMN_MONSTER_TREASURE + " TEXT, " +
                        PERSON_COLUMN_MONSTER_ALIGMENT + " TEXT, " +
                        PERSON_COLUMN_MONSTER_ADVANCEMENT + " TEXT, " +
                        PERSON_COLUMN_MONSTER_LEVEL_ADJUSTMENT + " TEXT, " +


                        PERSON_COLUMN_MONSTER_SIZE + " INTEGER)"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + PERSON_TABLE_NAME);
        onCreate(db);
    }

    public boolean insertPerson(String name_monster, String gender_monster, String size_monster
            , String initiative_monster, String speed_monster, String armor_class_monster, String base_attack_monster, String attack_monster
            , String full_attack_monster, String space_monster, String special_attack_monster, String special_qualities_monster, String save_monster, String abilities_monster
            , String skills_monster, String feats_monster, String environment_monster, String organization_monster, String challenger_monster, String treasure_monster
            , String aligment_monster, String advancement_monster, String level_adjustment_monster

    ) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(PERSON_COLUMN_MONSTER_NAME, name_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_GENDER, gender_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_INITIATIVE, initiative_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_SPEED, speed_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_ARMOR_CLASS, armor_class_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_BASE_ATTACK, base_attack_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_ATTACK, attack_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_FULL_ATTACK, full_attack_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_SPACE, space_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_SPECIAL_ATTACK, special_attack_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_SPECIAL_QUALITIES, special_qualities_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_SAVE, save_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_ABILITIE, abilities_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_SKILLS, skills_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_FEATS, feats_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_ENVIRONMNET, environment_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_ORGANIZATION, organization_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_CHALLENGE, challenger_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_TREASURE, treasure_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_ALIGMENT, aligment_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_ADVANCEMENT, advancement_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_LEVEL_ADJUSTMENT, level_adjustment_monster);


        contentValues.put(PERSON_COLUMN_MONSTER_SIZE, size_monster);
        db.insert(PERSON_TABLE_NAME, null, contentValues);
        return true;
    }

    public int numberOfRows() {
        SQLiteDatabase db = this.getReadableDatabase();
        int numRows = (int) DatabaseUtils.queryNumEntries(db, PERSON_TABLE_NAME);
        return numRows;
    }

    public boolean updatePerson(Integer id, String name_monster, String gender_monster, String size_monster
            , String initiative_monster, String speed_monster, String armor_class_monster, String base_attack_monster, String attack_monster
            , String full_attack_monster, String space_monster, String special_attack_monster, String special_qualities_monster, String save_monster, String abilities_monster
            , String skills_monster, String feats_monster, String environment_monster, String organization_monster, String challenger_monster, String treasure_monster
            , String aligment_monster, String advancement_monster, String level_adjustment_monster) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(PERSON_COLUMN_MONSTER_NAME, name_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_GENDER, gender_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_INITIATIVE, initiative_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_SPEED, speed_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_ARMOR_CLASS, armor_class_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_BASE_ATTACK, base_attack_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_ATTACK, attack_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_FULL_ATTACK, full_attack_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_SPACE, space_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_SPECIAL_ATTACK, special_attack_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_SPECIAL_QUALITIES, special_qualities_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_SAVE, save_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_ABILITIE, abilities_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_SKILLS, skills_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_FEATS, feats_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_ENVIRONMNET, environment_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_ORGANIZATION, organization_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_CHALLENGE, challenger_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_TREASURE, treasure_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_ALIGMENT, aligment_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_ADVANCEMENT, advancement_monster);
        contentValues.put(PERSON_COLUMN_MONSTER_LEVEL_ADJUSTMENT, level_adjustment_monster);


        contentValues.put(PERSON_COLUMN_MONSTER_SIZE, size_monster);
        db.update(PERSON_TABLE_NAME, contentValues, PERSON_COLUMN_ID + " = ? ", new String[]{Integer.toString(id)});
        return true;

    }


    public Integer deletePerson(Integer id) {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete(PERSON_TABLE_NAME,
                PERSON_COLUMN_ID + " = ? ",
                new String[]{Integer.toString(id)});
    }

    public Cursor getPerson(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res = db.rawQuery("SELECT * FROM " + PERSON_TABLE_NAME + " WHERE " +
                PERSON_COLUMN_ID + "=?", new String[]{Integer.toString(id)});
        return res;
    }

    public Cursor getAllPersons() {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res = db.rawQuery("SELECT * FROM " + PERSON_TABLE_NAME, null);
        return res;
    }
}

DisplayNote_monster

Codice (Java): [Seleziona]
package jekan.mydb;

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.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.sample.foo.sqliteexample.R;


public class CreateOrEditActivity extends ActionBarActivity implements View.OnClickListener {


    private ExampleDBHelper dbHelper ;
    EditText nameEditText_monster;
    EditText genderEditText_monster;

    EditText sizeEditText_monster;

    EditText initiativeEditText_monster;
    EditText speedEditText_monster;
    EditText armor_classEditText_monster;
    EditText base_attack_gEditText_monster;
    EditText attacEditText_monster;
    EditText full_attackEditText_monster;
    EditText space_reachEditText_monster;
    EditText special_attackEditText_monster;
    EditText special_qualitiesEditText_monster;
    EditText saveEditText_monster;
    EditText abilitiesEditText_monster;
    EditText skillsEditText_monster;
    EditText featsEditText_monster;
    EditText environmentEditText_monster;
    EditText organizationEditText_monster;
    EditText challenge_ratingEditText_monster;
    EditText treasureEditText_monster;
    EditText alignmentEditText_monster;
    EditText advancementEditText_monster;
    EditText level_adjustmentEditText_monster;

    Button saveButton;
    LinearLayout buttonLayout;
    Button editButton, deleteButton;

    int personID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        personID = getIntent().getIntExtra(MainActivity.KEY_EXTRA_CONTACT_ID, 0);

        setContentView(R.layout.activity_edit);
       
        nameEditText_monster = (EditText) findViewById(R.id.monster_editTextName);
        genderEditText_monster = (EditText) findViewById(R.id.monster_editTextGender);

        sizeEditText_monster = (EditText) findViewById(R.id.monster_editTextsize);

        initiativeEditText_monster = (EditText) findViewById(R.id.editText_monster_initiative);
        speedEditText_monster = (EditText) findViewById(R.id.editText_monster_speed);
        armor_classEditText_monster = (EditText) findViewById(R.id.editText_monster_armor_class);
        base_attack_gEditText_monster = (EditText) findViewById(R.id.editText_monster_base_attack_g);
        attacEditText_monster = (EditText) findViewById(R.id.editText_monster_attack);
        full_attackEditText_monster = (EditText) findViewById(R.id.editText_monster_full_attack);
        space_reachEditText_monster = (EditText) findViewById(R.id.editText_monster_space_reach);
        special_attackEditText_monster = (EditText) findViewById(R.id.editText_monster_special_attacks);
        special_qualitiesEditText_monster = (EditText) findViewById(R.id.editText_monster_special_qualities);
        saveEditText_monster = (EditText) findViewById(R.id.editText_monster_save);
        abilitiesEditText_monster = (EditText) findViewById(R.id.editText_monster_abilities);
        skillsEditText_monster = (EditText) findViewById(R.id.editText_monster_skills);
        featsEditText_monster = (EditText) findViewById(R.id.editText_monster_feats);
        environmentEditText_monster = (EditText) findViewById(R.id.editText_monster_environment);
        organizationEditText_monster = (EditText) findViewById(R.id.editText_monster_organization);
        challenge_ratingEditText_monster = (EditText) findViewById(R.id.editText_monster_challenge_rating);
        treasureEditText_monster = (EditText) findViewById(R.id.editText_monster_treasure);
        alignmentEditText_monster = (EditText) findViewById(R.id.editText_monster_alignment);
        advancementEditText_monster = (EditText) findViewById(R.id.editText_monster_advancement);
        level_adjustmentEditText_monster = (EditText) findViewById(R.id.editText_monster_level_adjustment);






        saveButton = (Button) findViewById(R.id.saveButton);
        saveButton.setOnClickListener(this);
        buttonLayout = (LinearLayout) findViewById(R.id.buttonLayout);
        editButton = (Button) findViewById(R.id.editButton);
        editButton.setOnClickListener(this);
        deleteButton = (Button) findViewById(R.id.deleteButton);
        deleteButton.setOnClickListener(this);

        dbHelper = new ExampleDBHelper(this);

        if(personID > 0) {
            saveButton.setVisibility(View.GONE);
            buttonLayout.setVisibility(View.VISIBLE);

            Cursor rs = dbHelper.getPerson(personID);
            rs.moveToFirst();
            String personName_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_NAME));
            String personGender_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_GENDER));
            String personsize_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_SIZE));

            String personinitiative_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_INITIATIVE));
            String personspeed_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_SPEED));
            String personarmor_class_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_ARMOR_CLASS));
            String personbase_attack_g_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_BASE_ATTACK));
            String personattack_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_ATTACK));
            String personfull_attack_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_FULL_ATTACK));
            String personspace_reach_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_SPACE));
            String personspecial_attack_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_SPECIAL_ATTACK));
            String personspecial_qualities_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_SPECIAL_QUALITIES));
            String personsave_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_SAVE));
            String personabilities_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_ABILITIE));
            String personskills_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_SKILLS));
            String personfeats_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_FEATS));
            String personenvironment_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_ENVIRONMNET));
            String personorganization_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_ORGANIZATION));
            String personchallenge_rating_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_CHALLENGE));
            String persontreasure_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_TREASURE));
            String personalignment_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_ALIGMENT));
            String personadvancement_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_ADVANCEMENT));
            String personlevel_adjustment_monster = rs.getString(rs.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_MONSTER_LEVEL_ADJUSTMENT));


            if (!rs.isClosed()) {
                rs.close();
            }

            nameEditText_monster.setText(personName_monster);
            nameEditText_monster.setFocusable(false);
            nameEditText_monster.setClickable(false);

            genderEditText_monster.setText((CharSequence) personGender_monster);
            genderEditText_monster.setFocusable(false);
            genderEditText_monster.setClickable(false);

            sizeEditText_monster.setText((CharSequence) personsize_monster);
            sizeEditText_monster.setFocusable(false);
            sizeEditText_monster.setClickable(false);

            initiativeEditText_monster.setText((CharSequence) personinitiative_monster);
            initiativeEditText_monster.setFocusable(false);
            initiativeEditText_monster.setClickable(false);

            speedEditText_monster.setText((CharSequence) personspeed_monster);
            speedEditText_monster.setFocusable(false);
            speedEditText_monster.setClickable(false);

            armor_classEditText_monster.setText((CharSequence) personarmor_class_monster);
            armor_classEditText_monster.setFocusable(false);
            armor_classEditText_monster.setClickable(false);

            base_attack_gEditText_monster.setText((CharSequence) personbase_attack_g_monster);
            base_attack_gEditText_monster.setFocusable(false);
            base_attack_gEditText_monster.setClickable(false);

            attacEditText_monster.setText((CharSequence) personattack_monster);
            attacEditText_monster.setFocusable(false);
            attacEditText_monster.setClickable(false);


            full_attackEditText_monster.setText((CharSequence) personfull_attack_monster);
            full_attackEditText_monster.setFocusable(false);
            full_attackEditText_monster.setClickable(false);

            space_reachEditText_monster.setText((CharSequence) personspace_reach_monster);
            space_reachEditText_monster.setFocusable(false);
            space_reachEditText_monster.setClickable(false);

            special_attackEditText_monster.setText((CharSequence) personspecial_attack_monster);
            special_attackEditText_monster.setFocusable(false);
            special_attackEditText_monster.setClickable(false);

            special_qualitiesEditText_monster.setText((CharSequence) personspecial_qualities_monster);
            special_qualitiesEditText_monster.setFocusable(false);
            special_qualitiesEditText_monster.setClickable(false);

            saveEditText_monster.setText((CharSequence) personsave_monster);
            saveEditText_monster.setFocusable(false);
            saveEditText_monster.setClickable(false);


            abilitiesEditText_monster.setText((CharSequence) personabilities_monster);
            abilitiesEditText_monster.setFocusable(false);
            abilitiesEditText_monster.setClickable(false);

            skillsEditText_monster.setText((CharSequence) personskills_monster);
            skillsEditText_monster.setFocusable(false);
            skillsEditText_monster.setClickable(false);

            featsEditText_monster.setText((CharSequence) personfeats_monster);
            featsEditText_monster.setFocusable(false);
            featsEditText_monster.setClickable(false);

            environmentEditText_monster.setText((CharSequence) personenvironment_monster);
            environmentEditText_monster.setFocusable(false);
            environmentEditText_monster.setClickable(false);

            organizationEditText_monster.setText((CharSequence) personorganization_monster);
            organizationEditText_monster.setFocusable(false);
            organizationEditText_monster.setClickable(false);

            challenge_ratingEditText_monster.setText((CharSequence) personchallenge_rating_monster);
            challenge_ratingEditText_monster.setFocusable(false);
            challenge_ratingEditText_monster.setClickable(false);

            treasureEditText_monster.setText((CharSequence) persontreasure_monster);
            treasureEditText_monster.setFocusable(false);
            treasureEditText_monster.setClickable(false);

            alignmentEditText_monster.setText((CharSequence) personalignment_monster);
            alignmentEditText_monster.setFocusable(false);
            alignmentEditText_monster.setClickable(false);

            advancementEditText_monster.setText((CharSequence) personadvancement_monster);
            advancementEditText_monster.setFocusable(false);
            advancementEditText_monster.setClickable(false);

            level_adjustmentEditText_monster.setText((CharSequence) personlevel_adjustment_monster);
            level_adjustmentEditText_monster.setFocusable(false);
            level_adjustmentEditText_monster.setClickable(false);

        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.saveButton:
                persistPerson();
                return;
            case R.id.editButton:
                saveButton.setVisibility(View.VISIBLE);
                buttonLayout.setVisibility(View.GONE);
                nameEditText_monster.setEnabled(true);
                nameEditText_monster.setFocusableInTouchMode(true);
                nameEditText_monster.setClickable(true);

                genderEditText_monster.setEnabled(true);
                genderEditText_monster.setFocusableInTouchMode(true);
                genderEditText_monster.setClickable(true);

                sizeEditText_monster.setEnabled(true);
                sizeEditText_monster.setFocusableInTouchMode(true);
                sizeEditText_monster.setClickable(true);

                initiativeEditText_monster.setEnabled(true);
                initiativeEditText_monster.setFocusableInTouchMode(true);
                initiativeEditText_monster.setClickable(true);

                speedEditText_monster.setEnabled(true);
                speedEditText_monster.setFocusableInTouchMode(true);
                speedEditText_monster.setClickable(true);

                armor_classEditText_monster.setEnabled(true);
                armor_classEditText_monster.setFocusableInTouchMode(true);
                armor_classEditText_monster.setClickable(true);

                base_attack_gEditText_monster.setEnabled(true);
                base_attack_gEditText_monster.setFocusableInTouchMode(true);
                base_attack_gEditText_monster.setClickable(true);

                attacEditText_monster.setEnabled(true);
                attacEditText_monster.setFocusableInTouchMode(true);
                attacEditText_monster.setClickable(true);

                full_attackEditText_monster.setEnabled(true);
                full_attackEditText_monster.setFocusableInTouchMode(true);
                full_attackEditText_monster.setClickable(true);

                space_reachEditText_monster.setEnabled(true);
                space_reachEditText_monster.setFocusableInTouchMode(true);
                space_reachEditText_monster.setClickable(true);

                special_attackEditText_monster.setEnabled(true);
                special_attackEditText_monster.setFocusableInTouchMode(true);
                special_attackEditText_monster.setClickable(true);

                special_qualitiesEditText_monster.setEnabled(true);
                special_qualitiesEditText_monster.setFocusableInTouchMode(true);
                special_qualitiesEditText_monster.setClickable(true);

                saveEditText_monster.setEnabled(true);
                saveEditText_monster.setFocusableInTouchMode(true);
                saveEditText_monster.setClickable(true);

                abilitiesEditText_monster.setEnabled(true);
                abilitiesEditText_monster.setFocusableInTouchMode(true);
                abilitiesEditText_monster.setClickable(true);

                skillsEditText_monster.setEnabled(true);
                skillsEditText_monster.setFocusableInTouchMode(true);
                skillsEditText_monster.setClickable(true);

                featsEditText_monster.setEnabled(true);
                featsEditText_monster.setFocusableInTouchMode(true);
                featsEditText_monster.setClickable(true);

                environmentEditText_monster.setEnabled(true);
                environmentEditText_monster.setFocusableInTouchMode(true);
                environmentEditText_monster.setClickable(true);

                organizationEditText_monster.setEnabled(true);
                organizationEditText_monster.setFocusableInTouchMode(true);
                organizationEditText_monster.setClickable(true);

                challenge_ratingEditText_monster.setEnabled(true);
                challenge_ratingEditText_monster.setFocusableInTouchMode(true);
                challenge_ratingEditText_monster.setClickable(true);

                treasureEditText_monster.setEnabled(true);
                treasureEditText_monster.setFocusableInTouchMode(true);
                treasureEditText_monster.setClickable(true);

                alignmentEditText_monster.setEnabled(true);
                alignmentEditText_monster.setFocusableInTouchMode(true);
                alignmentEditText_monster.setClickable(true);

                advancementEditText_monster.setEnabled(true);
                advancementEditText_monster.setFocusableInTouchMode(true);
                advancementEditText_monster.setClickable(true);

                level_adjustmentEditText_monster.setEnabled(true);
                level_adjustmentEditText_monster.setFocusableInTouchMode(true);
                level_adjustmentEditText_monster.setClickable(true);

                return;
            case R.id.deleteButton:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage(R.string.deletePerson)
                        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dbHelper.deletePerson(personID);
                                Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                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("Delete Person?");
                d.show();
                return;
        }
    }

    public void persistPerson() {
        if(personID > 0) {
            if(dbHelper.updatePerson(personID,
                    nameEditText_monster.getText().toString(),
                    genderEditText_monster.getText().toString(),
                    initiativeEditText_monster.getText().toString(),
                    speedEditText_monster.getText().toString(),
                    armor_classEditText_monster.getText().toString(),
                    base_attack_gEditText_monster.getText().toString(),
                    attacEditText_monster.getText().toString(),
                    full_attackEditText_monster.getText().toString(),
                    space_reachEditText_monster.getText().toString(),
                    special_qualitiesEditText_monster.getText().toString(),
                    special_attackEditText_monster.getText().toString(),
                    saveEditText_monster.getText().toString(),
                    abilitiesEditText_monster.getText().toString(),
                    skillsEditText_monster.getText().toString(),
                    featsEditText_monster.getText().toString(),
                    environmentEditText_monster.getText().toString(),
                    organizationEditText_monster.getText().toString(),
                    challenge_ratingEditText_monster.getText().toString(),
                    treasureEditText_monster.getText().toString(),
                    alignmentEditText_monster.getText().toString(),
                    advancementEditText_monster.getText().toString(),
                    level_adjustmentEditText_monster.getText().toString(),
                    sizeEditText_monster.getText().toString())) {
                Toast.makeText(getApplicationContext(), "Person Update Successful", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
            else {
                Toast.makeText(getApplicationContext(), "Person Update Failed", Toast.LENGTH_SHORT).show();
            }
        }
        else {
            if(dbHelper.insertPerson(nameEditText_monster.getText().toString(),
                    genderEditText_monster.getText().toString(),
                    initiativeEditText_monster.getText().toString(),
                    speedEditText_monster.getText().toString(),
                    armor_classEditText_monster.getText().toString(),
                    base_attack_gEditText_monster.getText().toString(),
                    attacEditText_monster.getText().toString(),
                    full_attackEditText_monster.getText().toString(),
                    space_reachEditText_monster.getText().toString(),
                    special_qualitiesEditText_monster.getText().toString(),
                    special_attackEditText_monster.getText().toString(),
                    saveEditText_monster.getText().toString(),
                    abilitiesEditText_monster.getText().toString(),
                    skillsEditText_monster.getText().toString(),
                    featsEditText_monster.getText().toString(),
                    environmentEditText_monster.getText().toString(),
                    organizationEditText_monster.getText().toString(),
                    challenge_ratingEditText_monster.getText().toString(),
                    treasureEditText_monster.getText().toString(),
                    alignmentEditText_monster.getText().toString(),
                    advancementEditText_monster.getText().toString(),
                    level_adjustmentEditText_monster.getText().toString(),
                    sizeEditText_monster.getText().toString()
                    )) {
                Toast.makeText(getApplicationContext(), "Person Inserted", Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(getApplicationContext(), "Could not Insert person", Toast.LENGTH_SHORT).show();
            }
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
    }
}

main

Codice (Java): [Seleziona]
package jekan.mydb;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.sample.foo.sqliteexample.R;


public class MainActivity extends ActionBarActivity {
    public final static String KEY_EXTRA_CONTACT_ID = "KEY_EXTRA_CONTACT_ID";

    private ListView listView;
    ExampleDBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.addNew);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, CreateOrEditActivity.class);
                intent.putExtra(KEY_EXTRA_CONTACT_ID, 0);
                startActivity(intent);
            }
        });

        dbHelper = new ExampleDBHelper(this);

        final Cursor cursor = dbHelper.getAllPersons();
        String [] columns = new String[] {
                ExampleDBHelper.PERSON_COLUMN_ID,
                ExampleDBHelper.PERSON_COLUMN_MONSTER_NAME
        };
        int [] widgets = new int[] {
                R.id.personID,
                R.id.personName
        };

        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.person_info,
                cursor, columns, widgets, 0);
        listView = (ListView)findViewById(R.id.listView1);
        listView.setAdapter(cursorAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                Cursor itemCursor = (Cursor) MainActivity.this.listView.getItemAtPosition(position);
                int personID = itemCursor.getInt(itemCursor.getColumnIndex(ExampleDBHelper.PERSON_COLUMN_ID));
                Intent intent = new Intent(getApplicationContext(), CreateOrEditActivity.class);
                intent.putExtra(KEY_EXTRA_CONTACT_ID, personID);
                startActivity(intent);
            }
        });

    }

}
« Ultima modifica: 29 Maggio 2017, 17:05:14 CEST da Jekan »

Offline Ohmnibus

  • Utente senior
  • ****
  • Post: 804
  • Respect: +168
    • Github
    • Google+
    • @ohmnibus
    • Mostra profilo
    • Lords of Knowledge GdR
  • Dispositivo Android:
    Huawei P9 Lite
  • Play Store ID:
    Ohmnibus
  • Sistema operativo:
    Windows 10 x64
Re:Database Dati cambiano posizione
« Risposta #1 il: 29 Maggio 2017, 17:25:21 CEST »
0
In dbHelper.updatePerson non rispetti l'ordine dei parametri. Il metodo riceve:
Integer id,
String name_monster,
String gender_monster,
String size_monster,
String initiative_monster

metre tu passi
personID,
nameEditText_monster.getText().toString(),
genderEditText_monster.getText().toString(),
initiativeEditText_monster.getText().toString(), //Invece di size_monster
speedEditText_monster.getText().toString(),  //Invece di initiative_monster

Per non impazzire ti consiglio di utilizzare una classe contenitore:

Codice (Java): [Seleziona]
public class Person {
  public String name;
  public String gender;
  public String size;
  //eccetera
}

e poi modifica i metodi che ricevono 150 parametri in modo da ricevere in input solo un'istanza di quella classe:

Codice (Java): [Seleziona]
public boolean updatePerson(long id, Person personToUpdate) {
  SQLiteDatabase db = this.getWritableDatabase();
  ContentValues contentValues = new ContentValues();
  contentValues.put(PERSON_COLUMN_MONSTER_NAME, personToUpdate.name);
  contentValues.put(PERSON_COLUMN_MONSTER_GENDER, personToUpdate.gender);
  contentValues.put(PERSON_COLUMN_MONSTER_INITIATIVE, personToUpdate.initiative);
  //Eccetera

  db.update(PERSON_TABLE_NAME, contentValues, PERSON_COLUMN_ID + " = ? ", new String[]{Integer.toString(id)});
  return true;
}

(fai lo stesso per insertPerson)

prima di chiamare insertPerson o updatePerson puoi caricare i dati da Activity a classe Person con un nuovo metodo:

Codice (Java): [Seleziona]
private Person readPersonFromUI() {
  Person person = new Person();
  person.name = nameEditText_monster.getText().toString();
  person.gender = genderEditText_monster.getText().toString();
  person.size = sizeEditText_monster.getText().toString();
  //Eccetera

  return person;
}

quindi puoi fare:

Codice (Java): [Seleziona]
Person person = readPersonFromUI();
if (dbHelper.insertPerson(person)) {
  //...
} else {
  //...
}

Codice (Java): [Seleziona]
Person person = readPersonFromUI();
if (dbHelper.updatePerson(personID, person)) {
  //...
} else {
  //...
}

Così il codice è molto più pulito e meno soggetto ad errori.
Ohmnibus
Le mie app su Play Store

È stata trovata una soluzione al tuo problema? Evidenzia il post più utile premendo . È un ottimo modo per ringraziare chi ti ha aiutato.

Offline Jekan

  • Nuovo arrivato
  • *
  • Post: 44
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    Umi Super
  • Sistema operativo:
    Windows 7
Re:Database Dati cambiano posizione
« Risposta #2 il: 29 Maggio 2017, 18:19:15 CEST »
0
Grazie mille  :-) :-) :-) :-)