2016-05-01 14 views
0

Ich versuche, die Daten aus meiner Datenbank und aus einer Listview basierend auf einem langen Druck oder Klick zu entfernen. Ich habe mir einige Tutorials angesehen, bin mir aber nicht sicher, wie ich das anstellen soll. irgendein Rat?Entfernen von Element aus einer ListView und Daten aus der Datenbank Android

hier ist mein Code so weit: Listview-Adapter Klasse:

public class AndroidListViewCursorAdaptorActivity extends Activity { 

private DBAdapter dbHelper; 
private SimpleCursorAdapter dataAdapter; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.workouts); 

    dbHelper = new DBAdapter(this); 
    dbHelper.open(); 



    //Generate ListView from SQLite Database 
    displayListView(); 

} 

private void displayListView() { 


    Cursor cursor = dbHelper.getAllRecords(); 

    // The desired columns to be bound 
    String[] columns = new String[] { 
      DBAdapter.KEY_TITLE, 
      DBAdapter.KEY_WORKOUTDATE, 
      DBAdapter.KEY_EXERCISE_NOTES, 

    }; 

    // the XML defined views which the data will be bound to 
    int[] to = new int[] { 
      R.id.title, 
      R.id.workoutDate, 
      R.id.workoutDetails, 

    }; 

    // create the adapter using the cursor pointing to the desired data 
    //as well as the layout information 
    dataAdapter = new SimpleCursorAdapter(
      this, R.layout.workout_info, 
      cursor, 
      columns, 
      to, 
      0); 

    ListView listView = (ListView) findViewById(R.id.listView1); 
    // Assign adapter to ListView 
    listView.setAdapter(dataAdapter); 

    } 


    lv.setOnItemLongClickListener(new OnItemLongClickListener() { 

    public boolean onItemLongClick(AdapterView<?> parent, View view, 
    int position, long arg3) { 

     dbHelper.deleteRow(); 
    } 
} 

    } 

und here meine DbAdapter Klasse:

public class DBAdapter { 

public static final String KEY_ROWID = "_id"; 
public static final String KEY_TITLE = "title"; 
public static final String KEY_WORKOUTDATE = "workoutDate"; 
public static final String KEY_EXERCISE_NOTES = "notes"; 

private static final String TAG = "WorkoutDBAdapter"; 
private DatabaseHelper mDBHelper; 
private SQLiteDatabase mdb; 

private static final String DATABASE_NAME = "WorkoutDB"; 
private static final String DATABASE_TABLE = "workouts"; 
private static final int DATABASE_VERSION = 2; 

private final Context mCtx; 

private static final String DATABASE_CREATE = 
     "create table if not exists workouts " + 
       "(_id integer primary key autoincrement, " + 
       "title VARCHAR not null, " + 
       "workoutDate date, " + 
       "notes VARCHAR);"; 

private static class DatabaseHelper extends SQLiteOpenHelper { 

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

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     Log.w(TAG, DATABASE_CREATE); 
     db.execSQL(DATABASE_CREATE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     Log.w(TAG, "Upgrading database from version " + oldVersion + " to " 
       + newVersion + ", which will destroy all old data"); 
     db.execSQL("DROP TABLE IF EXISTS workouts"); 
     onCreate(db); 
    } 
} 


public DBAdapter(Context ctx) { 
    this.mCtx = ctx; 
} 


public DBAdapter open() throws SQLException { 
    mDBHelper = new DatabaseHelper(mCtx); 
    mdb = mDBHelper.getWritableDatabase(); 
    return this; 
} 

public void close() { 
    if (mDBHelper != null) { 
     mDBHelper.close(); 
    } 
} 

//---insert a record into the database--- 
public long insertRecord(String title, String workoutdate, String notes) { 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(KEY_TITLE, title); 
    initialValues.put(KEY_WORKOUTDATE, workoutdate); 
    initialValues.put(KEY_EXERCISE_NOTES, notes); 

    return mdb.insert(DATABASE_TABLE, null, initialValues); 
} 


//---retrieves all the records--- 
public Cursor getAllRecords() { 
    Cursor mCursor = mdb.query(DATABASE_TABLE, new String[]{KEY_ROWID, KEY_TITLE, 
      KEY_WORKOUTDATE, KEY_EXERCISE_NOTES}, null, null, null, null, null); 

    if (mCursor != null) { 
     mCursor.moveToFirst(); 
    } 
    return mCursor; 

    public boolean deleteRow(long rowId) { 
    String where = KEY_ROWID + "=" + rowId; 
    return mdb.delete(DATABASE_TABLE, where, null) != 0; 
} 
} 
+0

Was 'EINIGE-DATEN [position] ist'? –

+0

Haben Sie versucht, eine Delete-Methode in Ihrer DatabaseHelper-Klasse zu implementieren? Ich würde dort anfangen –

+0

es ist nur ein Kommentar ich denke, Sie setzen die Daten dort Sie wollen gelöscht basierend auf der Position, aber ich bin nicht sicher, ob das richtig ist – Ryan159

Antwort

0

Ich habe mein Problem herausgefunden, hier arbeitet Code
hier ist die Klasse AndroidListViewCursorAdaptorActivity

public class AndroidListViewCursorAdaptorActivity extends Activity { 
private ListView lv; 
private DBAdapter dbHelper; 
private SimpleCursorAdapter dataAdapter; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.workouts); 

