Android实现水果列表,Android ListView與SQLite綜合使用(水果商品展示案例)
时间: 2024-03-14 10:20:27 浏览: 74
Android利用listview控件操作SQLite数据库实例
要实现一个水果列表,我们可以使用Android中的ListView和SQLite来完成。
首先,我们需要创建一个SQLite数据库来存储水果的信息,包括名称、价格和图片等。我们可以使用SQLiteOpenHelper类来创建和管理数据库。以下是一个简单的示例:
```java
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "fruit.db";
private static final int DATABASE_VERSION = 1;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS fruit("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "name TEXT,"
+ "price REAL,"
+ "image BLOB)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS fruit");
onCreate(db);
}
}
```
然后,我们需要创建一个适配器来将数据显示在ListView中。我们可以继承BaseAdapter类来创建适配器。以下是一个简单的示例:
```java
public class FruitAdapter extends BaseAdapter {
private Context mContext;
private List<Fruit> mFruitList;
public FruitAdapter(Context context, List<Fruit> fruitList) {
mContext = context;
mFruitList = fruitList;
}
@Override
public int getCount() {
return mFruitList.size();
}
@Override
public Object getItem(int position) {
return mFruitList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder holder;
if (convertView == null) {
view = LayoutInflater.from(mContext).inflate(R.layout.fruit_item, parent, false);
holder = new ViewHolder();
holder.imageView = view.findViewById(R.id.fruit_image);
holder.nameTextView = view.findViewById(R.id.fruit_name);
holder.priceTextView = view.findViewById(R.id.fruit_price);
view.setTag(holder);
} else {
view = convertView;
holder = (ViewHolder) view.getTag();
}
Fruit fruit = mFruitList.get(position);
holder.imageView.setImageBitmap(fruit.getImage());
holder.nameTextView.setText(fruit.getName());
holder.priceTextView.setText(String.format(Locale.getDefault(), "¥%.2f", fruit.getPrice()));
return view;
}
private static class ViewHolder {
ImageView imageView;
TextView nameTextView;
TextView priceTextView;
}
}
```
在适配器中,我们使用了ViewHolder模式来优化ListView的性能。同时,我们还使用了Locale.getDefault()方法来格式化价格为本地化货币格式。
最后,我们需要在Activity中将适配器与ListView关联起来,并从数据库中获取水果数据。以下是一个简单的示例:
```java
public class MainActivity extends AppCompatActivity {
private ListView mListView;
private List<Fruit> mFruitList;
private FruitAdapter mAdapter;
private DBHelper mDBHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = findViewById(R.id.list_view);
mDBHelper = new DBHelper(this);
mFruitList = new ArrayList<>();
mAdapter = new FruitAdapter(this, mFruitList);
mListView.setAdapter(mAdapter);
loadFruits();
}
private void loadFruits() {
SQLiteDatabase db = mDBHelper.getWritableDatabase();
Cursor cursor = db.query("fruit", null, null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
float price = cursor.getFloat(cursor.getColumnIndex("price"));
byte[] imageBytes = cursor.getBlob(cursor.getColumnIndex("image"));
Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
mFruitList.add(new Fruit(id, name, price, image));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
mAdapter.notifyDataSetChanged();
}
}
```
在Activity中,我们首先创建了一个ListView和适配器,并将它们关联起来。然后,我们从数据库中获取水果数据,并将它们添加到适配器中。最后,我们调用了适配器的notifyDataSetChanged()方法来更新ListView的显示。
注意,我们在获取图片数据时使用了BitmapFactory.decodeByteArray()方法来将字节数组转换为Bitmap对象。
最终,我们需要定义一个Fruit类来表示水果的信息:
```java
public class Fruit {
private int mId;
private String mName;
private float mPrice;
private Bitmap mImage;
public Fruit(int id, String name, float price, Bitmap image) {
mId = id;
mName = name;
mPrice = price;
mImage = image;
}
public int getId() {
return mId;
}
public String getName() {
return mName;
}
public float getPrice() {
return mPrice;
}
public Bitmap getImage() {
return mImage;
}
}
```
这样,我们就完成了一个简单的水果列表应用程序。您可以根据需要添加更多功能,例如添加新水果、删除水果等。
阅读全文