0

我使用 ZXing 库创建了一个用于扫描条形码和二维码的应用程序。我还实现了一个database存储扫描产品的方法。我需要实现一个listview来显示存储的产品。有任何想法吗?

这里是类:

条码活动

  @Override
  public void onCreate (Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.spot_pay);
      Button addButton = (Button) findViewById (R.id.addMenuButton);
      addButton.setOnClickListener (new OnClickListener(){
          public void onClick (View v){
              startActivity(new Intent(CodiceBarreActivity.this, AggiungiCodiceActivity.class));
          }
          });
  }   
static final class ProductData {
    String barcode;
    String format;
    String title;
    BigDecimal price;
  }
}

产品数据库:

 private SQLiteDatabase db;

    private static class ProductDatabaseHelper extends SQLiteOpenHelper {

        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 currency")
                .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);
        }
    }

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

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

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

添加产品

    private static final int REQUEST_BARCODE = 0;
    private static final ProductData mProductData = new ProductData();





    private EditText mBarcodeEdit;
    private EditText mFormatEdit;
    private EditText mTitleEdit;
    private EditText mPriceEdit;
    private Button mScanButton;
    private Button mAddButton;
    private CodiciDatabase mProductDb;

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

        mBarcodeEdit = (EditText) findViewById(R.id.barcodeEdit);
        mFormatEdit = (EditText) findViewById(R.id.codeFormatEdit);
        mTitleEdit = (EditText) findViewById(R.id.titleEdit);
        mPriceEdit = (EditText) findViewById(R.id.priceEdit);
        mScanButton = (Button) findViewById(R.id.scanButton);
        mScanButton.setOnClickListener(this);
        mAddButton = (Button) findViewById(R.id.addButton);
        mAddButton.setOnClickListener(this);
        mProductDb = new CodiciDatabase(this); // not yet shown
    }




    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.scanButton:
            Intent intent = new Intent ("com.google.zxing.client.android.SCAN");
            intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
            startActivityForResult(intent, REQUEST_BARCODE);
            break;

        case R.id.addButton:
            String barcode = mBarcodeEdit.getText().toString();
            String format = mFormatEdit.getText().toString();
            String title = mTitleEdit.getText().toString();
            String price = mPriceEdit.getText().toString();

            String errors = validateFields(barcode, format, title, price);
            if (errors.length() > 0) {
                showInfoDialog(this, "Please fix errors", errors);
            } else {
                mProductData.barcode = barcode;
                mProductData.format = format;
                mProductData.title = title;
                mProductData.price = new BigDecimal(price);

                mProductDb.insert(mProductData);
                showInfoDialog(this, "Success", "Product saved successfully");
                resetForm();
            }
            break;
        }   
    }

        }


    private void resetForm() {
        mBarcodeEdit.getText().clear();
        mFormatEdit.getText().clear();
        mTitleEdit.getText().clear();
        mPriceEdit.getText().clear();

    }


    private void showInfoDialog(Context context, String title, String information) {
        new AlertDialog.Builder (context)
        .setMessage(information)
        .setTitle(title)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {


            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

            }
        }).show();  
    }


    public void onActivityResult(int requestCode, int resultCode, Intent intent){
        if (requestCode == REQUEST_BARCODE){
            if (resultCode == RESULT_OK) {
                String barcode = intent.getStringExtra("SCAN_RESULT");
                mBarcodeEdit.setText(barcode);

                String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                mFormatEdit.setText(format);
            } else if (resultCode == RESULT_CANCELED){
                finish();
            }

        }
    }
}

    private static String validateFields(String barcode, String format, 
            String title, String price) {
            StringBuilder errors = new StringBuilder();

            if (barcode.matches("^\\s*$")) {
                errors.append("Barcode required\n");
            }

            if (format.matches("^\\s*$")) {
                errors.append("Format required\n");
            }

            if (title.matches("^\\s*$")) {
                errors.append("Title required\n");
            }

            if (!price.matches("^-?\\d+(.\\d+)?$")) {
                errors.append("Need numeric price\n");
            }

            return errors.toString();
        }
}
4

1 回答 1

0

您需要做的概述:

在您的数据库上运行一个查询,该查询将返回一个光标给您。一旦你得到了,你将不得不制作一个CursorAdapter并覆盖它的 getView() 方法来膨胀和填充行视图。之后,您可以使用传入适配器实例的 ListView.setAdapter() 方法。每当有新数据时,它将为您处理更新屏幕上的列表。

我建议不要尝试在你自己的项目中解决这个问题,而是休息一下,然后从开发人员文档中学习这个记事本教程。它非常小而且简单,但是一旦你完成了,当你为你的条形码应用程序做这件事时,你将有一些示例代码可以使用。

于 2012-06-15T02:48:07.257 回答