Android实现水果列表,Android ListView與SQLite綜合使用(水果商品展示案例)

时间: 2023-06-16 14:07:54 浏览: 117
以下是一个简单的Android应用程序,它使用ListView和SQLite数据库来显示水果列表: 1. 建立一个新的Android项目,命名为“FruitList”。 2. 在布局文件中添加ListView组件: ``` <ListView android:id="@+id/listViewFruits" android:layout_width="match_parent" android:layout_height="match_parent"/> ``` 3. 创建一个新的Java类名为“Fruit”,用于表示水果对象。它应该包含以下属性: ``` private int id; private String name; private String description; private double price; ``` 并提供相应的getter和setter方法。 4. 创建一个新的Java类名为“FruitDatabaseHelper”,用于创建和管理SQLite数据库。它应该继承自SQLiteOpenHelper,并实现onCreate和onUpgrade方法。在onCreate方法中创建名为“fruits”的表,其中包含id、name、description和price列。 5. 创建一个新的Java类名为“FruitListAdapter”,用于将Fruit对象显示在ListView中。它应该继承自BaseAdapter,并实现getView、getItemId、getItem和getCount方法。 在getView方法中,使用LayoutInflater从布局文件中创建一个新的视图,并将Fruit对象的属性设置为相应的TextView组件。 6. 在MainActivity类中,创建一个FruitDatabaseHelper对象,并调用getReadableDatabase方法以获取一个可读的SQLite数据库对象。 使用Cursor对象从数据库中检索所有水果,并将它们存储在一个ArrayList<Fruit>中。 7. 创建一个FruitListAdapter对象并将其设置为ListView的适配器。 8. 运行应用程序,你应该看到一个包含所有水果的列表。 这里是完整的MainActivity代码: ``` public class MainActivity extends AppCompatActivity { private ListView listViewFruits; private FruitDatabaseHelper databaseHelper; private ArrayList<Fruit> fruits; private FruitListAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listViewFruits = (ListView) findViewById(R.id.listViewFruits); databaseHelper = new FruitDatabaseHelper(this); fruits = new ArrayList<Fruit>(); SQLiteDatabase db = databaseHelper.getReadableDatabase(); Cursor cursor = db.query("fruits", null, null, null, null, null, null); while (cursor.moveToNext()) { Fruit fruit = new Fruit(); fruit.setId(cursor.getInt(cursor.getColumnIndex("id"))); fruit.setName(cursor.getString(cursor.getColumnIndex("name"))); fruit.setDescription(cursor.getString(cursor.getColumnIndex("description"))); fruit.setPrice(cursor.getDouble(cursor.getColumnIndex("price"))); fruits.add(fruit); } cursor.close(); db.close(); adapter = new FruitListAdapter(this, fruits); listViewFruits.setAdapter(adapter); } } ``` 这里是完整的FruitListAdapter代码: ``` public class FruitListAdapter extends BaseAdapter { private Context context; private ArrayList<Fruit> fruits; public FruitListAdapter(Context context, ArrayList<Fruit> fruits) { this.context = context; this.fruits = fruits; } @Override public int getCount() { return fruits.size(); } @Override public Object getItem(int position) { return fruits.get(position); } @Override public long getItemId(int position) { return fruits.get(position).getId(); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.list_item_fruit, parent, false); } TextView textViewName = (TextView) convertView.findViewById(R.id.textViewName); TextView textViewDescription = (TextView) convertView.findViewById(R.id.textViewDescription); TextView textViewPrice = (TextView) convertView.findViewById(R.id.textViewPrice); textViewName.setText(fruits.get(position).getName()); textViewDescription.setText(fruits.get(position).getDescription()); textViewPrice.setText("$" + String.format("%.2f", fruits.get(position).getPrice())); return convertView; } } ```

相关推荐

最新推荐

recommend-type

android listview实现新闻列表展示效果

主要为大家详细介绍了android listview实现新闻列表展示效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Android编程使用ListView实现数据列表显示的方法

主要介绍了Android编程使用ListView实现数据列表显示的方法,实例分析了Android中ListView控件的使用技巧,需要的朋友可以参考下
recommend-type

Android studio listview实现列表数据显示 数据循环显示效果

主要介绍了Android studio listview实现列表数据显示 数据循环显示功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

Android中使用ListView实现漂亮的表格效果

主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
recommend-type

Android Studio如何获取SQLite数据并显示到ListView上

主要介绍了Android Studio获取SQLite数据并显示到ListView上,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

HSV转为RGB的计算公式

HSV (Hue, Saturation, Value) 和 RGB (Red, Green, Blue) 是两种表示颜色的方式。下面是将 HSV 转换为 RGB 的计算公式: 1. 将 HSV 中的 S 和 V 值除以 100,得到范围在 0~1 之间的值。 2. 计算色相 H 在 RGB 中的值。如果 H 的范围在 0~60 或者 300~360 之间,则 R = V,G = (H/60)×V,B = 0。如果 H 的范围在 60~120 之间,则 R = ((120-H)/60)×V,G = V,B = 0。如果 H 的范围在 120~180 之间,则 R = 0,G = V,B =
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。