バーコードをスキャンしてデータベースに保存し、リストで管理するアプリを作成しています。だから、これらは私の問題に興味を持っているクラスです
主要
@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);
}
db
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を実装する必要があります。正しい構文を探して夢中になっています。あなたが私を助けてくれることを願っています!