    dbHelper = new DBAdapter(this); 
    dbHelper.open(); 



    //Generate ListView from SQLite Database 
    displayListView(); 

    ListView listView = (ListView) findViewById(R.id.listView1); 
    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
     @Override 
     public boolean onItemLongClick(AdapterView<?> arg0, View arg1, 
             int pos, long id) { 
      Toast.makeText(getApplicationContext(), 

        "You have deleted a workout :)", Toast.LENGTH_LONG).show(); 

      Log.v("long clicked", "pos: " + pos); 
      dbHelper.deleteRow(id); 
      return true; 

     } 

    }); 
    displayListView(); 
} 

private void displayListView() { 


    Cursor cursor = dbHelper.getAllRecords(); 

    // The desired columns to be bound 
    String[] columns = new String[] { 
      DBAdapter.KEY_TITLE, 
      DBAdapter.KEY_WORKOUTDATE, 
      DBAdapter.KEY_EXERCISE_NOTES, 

    }; 

    // the XML defined views which the data will be bound to 
    int[] to = new int[] { 
      R.id.title, 
      R.id.workoutDate, 
      R.id.workoutDetails, 

    }; 

    // create the adapter using the cursor pointing to the desired data 
    //as well as the layout information 
    dataAdapter = new SimpleCursorAdapter(
      this, R.layout.workout_info, 
      cursor, 
      columns, 
      to, 
      0); 

    ListView listView = (ListView) findViewById(R.id.listView1); 
    // Assign adapter to ListView 
    listView.setAdapter(dataAdapter); 

    } 

} 

hier ist die DbAdapter Klasse:

public class DbAdapter {

public static final String KEY_ROWID = "_id"; 
public static final String KEY_TITLE = "title"; 
public static final String KEY_WORKOUTDATE = "workoutDate"; 
public static final String KEY_EXERCISE_NOTES = "notes"; 

private static final String TAG = "WorkoutDBAdapter"; 
private DatabaseHelper mDBHelper; 
private SQLiteDatabase mdb; 

private static final String DATABASE_NAME = "WorkoutDB"; 
private static final String DATABASE_TABLE = "workouts"; 
private static final int DATABASE_VERSION = 2; 

private final Context mCtx; 

private static final String DATABASE_CREATE = 
     "create table if not exists workouts " + 
       "(_id integer primary key autoincrement, " + 
       "title VARCHAR not null, " + 
       "workoutDate date, " + 
       "notes VARCHAR);"; 

private static class DatabaseHelper extends SQLiteOpenHelper { 

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

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     Log.w(TAG, DATABASE_CREATE); 
     db.execSQL(DATABASE_CREATE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     Log.w(TAG, "Upgrading database from version " + oldVersion + " to " 
       + newVersion + ", which will destroy all old data"); 
     db.execSQL("DROP TABLE IF EXISTS workouts"); 
     onCreate(db); 
    } 
} 


public DBAdapter(Context ctx) { 
    this.mCtx = ctx; 
} 


public DBAdapter open() throws SQLException { 
    mDBHelper = new DatabaseHelper(mCtx); 
    mdb = mDBHelper.getWritableDatabase(); 
    return this; 
} 

public void close() { 
    if (mDBHelper != null) { 
     mDBHelper.close(); 
    } 
} 

//---insert a record into the database--- 
public long insertRecord(String title, String workoutdate, String notes) { 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(KEY_TITLE, title); 
    initialValues.put(KEY_WORKOUTDATE, workoutdate); 
    initialValues.put(KEY_EXERCISE_NOTES, notes); 

    return mdb.insert(DATABASE_TABLE, null, initialValues); 
} 


//---retrieves all the records--- 
public Cursor getAllRecords() { 
    Cursor mCursor = mdb.query(DATABASE_TABLE, new String[]{KEY_ROWID, KEY_TITLE, 
      KEY_WORKOUTDATE, KEY_EXERCISE_NOTES}, null, null, null, null, null); 

    if (mCursor != null) { 
     mCursor.moveToFirst(); 
    } 
    return mCursor; 
} 

public boolean deleteRow(long rowId) { 
    String where = KEY_ROWID + "=" + rowId; 
    return mdb.delete(DATABASE_TABLE, where, null) != 0; 
} 
} 
Verwandte Themen