2

I have a problem which can't cope. I downloading data from the database itself about the players, two columns, id and the player's name (in DatabaseHelper class):

public ArrayList<HashMap<String, String>> getAllPlayers() {
    try {
        ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;
        SQLiteDatabase db = this.getReadableDatabase();
        String sqlSelect = "SELECT " + _id + ", " +
                columnPlayerName  + " FROM " + TABLE_NAME;
        Cursor select = db.rawQuery(sqlSelect, null);
        if(select != null) {
            if(select.moveToFirst()) {
                do {
                    map = new HashMap<String, String>();
                    map.put("_id", select.getString(0));
                    map.put("PLAYER_NAME", select.getString(1));
                    arraylist.add(map);
                }
                while(select.moveToNext());
            }
        }
        select.close();
        db.close();
        return arraylist;
    }
    catch (Exception e) {
        return null;
    }
}

Ok, then downloaded the data displayed in a list in two columns (MainActivity class):

public class MainActivity extends AppCompatActivity {

ListView playersList;
DatabaseHelperPlayer playerDb = new DatabaseHelperPlayer(this);


@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_players_list_view, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
        case R.id.delete_id:
            TextView tv = (TextView) findViewById(R.id.playerNameListView);
            String n = tv.getText().toString();
            playerDb.deletePlayer(info.id);



    }
    return super.onContextItemSelected(item);
}

private void playersListView() {
    ArrayList<HashMap<String, String>> playerList = playerDb.getAllPlayers();
    playersList = (ListView) findViewById(R.id.listPlayer);
    SimpleAdapter adt = new SimpleAdapter(this, playerList, R.layout.row_players, new String[]{"_id", "PLAYER_NAME"}, new int[]{R.id.id, R.id.playerNameListView});
    playersList.setAdapter(adt);
}


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

    playersListView();
    registerForContextMenu(playersList);

}

}

When you hold on an item on the list, shows the menu when I select delete, run the method that I should remove the selected row in the list from the database. Unfortunately, this is not happening. I can not deal with it. Please help.

Method delete:

public Cursor deletePlayer(long id) {
    SQLiteDatabase db = this.getWritableDatabase();
    String sql = "DELETE FROM " + TABLE_NAME + " WHERE " + columnPlayerName + " = " + id;
    Cursor delete = db.rawQuery(sql, null);
    if(delete != null) {
        delete.moveToFirst();
    }
    return delete;
}
1
  • Libraries like Realm and ActiveAndroid can make your work so much easier; just suggesting Commented Jan 14, 2016 at 22:30

1 Answer 1

2

Don't use rawQuery to execute a deletion. Try this:

db.execSQL(sql);

instead of

Cursor delete = db.rawQuery(sql, null);

Or use the db.delete method directly. See here for API: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.