这是我的主要产品:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lvProduct = (ListView) findViewById(R.id.listview_product);
mDBHelper = new DatabaseHelper(this);
//Check exists database
File database = getApplicationContext().getDatabasePath(DatabaseHelper.DBNAME);
if (false == database.exists()) {
mDBHelper.getReadableDatabase();
//Copy db
if (copyDatabase(this)) {
Toast.makeText(this, "Copy database success", LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Copy data error", LENGTH_SHORT).show();
return;
}
}
//Get product list in db when db exists
mProductList = mDBHelper.getListProduct();
//Init adapter
adapter = new ProductListAdapter(this, mProductList);
//Set adapter for listview
lvProduct.setAdapter(adapter);
spinners = (Spinner) findViewById(R.id.spinnertype);
final ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.type_arrays, R.layout.support_simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinners.setAdapter(adapter);
spinners.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String[] dataArray = getResources().getStringArray(R.array.type_arrays);
String type = dataArray[pos];
Toast.makeText(getApplicationContext(), "Type Selected : " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private boolean copyDatabase(Context context) {
try {
InputStream inputStream = context.getAssets().open(DatabaseHelper.DBNAME);
String outFileName = DatabaseHelper.DBLOCATION + DatabaseHelper.DBNAME;
OutputStream outputStream = new FileOutputStream(outFileName);
byte[] buff = new byte[1024];
int length = 0;
while ((length = inputStream.read(buff)) > 0) {
outputStream.write(buff, 0, length);
}
outputStream.flush();
outputStream.close();
Log.w("MainProduct", "DB copied");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
这是我的databasehelper:
public DatabaseHelper(Context context) {
super(context, DBNAME, null, 1);
this.mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void openDatabase() {
String dbPath = mContext.getDatabasePath(DBNAME).getPath();
if (mDatabase != null && mDatabase.isOpen()) {
return;
}
mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
}
public void closeDatabase() {
if (mDatabase != null) {
mDatabase.close();
}
}
public List<Product> getListProduct() {
Product product = null;
List<Product> productList = new ArrayList<>();
openDatabase();
Cursor cursor = mDatabase.rawQuery("SELECT * FROM Product", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
product = new Product(cursor.getInt(0), cursor.getString(1), cursor.getDouble(2), cursor.getString(3));
productList.add(product);
cursor.moveToNext();
}
cursor.close();
closeDatabase();
return productList;
}
}
要过滤listview,我是否需要有额外的java类(过滤器列表适配器)或将某个代码添加到'OnItemSelectedListener'? 请帮助我,这是帮助我完成项目的最后一件事。我需要编辑的代码。我的微调器字符串是一个数组,我的sqlite数据库中有'type'列,我的create语句是
CREATE TABLE“Product”(ID
INTEGER NOT NULL,NAME
TEXT,PRICE
DECIMAL(10,3)DEFAULT(null),DESC
TEXT,{{ 1}} TEXT,TYPE
BLOB,PRIMARY KEY(IMAGE
))
希望任何人都可以帮助我。提前谢谢。