如何添加到列表顶部,如果相同,则仅通过数据库添加一次



嗨,堆栈溢出成员 我创建了一个应用程序,用户可以在其中将收藏夹项目添加到收藏夹列表中。 但是我面临的问题是,当我单击"添加到收藏夹"按钮时,该项目会添加到列表底部而不是顶部。

其次,当我多次单击同一项目添加到收藏夹项目时,这会多次添加该项目,而不仅仅是一次。 我怎样才能只将其添加到列表中一次而不是多次。这是我的活动和数据库代码。

我的数据库管理员.class

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

// TO USE:
// Change the package (at top) to match your project.
// Search for "TODO", and make the appropriate changes.
public class DBAdapter {
    /////////////////////////////////////////////////////////////////////
    //  Constants & Data
    /////////////////////////////////////////////////////////////////////
    // For logging:
    private static final String TAG = "DBAdapter";
    // DB Fields
    public static final String KEY_ROWID = "_id";
    public static final int COL_ROWID = 0;
    /*
     * CHANGE 1:
     */
    // TODO: Setup your fields here:
    public static final String KEY_NAME = "name";
    public static final String KEY_STUDENTNUM = "studentnum";
    public static final String KEY_FAVCOLOUR = "favcolour";
    // TODO: Setup your field numbers here (0 = KEY_ROWID, 1=...)
    public static final int COL_NAME = 1;
    public static final int COL_STUDENTNUM = 2;
    public static final int COL_FAVCOLOUR = 3;

    public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_NAME, KEY_STUDENTNUM, KEY_FAVCOLOUR};
    // DB info: it's name, and the table we are using (just one).
    public static final String DATABASE_NAME = "MyDb";
    public static final String DATABASE_TABLE = "mainTable";
    // Track DB version if a new version of your app changes the format.
    public static final int DATABASE_VERSION = 2;   
    private static final String DATABASE_CREATE_SQL = 
            "create table " + DATABASE_TABLE 
            + " (" + KEY_ROWID + " integer primary key autoincrement, "
            /*
             * CHANGE 2:
             */
            // TODO: Place your fields here!
            // + KEY_{...} + " {type} not null"
            //  - Key is the column name you created above.
            //  - {type} is one of: text, integer, real, blob
            //      (http://www.sqlite.org/datatype3.html)
            //  - "not null" means it is a required field (must be given a value).
            // NOTE: All must be comma separated (end of line!) Last one must have NO comma!!
            + KEY_NAME + " text not null, "
            + KEY_STUDENTNUM + " integer not null, "
            + KEY_FAVCOLOUR + " string not null"
            // Rest  of creation:
            + ");";
    // Context of application who uses us.
    private final Context context;
    private DatabaseHelper myDBHelper;
    private SQLiteDatabase db;
    /////////////////////////////////////////////////////////////////////
    //  Public methods:
    /////////////////////////////////////////////////////////////////////
    public DBAdapter(Context ctx) {
        this.context = ctx;
        myDBHelper = new DatabaseHelper(context);
    }
    // Open the database connection.
    public DBAdapter open() {
        db = myDBHelper.getWritableDatabase();
        return this;
    }
    // Close the database connection.
    public void close() {
        myDBHelper.close();
    }
    // Add a new set of values to the database.
    public long insertRow(String name, int studentNum, String favColour) {
        /*
         * CHANGE 3:
         */     
        // TODO: Update data in the row with new fields.
        // TODO: Also change the function's arguments to be what you need!
        // Create row's data:
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_NAME, name);
        initialValues.put(KEY_STUDENTNUM, studentNum);
        initialValues.put(KEY_FAVCOLOUR, favColour);
        // Insert it into the database.
        return db.insert(DATABASE_TABLE, null, initialValues);
    }
    // Delete a row from the database, by rowId (primary key)
    public boolean deleteRow(long rowId) {
        String where = KEY_ROWID + "=" + rowId;
        return db.delete(DATABASE_TABLE, where, null) != 0;
    }
    public void deleteAll() {
        Cursor c = getAllRows();
        long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
        if (c.moveToFirst()) {
            do {
                deleteRow(c.getLong((int) rowId));              
            } while (c.moveToNext());
        }
        c.close();
    }
    // Return all data in the database.
    public Cursor getAllRows() {
        String where = null;
        Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                            where, null, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }
    // Get a specific row (by rowId)
    public Cursor getRow(long rowId) {
        String where = KEY_ROWID + "=" + rowId;
        Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                        where, null, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }
    // Change an existing row to be equal to new data.
    public boolean updateRow(long rowId, String name, int studentNum, String favColour) {
        String where = KEY_ROWID + "=" + rowId;
        /*
         * CHANGE 4:
         */
        // TODO: Update data in the row with new fields.
        // TODO: Also change the function's arguments to be what you need!
        // Create row's data:
        ContentValues newValues = new ContentValues();
        newValues.put(KEY_NAME, name);
        newValues.put(KEY_STUDENTNUM, studentNum);
        newValues.put(KEY_FAVCOLOUR, favColour);
        // Insert it into the database.
        return db.update(DATABASE_TABLE, newValues, where, null) != 0;
    }

    /////////////////////////////////////////////////////////////////////
    //  Private Helper Classes:
    /////////////////////////////////////////////////////////////////////
    /**
     * Private class which handles database creation and upgrading.
     * Used to handle low-level database access.
     */
    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        @Override
        public void onCreate(SQLiteDatabase _db) {
            _db.execSQL(DATABASE_CREATE_SQL);           
        }
        @Override
        public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading application's database from version " + oldVersion
                    + " to " + newVersion + ", which will destroy all old data!");
            // Destroy old database:
            _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            // Recreate new database:
            onCreate(_db);
        }
    }
}

