mardi 29 mars 2016

Using Listview and OnItemClick with SQLite to Load Data

I'm having trouble loading data from my ListView that I've populated with my CharacterSheetDBHelper. I've tried searching for several answers, one including using SimpleCursorAdapter, but I'm still having trouble. Can someone steer me into the right direction for this? I want to click on a list item and then fill out the form with the data stored on the database for editing.

My Code below:

The LoadCharacter class

public class LoadCharacter extends AppCompatActivity {
TextView testView;
ListView charListView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.load_character);
    CharacterDBHelper db = new CharacterDBHelper(getApplicationContext());
    charListView = (ListView) findViewById(R.id.charListView);

    //get list of names from the Database helper.
    List<String> names = new ArrayList<>(db.getNames());

    //attempting to create a listAdapter
    ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, names);
    charListView.setAdapter(adapter);
    charListView.setTextFilterEnabled(true);
    charListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override

        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent passIntent = new Intent(getApplicationContext(), CreateCharacter.class);

            //Logically onItemClick would open up a game in progress rather than the character sheet screen.
            //I was going to load character data into the Create Character class as an example.
            //This is not working right now.
            //Cursor c=  (Cursor)charListView.getItemAtPosition(position);
            //passIntent.putExtra("Characters", c.getColumn);
            startActivity(passIntent);

        }
    });

}
public boolean onCreateOptionsMenu(Menu menu){
    menu.add(0,0,0, "New Character");
    return(super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId()==0){
        Intent intent = new Intent(this, CreateCharacter.class);
        startActivity(intent);
    }

    return super.onOptionsItemSelected(item);
   }
}

My CharacterDBHelper Class:

public class CharacterDBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "char.db";
private static final int DATABASE_VERSION = 1;
private SQLiteDatabase charDB = null;

public CharacterDBHelper(Context context) {
    super(context, DB_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    //Create database
    String CREATE_CHAR_TABLE = "CREATE TABLE IF NOT EXISTS Characters(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, " +
            "brawling TEXT NOT NULL, highflying TEXT NOT NULL, technical TEXT NOT NULL, startinghealth TEXT NOT NULL," +
            "remainingpoints TEXT NOT NULL)";
    db.execSQL(CREATE_CHAR_TABLE);

}

public List<String> getNames(){
    List<String> names = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    //Read from database and get ALL entries by name.
    Cursor cursor = db.rawQuery("SELECT * FROM Characters", null);

    if (cursor.moveToFirst()){
        do {
            //add extracted names to array.
            names.add(cursor.getString(cursor.getColumnIndex("name")));
        }while(cursor.moveToNext());
    }
    //close cursor and database.
    cursor.close();
    db.close();

    return names;
}

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

public void insertData(String name, String brawl, String flying, String tech, String health, String points){
    SQLiteDatabase charDB = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("name", name);
    values.put("brawling", brawl);
    values.put("highflying", flying);
    values.put("technical", tech);
    values.put("startinghealth", health);
    values.put("remainingpoints", points);

    //row insert
    charDB.insert("Characters", null, values);
    charDB.close();
}
}

Aucun commentaire:

Enregistrer un commentaire