通过长时间单击ListView从SQLite和ListView中删除其内容



任何人都可以通过长时间单击它向我展示如何从sqlite数据库中删除内容和列表视图吗?另外,我是否必须仅从sqlite数据库中删除内容,或者从sqlite数据库和listView?

中删除内容。

这是我的项目课程:

数据库助手

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 3;
    private static final String DATABASE_NAME = "products.db";
    private static final String TABLE_NAME = "products";
    private static final String COLUMN_ID = "_id";
    private static final String MARKET = "market";
    private static final String PRODUCT = "product";
    private SQLiteDatabase db;
    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME + "("
                + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + MARKET + " TEXT, "
                + PRODUCT + " TEXT)");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + "");
        onCreate(db);
    }
    public Cursor getRecords() {
        db = getReadableDatabase();
        return db.rawQuery(
                "SELECT * FROM " + TABLE_NAME,
                null);
    }
    public void addRecords(String market, String product) {
        db = getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(MARKET, market);
        contentValues.put(PRODUCT, product);
        db.insert(TABLE_NAME, null, contentValues);
        db.close();
    }
    public void deleteRecords(int id){
    }}

MainActivity

public class MainActivity extends AppCompatActivity {
    ListView lv;
    DatabaseHelper databaseHelper;
    ShoppingCartAdapter shoppingCartAdapter;
    private static final int TIME_ENTRY_REQUEST_CODE = 1;
    Cursor cursor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.list_view_main);
        databaseHelper = new DatabaseHelper(this);
        ListView listView = (ListView) findViewById(R.id.list_view_main);
        shoppingCartAdapter = new ShoppingCartAdapter(this, databaseHelper.getRecords(), CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        listView.setAdapter(shoppingCartAdapter);
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(final AdapterView<?> parent, View view, int position, long id) {
                final int pos = position;
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Are you sure you want to delete?");
                builder.setPositiveButton("DELETE", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                    }
                });
                builder.setNegativeButton("CANCEL", null);
                builder.show();
                return true;
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_template, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.menu_add_id) {
            Intent intent = new Intent(this, AddContentActivity.class);
            startActivityForResult(intent, TIME_ENTRY_REQUEST_CODE);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == TIME_ENTRY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                String market = data.getStringExtra("market");
                String product = data.getStringExtra("product");
                databaseHelper.addRecords(market, product);
                shoppingCartAdapter.changeCursor(databaseHelper.getRecords());
            }
        }
    }
}

addContentActivity

public class AddContentActivity extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addcontent);
    }
    public void onAddButton(View view){
        Intent intent = getIntent();
        EditText marketet = (EditText) findViewById(R.id.marketet_id);
        EditText productet = (EditText) findViewById(R.id.productet_id);
        intent.putExtra("market", marketet.getText().toString());
        intent.putExtra("product", productet.getText().toString());
        this.setResult(RESULT_OK, intent);
        finish();
    }
}

shoppingcartadapter

public class ShoppingCartAdapter extends CursorAdapter{
    public ShoppingCartAdapter(Context context, Cursor cursor, int flags){
        super(context, cursor, flags);
    }
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView marketTv = (TextView) view.findViewById(R.id.markettv_id);
        TextView productTv = (TextView) view.findViewById(R.id.producttv_id);
        marketTv.setText(cursor.getString(1));
        productTv.setText(cursor.getString(2));
    }
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.list_view_template, parent, false);
        return view;
    }
}

几天后,我在问题上找到了解决方案。如果有更好的删除方法,请发布答案。

我添加了以下代码行:

主要活动

Cursor cursor = (Cursor) parent.getItemAtPosition(pos);
final int item_id = cursor.getInt(cursor.getColumnIndex("_id"));
databaseHelper.deleteRecords(item_id );
cursor.requery();

databasehelper

public void deleteRecords(int id){
        db.delete(TABLE_NAME, COLUMN_ID + "=" + id, null);
    }

编辑:

替换cursor.requery();
shoppingCartAdapter.changeCursor(databaseHelper.getRecords());

因为使用requery();已弃用但两行代码工作。

相关内容

  • 没有找到相关文章

最新更新