我最喜欢的列表活动..

DBAdapter myDb;
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.favoritediseases);
        openDB();
        populateListViewFromDB();
        registerListClickCallback();

         // this code is used for the action bar color change//
        ActionBar bar = getActionBar();
           bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#6B8E23")));
           getActionBar().setDisplayHomeAsUpEnabled(true);
           getActionBar().setHomeButtonEnabled(true); 
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();  
        closeDB();
    }
    private void openDB() {
        myDb = new DBAdapter(this);
        myDb.open();
    }
    private void closeDB() {
        myDb.close();
    }
    /* 
     * UI Button Callbacks
     */

    private void populateListViewFromDB() {
        Cursor cursor = myDb.getAllRows();
        // Allow activity to manage lifetime of the cursor.
        // DEPRECATED! Runs on the UI thread, OK for small/short queries.
        startManagingCursor(cursor);
        // Setup mapping from cursor to view fields:
        String[] fromFieldNames = new String[] 
                {DBAdapter.KEY_NAME, DBAdapter.KEY_STUDENTNUM};
        int[] toViewIDs = new int[]
                {R.id.item_name};
        // Create adapter to may columns of the DB onto elemesnt in the UI.
        SimpleCursorAdapter myCursorAdapter = 
                new SimpleCursorAdapter(
                        this,       // Context
                        R.layout.item_layout,   // Row layout template
                        cursor,                 // cursor (set of DB records to map)
                        fromFieldNames,         // DB Column names
                        toViewIDs               // View IDs to put information in
                        );
        // Set the adapter for the list view
        ListView myList = (ListView) findViewById(R.id.favlistView1);
        myList.setAdapter(myCursorAdapter);
    }
    private void registerListClickCallback() {
        ListView myList = (ListView) findViewById(R.id.favlistView1);

        //This code is for to delete the single item from the listview of favorite list
        myList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, final long arg3) {
                Cursor cursor = myDb.getRow(arg3);
                if (cursor.moveToFirst()) {

                        new AlertDialog.Builder(FavoriteDiseases.this)
                        .setTitle("Delete Item")
                        .setMessage("Do you want to delete this disease?")
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int which) { 
                                // continue with delete
                                myDb.deleteRow(arg3);
                                populateListViewFromDB();
                        }
                     })
                    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) { 
                            // do nothing
                        }
                     })
                     .show();            
    }
                return true;
            }
        });

        //this is the code used from starting activity from the favorite list database.
        myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View viewClicked, 
                    int position, long idInDB) {
                Cursor cursor = myDb.getRow(idInDB);
                if (cursor.moveToFirst()) {
                    String name = cursor.getString(DBAdapter.COL_NAME);
                    if (name.equals("Atherosclerosis")){
                        startActivity(new Intent(FavoriteDiseases.this,Atherosclerosis.class));
                    }else if
                     (name.equals("Coronary Heart Disease")){
                        startActivity(new Intent(FavoriteDiseases.this,CoronaryHeartDisease.class));
                    }else if (name.equals("Stable Angina")){
                        startActivity(new Intent(FavoriteDiseases.this,StableAngina.class));
                    }else if (name.equals("Acute Coronary Syndrome")){
                            startActivity(new Intent(FavoriteDiseases.this,AcuteCoronarySyndrome.class));
                    }else if (name.equals("Myocardial Infarction")){
                        startActivity(new Intent(FavoriteDiseases.this,MyocardialInfarction.class));
                    }else if (name.equals("Unstable Angina")){
                        startActivity(new Intent(FavoriteDiseases.this,UnstableAngina.class));
                    }else if (name.equals("Acute Heart Failure")){
                    startActivity(new Intent(FavoriteDiseases.this,AcuteHeartFailure.class));
                }
                }
                cursor.close();
                updateItemForId(idInDB);
            }   
            });}

    private void updateItemForId(long idInDB) {
        Cursor cursor = myDb.getRow(idInDB);
        if (cursor.moveToFirst()) {
            cursor.getLong(DBAdapter.COL_ROWID);
            String name = cursor.getString(DBAdapter.COL_NAME);
            int studentNum = cursor.getInt(DBAdapter.COL_STUDENTNUM);
            String favColour = cursor.getString(DBAdapter.COL_FAVCOLOUR);
            favColour += "!";
            myDb.updateRow(idInDB, name, studentNum, favColour);
        }
        cursor.close();
        populateListViewFromDB();       
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.favorite_diseases, menu);
        return true;
    }
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         // Take appropriate action for each action item click
         switch (item.getItemId()) {
         case R.id.action_clear:
            new AlertDialog.Builder(this)
                .setTitle("Delete List")
                .setMessage("Do you want to clear all?")
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) { 
                        // continue with delete
                        myDb.deleteAll();
                        populateListViewFromDB();
                    }
                 })
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) { 
                        // do nothing
                    }
                 })
                 .show();
             return true;
}
        return true;
     }}

