0

我正在创建一个扫描条形码、将它们保存在数据库中并通过列表管理它们的应用程序。所以,这些是对我的问题感兴趣的课程

主要的

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

      Button addButton = (Button) findViewById (R.id.addMenuButton);
      addButton.setOnClickListener (new OnClickListener(){
          public void onClick (View v){
              startActivity(new Intent(CodiceBarreActivity.this, AggiungiCodiceActivity.class));
          }
          });

      Button listButton = (Button) findViewById (R.id.ViewlistButton);
      listButton.setOnClickListener (new OnClickListener(){
          public void onClick (View v){
              startActivity(new Intent(CodiceBarreActivity.this, ProdottiSelectionActivity.class));
          }
          });

  }   

static final class ProductData {
    long _id;
    String barcode;
    String format;
    String title;
    String price;
}

}

帮手

  private SQLiteDatabase db;

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

@Override
public void onCreate(SQLiteDatabase db) {
    StringBuilder sql = new StringBuilder();

    sql.append("create table ")
        .append(PRODUCT_TABLE)
        .append("(  ")
        .append("   _id integer primary key,")
        .append("   barcode text,")
        .append("   format text,")
        .append("   title text,")
        .append("   price text")
        .append(")  ");

    db.execSQL(sql.toString());    

    Log.d(TAG, PRODUCT_TABLE + "table created");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      db.execSQL("drop table if exists " + PRODUCT_TABLE);       
      Log.d(TAG, PRODUCT_TABLE + "table dropped");
        onCreate(db);
}

D b

private static final String PRODUCT_TABLE = "products"; 
    private static final BigDecimal ONE_HUNDRED = new BigDecimal("100");
    private SQLiteDatabase db;

    public CodiciDatabase(Context context) {
        ProductDatabaseHelper helper = new ProductDatabaseHelper(context);
        db = helper.getWritableDatabase();
    }

    public boolean insert(ProductData product) {
        ContentValues vals = new ContentValues();
        vals.put("_id", product._id);
        vals.put("barcode", product.barcode);
        vals.put("format", product.format);
        vals.put("title", product.title);
        vals.put("price", product.price);

        return db.insert(PRODUCT_TABLE, null, vals) != -1;
    }

感兴趣的班级

private ProductDatabaseHelper dbHelper;
private static final String PRODUCT_TABLE = "products"; 
ProductData product = new ProductData();

@Override
 protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dbHelper = new ProductDatabaseHelper(this);
    }

@Override
protected void onResume(){
    super.onResume();
    SQLiteDatabase db = dbHelper.getReadableDatabase();

        Cursor cursor = db.rawQuery("select * from " + PRODUCT_TABLE, null);

        setListAdapter(new CursorAdapter(this, cursor, true) {
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent){
            TextView textView = new TextView(ProdottiSelectionActivity.this);
            return textView;
        }

        @Override
        public void bindView (View view, Context context, Cursor cursor){

            TextView textView = (TextView) view;
            textView.append(cursor.getString(1));
            textView.append("\n");
            textView.append(cursor.getString(2));
            textView.append("\n");
            textView.append(cursor.getString(3));
            textView.append("\n");
            textView.append(cursor.getString(4));
            registerForContextMenu(textView); 
            }

        });
    }

@Override
protected void onDestroy(){
    super.onDestroy();
    dbHelper.close();
    }
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

}

}

我需要实现 onListItemClick 方法,以便您删除所选项目。我正在疯狂寻找正确的语法,希望您能帮助我!

4

2 回答 2

1

像这样的东西应该工作:

db.delete("products", "barcode" + "="+ barcodeToDelete, null);

将barcodeToDelte 替换为要从表中删除的条形码值。如果您不知道条形码的值,您也可以根据数据库中的任何其他字段将其删除,方法是在上述方法中将它们替换为“条形码”。

于 2012-06-15T21:04:13.620 回答
1

在我看来,您可能在“感兴趣的课程”中遇到错误。您的代码在某些地方有点混乱,我不确定您是否使用列表视图。但是使用列表视图并将其绑定到 onItemClickListener 会很有帮助。您将extends ListActivity在要显示项目的布局中使用,确保在列表视图布局中放置一个列表视图元素。然后填充您的列表视图,您将拥有:

        String[] tableColumnNames = new String[] {
                "TABLE COLUMN NAMES SEPARATED BY COMMAS" };
        int[] textViewLabelNames = new int[] { "NAMES OF TEXT VIEW LABELS IN XML ROW LAYOUT" };

        SimpleCursorAdapter stuff = new SimpleCursorAdapter(this,
                R.layout.your_created_xml_row_layout, cursor, tableColumnNames,
                textViewLabelNames);
        this.setListAdapter(stuff);

然后绑定你的 onClickListener:

    listView.setOnItemClickListener(new OnItemClickListener() {

        //ListView item is clicked
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


    }
    });

回顾您的代码:

onListItemClick is triggered您需要从数据库中删除与传递给 onListItemClick 方法的 id 对应的行时。然后,您需要使用 更新光标以反映视图上的更改cursor.requery()

因此,为了从具有特定 id 的数据库中删除项目,您可以执行以下操作:

ProductDatabaseHelper helper = new ProductDatabaseHelper(context);
SQLiteDatabase database = helper.getWritableDatabase();
database.delete("TABLE NAME HERE", "NAME OF ID COLUMN HERE" + "=" + id, null);
database.close();

您还可以查看SQLiteDatabase 文档以获取有关方法签名的有用描述。

于 2012-06-15T21:17:19.637 回答