【发布时间】:2018-01-05 03:27:18
【问题描述】:
我想通过列表视图显示产品的名称、图片、描述、类别和价格。除了图像之外的所有内容都显示,因为我不知道如何。这些是我的代码:
MainActivity.java
public class CLARTIPS_Home extends AppCompatActivity {
private ListView lvProduct;
private ListProductAdapter adapter;
private List<Product> mProductList;
private DatabaseHelper mDBHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clartips__home);
lvProduct = (ListView) findViewById(R.id.listview_product);
mDBHelper = new DatabaseHelper(this);
//Button tryMe = (Button) findViewById(R.id.tryMe);
//Check exists database
File database = getApplicationContext().getDatabasePath(DatabaseHelper.DBNAME);
if(false==database.exists()) {
mDBHelper.getReadableDatabase();
//Copy db
if (copyDatabase(this)) {
Toast.makeText(this, "Success Copying Database", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Error Copying Database", Toast.LENGTH_SHORT).show();
return;
}
}
//Get product list in db when db exists
mProductList = mDBHelper.getListProduct();
//Init adapter
adapter = new ListProductAdapter(this,mProductList);
//Set adapter for listview
lvProduct.setAdapter(adapter);
}
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("MainActivity","DB copied");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
DatabaseHelper.java //数据库
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DBNAME = "sample.sqlite";
public static final String DBLOCATION = "/data/data/nerds.thesis.clartips/databases/";
private Context mContext;
private SQLiteDatabase mDatabase;
public DatabaseHelper(Context context) {
super(context, DBNAME, null, 1);
this.mContext = context;
}
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.getBlob(2), cursor.getInt(3), cursor.getString(4), cursor.getString(5));
productList.add(product);
cursor.moveToNext();
}
cursor.close();
closeDatabase();
return productList;
}
}
ListViewAdapter.java //适配器
public class ListProductAdapter extends BaseAdapter {
private Context mContext;
private List<Product> mProductList;
public ListProductAdapter(Context mContext, List<Product> mProductList) {
this.mContext = mContext;
this.mProductList = mProductList;
}
@Override
public int getCount() {
return mProductList.size();
}
@Override
public Object getItem(int position) {
return mProductList.get(position);
}
@Override
public long getItemId(int position) {
return mProductList.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = View.inflate(mContext, R.layout.listview, null);
TextView pName = (TextView)v.findViewById(R.id.product_name);
ImageView pImage = (ImageView)v.findViewById(R.id.product_image);
TextView pPrice = (TextView)v.findViewById(R.id.product_price);
TextView pDescription = (TextView)v.findViewById(R.id.product_description);
TextView pCategory = (TextView)v.findViewById(R.id.product_category);
pName.setText(mProductList.get(position).getName());
**//pImage.setImageBitmap(mProductList.get(position).getImage());**
pPrice.setText("$" + String.valueOf(mProductList.get(position).getPrice()));
pDescription.setText(mProductList.get(position).getDescription());
pCategory.setText(mProductList.get(position).getCategory());
return v;
}
}
Product.java //模型
public class Product{
private int id;
private String name;
private byte image;
private int price;
private String description;
private String category;
public Product(int id, String name, byte image, int price, String description, String category) {
this.id = id;
this.name = name;
this.image = image;
this.price = price;
this.description = description;
this.category = category;
}
public int getId(){
return id;
}
public String getName(){
return name;
}
public byte getImage(){
return image;
}
public int getPrice(){
return price;
}
public String getDescription(){
return description;
}
public String getCategory(){
return category;
}
}
为了线
**//pImage.setImageBitmap(mProductList.get(position).getImage());**
它不起作用并不断产生错误。我正在寻求任何替代方案。该错误表示它不适用于字节。请问我应该用什么代码替换该行才能显示我的图像?
【问题讨论】:
-
也许是
private byte[] image;? (以及所有其他提到 byte 的地方)。 -
我已经找到了答案:)
标签: java android image listview bitmap