我添加到列表中的最终代码

case R.id.id_favorit:
// Add it to the DB and re-draw the ListView
                myDb.insertRow("Atherosclerosis", 0, "");
                Toast.makeText(getApplicationContext(), "Item Added to Favorite List!", Toast.LENGTH_SHORT).show(); 
                favClicked=true;
                invalidateOptionsMenu();
               return true;

似乎您只想以相反的顺序从数据库中获取结果。将检索它们的函数更改为按降序排列的 ORDER BY 您的 rowid 将完成以下伎俩:

// Return all data in the database.
public Cursor getAllRows() {
    String where = null;
    Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                        where, null, null, null, KEY_ROWID + " DESC", null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

关于您的第二个问题,您应该在插入之前检查收藏夹是否存在。问题是你如何定义你的独特性。例如,如果您不想要两个同名的"收藏夹",则需要在插入函数中添加一个复选标记:

// Add a new set of values to the database, unless the name already exists.
public long insertRow(String name, int studentNum, String favColour) {
    Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                    KEY_NAME + "='" + name + "'", null, null, null, null, null);
    if (c.getCount() > 0) {
        return -1;
    }
    c.close();
    /*
     * CHANGE 3:
     */     
    // TODO: Update data in the row with new fields.
    // TODO: Also change the function's arguments to be what you need!
    // Create row's data:
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_NAME, name);
    initialValues.put(KEY_STUDENTNUM, studentNum);
    initialValues.put(KEY_FAVCOLOUR, favColour);
    // Insert it into the database.
    return db.insert(DATABASE_TABLE, null, initialValues);
}

最新更新